How to Import Data to DynamoDB: Simple Steps and Example
To import data to
DynamoDB, you can use the AWS CLI with the batch-write-item command, AWS SDKs to write items programmatically, or AWS Data Pipeline for large datasets. Each method requires data formatted as JSON or CSV and proper table setup.Syntax
Here is the basic syntax to import data using AWS CLI's batch-write-item command:
aws dynamodb batch-write-item --request-items file://data.json
Explanation:
aws dynamodb batch-write-item: AWS CLI command to write multiple items.--request-items: Specifies the JSON file containing the data to import.file://data.json: Path to the JSON file with the data formatted for DynamoDB.
bash
aws dynamodb batch-write-item --request-items file://data.jsonExample
This example shows how to import data into a DynamoDB table named Products using AWS SDK for Python (boto3). It writes two items to the table.
python
import boto3 # Create DynamoDB client client = boto3.client('dynamodb') # Data to import items = [ { 'PutRequest': { 'Item': { 'ProductID': {'S': '101'}, 'Name': {'S': 'Pen'}, 'Price': {'N': '1.20'} } } }, { 'PutRequest': { 'Item': { 'ProductID': {'S': '102'}, 'Name': {'S': 'Notebook'}, 'Price': {'N': '2.50'} } } } ] # Batch write items response = client.batch_write_item( RequestItems={ 'Products': items } ) print('Import response:', response)
Output
Import response: {'UnprocessedItems': {}, 'ResponseMetadata': {'RequestId': '...', 'HTTPStatusCode': 200, 'HTTPHeaders': {...}, 'RetryAttempts': 0}}
Common Pitfalls
Common mistakes when importing data to DynamoDB include:
- Not formatting data correctly as DynamoDB expects JSON with attribute types (e.g.,
{"S": "string"}for strings). - Exceeding batch write limits (max 25 items per batch).
- Ignoring unprocessed items returned by batch-write-item, which need retrying.
- Not setting the correct primary key attributes in the data.
Always validate your data format and handle unprocessed items to ensure complete import.
json
## Wrong: Missing attribute types
{
"Products": [
{"ProductID": "101", "Name": "Pen", "Price": 1.20}
]
}
## Right: Correct DynamoDB JSON format
{
"Products": [
{
"PutRequest": {
"Item": {
"ProductID": {"S": "101"},
"Name": {"S": "Pen"},
"Price": {"N": "1.20"}
}
}
}
]
}Quick Reference
| Method | Description | Use Case |
|---|---|---|
| AWS CLI batch-write-item | Import JSON data in batches | Small to medium datasets |
| AWS SDK (e.g., boto3) | Programmatic import with code | Custom import logic or automation |
| AWS Data Pipeline | Automated large-scale data import | Large datasets or scheduled imports |
| AWS Glue | ETL service for complex transformations | Data transformation before import |
Key Takeaways
Use AWS CLI batch-write-item or SDKs to import data formatted with DynamoDB attribute types.
Batch writes support up to 25 items per request; handle unprocessed items by retrying.
Ensure your data includes the correct primary key attributes matching your table schema.
For large or complex imports, consider AWS Data Pipeline or AWS Glue services.
Always validate your JSON data format before importing to avoid errors.