Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 !!

like image 955
Lasit Pant Avatar asked Feb 06 '18 14:02

Lasit Pant


People also ask

How does boto3 connect to DynamoDB python?

Connecting AWS resources to the python environment requires a boto3 package. Creating a dynamo DB client is a connection instance that lets us connect with our dynamo DB service. We need to specify region_name , aws_access_key_id , aws_secret_access_key in order to connect with our dynamoDb service.


1 Answers

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='***')
like image 72
nevsv Avatar answered Sep 30 '22 12:09

nevsv