Last modified: Jan 01, 2026 By Alexander Williams

Integrate AWS DynamoDB with Python Boto3 Guide

This guide shows how to use Python with AWS DynamoDB. DynamoDB is a fast NoSQL database. Python's Boto3 library makes integration simple.

You will learn to set up, create tables, and manage data. This is perfect for developers starting with serverless data.

Prerequisites for Integration

You need an AWS account and Python installed. Basic Python knowledge is required. AWS CLI setup is also recommended.

Install Boto3 using pip. This is the AWS SDK for Python. It allows interaction with all AWS services.


pip install boto3

Configure AWS credentials on your machine. Use the AWS CLI or environment variables. This grants your code access.

Setting Up Boto3 Client for DynamoDB

First, import boto3 in your Python script. Then create a DynamoDB client or resource. The resource offers a higher-level abstraction.


import boto3

# Create a low-level client
dynamodb_client = boto3.client('dynamodb', region_name='us-east-1')

# Create a high-level resource (often easier)
dynamodb = boto3.resource('dynamodb', region_name='us-east-1')

The resource object is simpler for common tasks. The client gives more detailed control. Choose based on your needs.

Creating a DynamoDB Table with Python

Tables need a primary key. This key uniquely identifies items. It consists of a partition key and optional sort key.

Use the create_table method. Define key schema and attribute definitions. Also set provisioned throughput or use on-demand.


def create_music_table():
    table = dynamodb.create_table(
        TableName='Music',
        KeySchema=[
            {
                'AttributeName': 'Artist',
                'KeyType': 'HASH'  # Partition key
            },
            {
                'AttributeName': 'SongTitle',
                'KeyType': 'RANGE'  # Sort key
            }
        ],
        AttributeDefinitions=[
            {
                'AttributeName': 'Artist',
                'AttributeType': 'S'  # String type
            },
            {
                'AttributeName': 'SongTitle',
                'AttributeType': 'S'
            }
        ],
        BillingMode='PAY_PER_REQUEST'  # On-demand pricing
    )
    # Wait for the table to be created
    table.meta.client.get_waiter('table_exists').wait(TableName='Music')
    print("Table created successfully.")
    return table

music_table = create_music_table()

Table created successfully.

On-demand billing is great for variable workloads. Provisioned capacity requires planning for consistent traffic.

Performing CRUD Operations

CRUD means Create, Read, Update, Delete. These are core data actions. Let's explore each with examples.

Create (Put) an Item

Use the put_item method. Provide the full item as a dictionary. The primary key attributes are mandatory.


response = music_table.put_item(
   Item={
        'Artist': 'The Beatles',
        'SongTitle': 'Yesterday',
        'Year': 1965,
        'Genre': 'Rock'
    }
)
print("Item added.")

Read (Get) an Item

Use the get_item method. You must specify the full primary key. It returns the item if found.


response = music_table.get_item(
    Key={
        'Artist': 'The Beatles',
        'SongTitle': 'Yesterday'
    }
)
item = response.get('Item')
if item:
    print(f"Retrieved item: {item}")
else:
    print("Item not found.")

Retrieved item: {'Artist': 'The Beatles', 'SongTitle': 'Yesterday', 'Year': 1965, 'Genre': 'Rock'}

Update an Item

Use the update_item method. It can modify attributes or add new ones. Use UpdateExpression to define changes.


music_table.update_item(
    Key={
        'Artist': 'The Beatles',
        'SongTitle': 'Yesterday'
    },
    UpdateExpression="SET Album = :album_val",
    ExpressionAttributeValues={
        ':album_val': 'Help!'
    },
    ReturnValues="UPDATED_NEW"
)
print("Item updated.")

Delete an Item

Use the delete_item method. Provide the primary key. The item is permanently removed.


music_table.delete_item(
    Key={
        'Artist': 'The Beatles',
        'SongTitle': 'Yesterday'
    }
)
print("Item deleted.")

Querying and Scanning Tables

Querying is efficient. It uses the primary key. Scanning reads every item, which can be slow and costly.

Use query for fast lookups. Use scan only when necessary. Always apply filters to limit data.


# Query for all songs by The Beatles
from boto3.dynamodb.conditions import Key
response = music_table.query(
    KeyConditionExpression=Key('Artist').eq('The Beatles')
)
items = response['Items']
for item in items:
    print(item['SongTitle'])

For more complex automation, see our guide on AWS Task Scheduling with Python & CloudWatch.

Best Practices for Performance

Design your table schema based on access patterns. This is crucial for DynamoDB. Avoid using scans on large tables.

Use secondary indexes for flexible queries. They allow querying on non-key attributes. This maintains performance.

Implement exponential backoff for throttling. DynamoDB may throttle requests. Boto3 can retry automatically.

For managing other AWS resources, learn about Automate AWS EC2 Management with Python Boto3.

Error Handling in Boto3

Always wrap calls in try-except blocks. Catch specific exceptions like ClientError. This makes your app robust.


from botocore.exceptions import ClientError

try:
    response = music_table.get_item(Key={'Artist': 'Unknown'})
except ClientError as e:
    print(f"Error: {e.response['Error']['Message']}")

Integrating with Serverless Applications

DynamoDB pairs well with AWS Lambda. It is a popular serverless database. Data access is fast and scalable.

You can build full APIs. Combine DynamoDB with API Gateway. This creates a powerful backend.

For a complete serverless setup, read Build Serverless APIs with AWS API Gateway and Python.

Conclusion

Integrating DynamoDB with Python is straightforward. Boto3 provides all necessary tools. You can manage data effectively.

Start with simple CRUD operations. Then explore queries and advanced patterns. DynamoDB scales with your needs.

Remember to follow best practices. Design your schema carefully. Handle errors gracefully.

This integration is key for modern cloud apps. It supports serverless architectures perfectly. Happy coding!