Boto3 and DynamoDB: Integrating AWS’s NoSQL Service with Python

Saumya - May 8 - - Dev Community

AWS Boto3 is the Amazon Web Services (AWS) SDK for Python, which provides an interface to interact with various AWS services, including DynamoDB, a fully managed NoSQL database service. Using Boto3 with DynamoDB allows developers to programmatically create, read, update, and delete data in DynamoDB tables. Here's an overview of working with AWS Boto3 DynamoDB:

Setting Up Boto3

Install Boto3: Use pip to install Boto3 if you haven't already:

pip install boto3

Configure AWS Credentials: Ensure that AWS credentials (access key ID and secret access key) are configured either through environment variables, AWS CLI configuration, or IAM roles (for running on AWS services).

Working with DynamoDB using Boto3

Import Boto3 and DynamoDB Client:

import boto3

Create a DynamoDB client

dynamodb = boto3.client('dynamodb')

CRUD Operations:

Create Table:

response = dynamodb.create_table(
TableName='YourTableName',
KeySchema=[
{
'AttributeName': 'primaryKey',
'KeyType': 'HASH' # Partition key
}
],
AttributeDefinitions=[
{
'AttributeName': 'primaryKey',
'AttributeType': 'S' # String
}
],
ProvisionedThroughput={
'ReadCapacityUnits': 5,
'WriteCapacityUnits': 5
}
)

Put Item (Insert Data):

response = dynamodb.put_item(
TableName='YourTableName',
Item={
'primaryKey': {'S': 'Value1'},
'attribute1': {'N': '123'},
'attribute2': {'S': 'Value2'}
}
)

Get Item (Retrieve Data):

response = dynamodb.get_item(
TableName='YourTableName',
Key={
'primaryKey': {'S': 'Value1'}
}
)
item = response.get('Item')

Update Item:

response = dynamodb.update_item(
TableName='YourTableName',
Key={
'primaryKey': {'S': 'Value1'}
},
UpdateExpression='SET attribute1 = :val1',
ExpressionAttributeValues={
':val1': {'N': '456'}
}
)

Delete Item:

response = dynamodb.delete_item(
TableName='YourTableName',
Key={
'primaryKey': {'S': 'Value1'}
}
)

Query and Scan Operations:

Query:

response = dynamodb.query(
TableName='YourTableName',
KeyConditionExpression='primaryKey = :val',
ExpressionAttributeValues={
':val': {'S': 'Value1'}
}
)

Scan:

response = dynamodb.scan(
TableName='YourTableName',
FilterExpression='attribute1 > :val',
ExpressionAttributeValues={
':val': {'N': '100'}
}
)

Handling Responses

Boto3 DynamoDB methods return a response dictionary containing the operation result.

Check for 'ResponseMetadata' and 'HTTPStatusCode' in the response to ensure the operation was successful.

Error Handling

Use try-except blocks to handle exceptions, such as ClientError, that may occur during DynamoDB operations.

By following these examples and guidelines, you can leverage AWS Boto3 to interact with DynamoDB programmatically from your Python applications.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player