Replicating Amazon DynamoDB tables in different regions
Often developers who work on AWS DynamoDB happen to replicate one or more tables on a different region. This can be done using AWS S3 and AWS Data Pipeline as explained here.
But for those hardcore command-line fans who don’t like to do click, click, click (same as me). The following is a simple python script which uses boto3, that will replicate the required table in a different region with the same key schema, attribute definitions and copy data into it.
How the script works:
- Scan the existing table for schema, attributes and data.
- Create a new table in the new region with the same schema and attribute definitions.
- Usually, DynamoDB takes a couple of seconds to create the table. This is handled by wait_until_exists(), which polls every 20 seconds until a successful state is reached.
- Reload to update the attributes of the table resource, thus get the actual table status.
- Once the new table is active write all the items scanned from the existing table into it.
python dynamodb_replicate_table.py -t my-table -r eu-west-1 -nr us-east-2
The above will replicate the table named “my-table” in eu-west-1 to us-east-2.