Lasit Pant
Lasit Pant

Reputation: 317

How to establish a connection to DynamoDB using python using boto3

I am bit new to AWS and DynamoDB. My aim is to embed a small piece of code. The problem I am facing is how to make a connection in python code. I made a connection using AWS cli and then entering access ID and key. But how to do it in my code as i wish to deploy my code on other systems.

Thanks in advance !!

Upvotes: 6

Views: 24624

Answers (1)

nevsv
nevsv

Reputation: 2466

First of all read documentation for boto3 dynamo, it's pretty simple:

http://boto3.readthedocs.io/en/latest/reference/services/dynamodb.html

If you want to provide access keys while connecting to dynamo, you can do the following:

client = boto3.client('dynamodb',aws_access_key_id='yyyy', aws_secret_access_key='xxxx', region_name='***')

But, remember, it is against best practices from security perspective to store such keys within the code.

For best security efforts use IAM roles. boto3 driver will automatically consume IAM role if it is attached to the instance. Link to the docs: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/iam-roles-for-amazon-ec2.html

Also, if IAM roles is to complicated, you can install and aws-cli and run aws configure on your server, and boto3 will use the key from here (less secure than a previous approach).

After implementing one of the options, you can connect to DynamoDB without the keys from code:

client = boto3.client('dynamodb', region_name='***')

Upvotes: 9

Related Questions