Hardcoded Credentials for Dynamodb AWS Queries

Question:

I have a script which checks if a specific value is inside a cell in a dynamodb table in AWS. I used to add hardcoded credentials containing the secret key in my script such as this:

dynamodb_session = Session(aws_access_key_id='access_key_id',
          aws_secret_access_key='secret_access_key',
          region_name='region')

dynamodb = dynamodb_session.resource('dynamodb')

table=dynamodb.Table('table_name')

Are there any other ways to use those credentials without adding them to my script ? Thank you.

Asked By: bkullukcu

||

Answers:

If you are running that code on an Amazon EC2 instance, then you simply need to assign an IAM Role to the instance and it will automatically receive credentials.

If you are running that code on your own computer, then use the AWS Command-Line Interface (CLI) aws configure command to store the credentials in a local configuration file. (It will be stored in ~/.aws/credentials).

Then, in both cases, you can simply use:

dynamodb = boto3.resource('dynamodb')

You can set the default region in that configuration too.

Answered By: John Rotenstein