Getting Started with DynamoDB and Python

Last updated 1 month ago | 97 views 75     5

Tags:- Python DynamoDB

Amazon DynamoDB is a fully managed NoSQL database service offered by AWS. It provides fast and predictable performance with seamless scalability. In this tutorial, you'll learn how to get started with DynamoDB using Python, including setting up your environment, creating tables, inserting data, and querying your database.


Table of Contents

  1. What is DynamoDB?

  2. Why Use DynamoDB with Python?

  3. Prerequisites

  4. Install Boto3 (AWS SDK for Python)

  5. Configure AWS Credentials

  6. Create a DynamoDB Table

  7. Insert (Put) Items

  8. Retrieve (Get) Items

  9. Full Working Example

  10. Tips and Best Practices

  11. Common Pitfalls

  12. Conclusion


What is DynamoDB?

DynamoDB is a key-value and document database that delivers single-digit millisecond performance at any scale. It's serverless, highly available, and designed for performance.

Key features:

  • NoSQL (schema-less)

  • Built-in replication and backup

  • Integrates well with other AWS services

  • Uses tables, items (rows), and attributes (columns)


❓ Why Use DynamoDB with Python?

Python is widely used for backend development, data science, and scripting. When combined with Boto3, the AWS SDK for Python, it becomes easy to interact with DynamoDB.

Use cases:

  • Serverless applications

  • IoT data collection

  • Real-time analytics

  • Caching and session storage


✅ Prerequisites

Before you begin, ensure you have:

  • An AWS account

  • Python 3.7 or higher

  • AWS IAM credentials with access to DynamoDB

  • AWS CLI (optional but recommended)


Step 1: Install Boto3

pip install boto3

This installs the Boto3 library, which is the official AWS SDK for Python.


Step 2: Configure AWS Credentials

You can configure your credentials using the AWS CLI:

aws configure

You'll be prompted to enter:

  • AWS Access Key ID

  • AWS Secret Access Key

  • Default region (e.g., us-east-1)

  • Output format (e.g., json)

Alternatively, you can use a .env file or directly set environment variables in Python (not recommended for production).


Step 3: Create a DynamoDB Table

import boto3

# Create DynamoDB client
dynamodb = boto3.resource('dynamodb', region_name='us-east-1')

# Create table
table = dynamodb.create_table(
    TableName='Users',
    KeySchema=[
        {
            'AttributeName': 'user_id',
            'KeyType': 'HASH'  # Partition key
        }
    ],
    AttributeDefinitions=[
        {
            'AttributeName': 'user_id',
            'AttributeType': 'S'  # S = String, N = Number, B = Binary
        }
    ],
    ProvisionedThroughput={
        'ReadCapacityUnits': 5,
        'WriteCapacityUnits': 5
    }
)

# Wait until the table exists
table.meta.client.get_waiter('table_exists').wait(TableName='Users')
print("Table status:", table.table_status)

Step 4: Insert (Put) Items

table = dynamodb.Table('Users')

response = table.put_item(
   Item={
        'user_id': '123',
        'name': 'Alice',
        'email': '[email protected]'
    }
)

print("PutItem succeeded:", response)

Step 5: Retrieve (Get) Items

response = table.get_item(
    Key={
        'user_id': '123'
    }
)

item = response.get('Item')
print("Retrieved Item:", item)

✅ Full Working Example

import boto3

# Connect to DynamoDB
dynamodb = boto3.resource('dynamodb', region_name='us-east-1')

# Create table (if needed)
table_name = 'Users'
existing_tables = [table.name for table in dynamodb.tables.all()]
if table_name not in existing_tables:
    table = dynamodb.create_table(
        TableName=table_name,
        KeySchema=[{'AttributeName': 'user_id', 'KeyType': 'HASH'}],
        AttributeDefinitions=[{'AttributeName': 'user_id', 'AttributeType': 'S'}],
        ProvisionedThroughput={'ReadCapacityUnits': 5, 'WriteCapacityUnits': 5}
    )
    table.meta.client.get_waiter('table_exists').wait(TableName=table_name)

# Insert data
table = dynamodb.Table(table_name)
table.put_item(Item={'user_id': '001', 'name': 'John Doe', 'email': '[email protected]'})

# Retrieve data
response = table.get_item(Key={'user_id': '001'})
item = response.get('Item')
print("User:", item)

Tips and Best Practices

Tip Description
✅ Use provisioned or on-demand billing modes based on workload  
✅ Define proper partition keys for even data distribution  
✅ Use boto3.resource() for higher-level operations  
Store AWS credentials securely, not in code  
Monitor read/write capacity and throttling with CloudWatch  
Use local DynamoDB for offline testing: amazon/dynamodb-local  

⚠️ Common Pitfalls

Pitfall Solution
❌ Not waiting for table creation Use waiter to wait until table is active
❌ Using incorrect region Always specify correct region_name
❌ Using duplicate keys Ensure user_id is unique when inserting
❌ Credentials not configured Use aws configure or environment variables

Conclusion

DynamoDB is a powerful, serverless NoSQL database that pairs perfectly with Python for building modern cloud applications. This guide covered everything to help you get started:

✅ Installing and configuring Boto3
✅ Creating a table
✅ Inserting and retrieving data
✅ Understanding best practices and pitfalls