create_table
Create DynamoDB tables with AWS SDK for Python (Boto3). Learn how to define attributes, schema, and provisioned throughput for your DynamoDB tables.
DynamoDB Create Table
Understanding DynamoDB Table Creation
This section demonstrates how to create a table in Amazon DynamoDB using the AWS SDK for Python, commonly known as Boto3. DynamoDB is a fully managed NoSQL database service that provides fast and predictable performance with seamless scalability. Creating a table is a fundamental step before you can start storing and retrieving data.
Key Components for Table Creation
When creating a DynamoDB table, several key components need to be defined:
- Attribute Definitions: These specify the name and data type of each attribute that will be used as a key attribute (primary key or sort key) or as part of an index. Common attribute types include 'S' for String, 'N' for Number, and 'B' for Binary.
- Table Name: A unique identifier for your table within your AWS account and region.
- Key Schema: This defines the primary key structure of your table. It consists of a partition key (HASH key) and an optional sort key (RANGE key). The partition key determines the physical partition where your data is stored, while the sort key allows you to sort items within a partition.
- Provisioned Throughput: For provisioned capacity mode, you specify the read and write capacity units (RCUs and WCUs) that your table will consume. This helps manage costs and performance.
Python Code Example for Creating a DynamoDB Table
The following Python code snippet utilizes Boto3 to create a DynamoDB table named 'gamescores'. It defines 'event' as the HASH key and 'timestamp' as the RANGE key, both of type String ('S'). It also sets the provisioned throughput for both read and write operations.
import boto3
import time
# Initialize DynamoDB resource with endpoint URL for local testing (e.g., DynamoDB Local)
# Replace with your actual AWS credentials and region if not using local setup
client = boto3.Session(
region_name='eu-west-1'
).resource(
'dynamodb',
aws_access_key_id='YOUR_ACCESS_KEY_ID', # Replace with your access key ID or remove if using IAM roles
aws_secret_access_key='YOUR_SECRET_ACCESS_KEY', # Replace with your secret access key or remove if using IAM roles
endpoint_url='http://localhost:4567' # Use this for DynamoDB Local, remove for AWS cloud
)
try:
response = client.create_table(
AttributeDefinitions=[
{
'AttributeName': 'event',
'AttributeType': 'S' # String type for the partition key
},
{
'AttributeName': 'timestamp',
'AttributeType': 'S' # String type for the sort key
}
],
TableName='gamescores', # The name of the table to create
KeySchema=[
{
'AttributeName': 'event',
'KeyType': 'HASH' # Partition key
},
{
'AttributeName': 'timestamp',
'KeyType': 'RANGE' # Sort key
}
],
ProvisionedThroughput={
'ReadCapacityUnits': 1, # Number of read units
'WriteCapacityUnits': 10 # Number of write units
}
)
# Wait for the table to be created and become active
print("Waiting for table 'gamescores' to be created...")
table = client.Table('gamescores')
table.meta.client.get_waiter('table_exists').wait(TableName='gamescores')
print("Table 'gamescores' created successfully.")
print(response)
except Exception as e:
print(f"Error creating table: {e}")
Best Practices for DynamoDB Table Design
When designing your DynamoDB tables, consider the following:
- Choose appropriate primary keys: A good partition key distributes data evenly across partitions to avoid hot spots. A sort key can help in querying and organizing data within a partition.
- Understand Provisioned Throughput: Accurately estimate your read and write traffic to set appropriate RCUs and WCUs. You can use Auto Scaling to adjust throughput automatically based on traffic.
- Data Types: Select the most efficient data types for your attributes to optimize storage and performance.
- Local Secondary Indexes (LSIs) and Global Secondary Indexes (GSIs): Use indexes to support query patterns that are not covered by your primary key.
Further Resources
- Amazon DynamoDB Developer Guide
- Boto3 DynamoDB Create Table Documentation
- ISO 8601 Standard (Relevant for timestamp formats)