Nirmal
Nirmal

Reputation: 9549

Boto3 Not Locating Credentials with DynamoDB Encryption SDK

For some reason, the credentials supplied in boto3's Session is not being picked up the EncryptedTable method of dynamodb-encryption-sdk. The same credentials work if I just use the unecrypted table method direct from boto3.

import boto3
from dynamodb_encryption_sdk import EncryptedTable
from dynamodb_encryption_sdk.material_providers.aws_kms import AwsKmsCryptographicMaterialsProvider
from environs import Env

env = Env()
env.read_env('local.env', False)

session = boto3.Session(aws_access_key_id=env('AWS_ACCESS_ID'),
                        aws_secret_access_key=env('AWS_SECRET_KEY'),
                        region_name=env('AWS_REGION'))

dynamodb = session.resource('dynamodb')

table = dynamodb.Table('accounts-table')

aws_cmk_id = env('AWS_CMK_ID')
aws_kms_cmp = AwsKmsCryptographicMaterialsProvider(key_id=aws_cmk_id)

encrypted_table = EncryptedTable(
    table=table,
    materials_provider=aws_kms_cmp,
)

plaintext_item = {
    'account_id': '4548',
    'account_name': 'Blah',
}

encrypted_table.put_item(Item=plaintext_item)

This is what I get while executing this code:

  File "/Users/nirmalnatarajan/venvs/account-postman/lib/python3.7/site-packages/botocore/auth.py", line 357, in add_auth
    raise NoCredentialsError
botocore.exceptions.NoCredentialsError: Unable to locate credentials

Any idea what I might be doing wrong? Appreciate your help.

Upvotes: 0

Views: 765

Answers (2)

Alex Hall
Alex Hall

Reputation: 36043

If you name your .env variables AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY then they will be picked up automatically from os.environ and you don't need a session.

Upvotes: 1

Milan Cermak
Milan Cermak

Reputation: 8074

Try passing in the session to the AwsKmsCryptographicMaterialsProvider:

aws_kms_cmp = AwsKmsCryptographicMaterialsProvider(key_id=aws_cmk_id,
                                                   botocore_session=session)

Alternatively, I think you could set it as the default session.

Upvotes: 1

Related Questions