How to Migrate DynamoDB Tables Between AWS Accounts
In this article
Migrating Amazon DynamoDB tables between AWS accounts is a common requirement for restructuring infrastructure, separating environments, or consolidating services. The most reliable method involves using the AWS Serverless Application Model (SAM) CLI to export data from the source account and import it into the destination account. This process ensures data consistency while allowing you to manage table schemas, indexes, and encryption settings independently in the target environment.
What You'll Need
- Two active AWS accounts: one as the source and one as the destination.
- AWS Command Line Interface (CLI) installed and configured with credentials for both accounts.
- The AWS SAM CLI installed on your local machine or CI/CD runner.
- Sufficient permissions in both accounts:
AdministratorAccessor specific IAM policies allowing DynamoDB read/write, S3 bucket creation/access, and CloudFormation stack management. - An Amazon S3 bucket in the destination account to temporarily store the exported data dumps.
Step-by-Step
- Prepare the Destination S3 Bucket: In the destination AWS account, create an S3 bucket that will hold the temporary export files. Ensure the bucket policy allows the IAM role or user performing the migration to write objects. Note the ARN of this bucket, as it will be required for the export command.
- Configure Source Account Credentials: Open your terminal and configure your AWS CLI profile for the source account. Run
aws configure --profile source-accountand enter the access key, secret key, and default region where your DynamoDB table resides. - Export the DynamoDB Table: Use the SAM CLI to export the table data. Execute the following command, replacing placeholders with your specific details:
However, for direct table migration without a full serverless app deployment, use thesam package \ --template-file template.yaml \ --output-template-file packaged.yaml \ --s3-bucket destination-s3-bucket-nameaws dynamodb export-table-to-point-in-timecommand if using native exports, or better yet, utilize the SAM CLI's built-in migration capabilities if available in your version. A more standard approach for simple table migration is using theaws cloudformation packagecombined with a custom script, but the most robust modern way is using theaws s3 syncafter exporting viaaws dynamodb export-table-to-point-in-time. Let's stick to the SAM CLI method which simplifies the packaging:
If thesam migrate \ --source-account-id SOURCE_ACCOUNT_ID \ --destination-account-id DESTINATION_ACCOUNT_ID \ --table-name YOUR_TABLE_NAME \ --region us-east-1sam migratecommand is not available in your CLI version, fall back to manual export/import. Export the table usingaws dynamodb export-table-to-point-in-timeto an S3 bucket in the destination account. Wait for the job to complete and check its status. - Import Data into Destination: Once the export job is complete, create a new DynamoDB table in the destination account with the same schema (or modified schema if needed). Then, initiate an import job using
aws dynamodb import-table, pointing to the S3 location of the exported data. Specify the role ARN that has permission to read from S3 and write to DynamoDB. - Verify Data Integrity: After the import job completes, compare the item count and size metrics between the source and destination tables. You can use the
aws dynamodb describe-tablecommand to check these metrics. Additionally, perform a sample query on random partitions to ensure data readability and correctness. - Update Application Configurations: Update your application code, Lambda functions, or other services that reference the table to point to the new table ARN in the destination account. Test connectivity thoroughly before decommissioning the old table.
Common Mistakes
One frequent error is neglecting to update the IAM roles associated with the applications accessing the table. Even if the data is migrated, the application will fail if it still tries to access the old table ARN or lacks permissions for the new one. Another mistake is attempting to migrate large tables during peak traffic hours, which can lead to throttling and increased costs due to high Read Capacity Units (RCUs) consumed during the export process. Always monitor CloudWatch metrics during the migration window.
Tips
To minimize downtime, consider using a dual-write strategy where your application writes to both the source and destination tables simultaneously during the transition period. This allows you to switch reads to the new table once it is fully synced. Additionally, always take a snapshot backup of the source table before starting the migration. This provides a safety net in case the import fails or data corruption occurs. Using consistent naming conventions for your S3 buckets and IAM roles across accounts will also streamline the process and reduce configuration errors.
Troubleshooting
If the export job fails, check the S3 bucket permissions first. The DynamoDB service needs permission to write to the specified bucket. Look for Access Denied errors in the CloudTrail logs. If the import job hangs or fails, verify that the IAM role passed to the import-table command has the necessary s3:GetObject and dynamodb:PutItem permissions. Ensure that the KMS keys used for encryption in the source table are either accessible in the destination account or that you have exported unencrypted data. Network issues, such as VPC endpoints blocking S3 access, can also cause failures; ensure your compute resources have proper network connectivity to the S3 bucket.
Frequently Asked Questions
Can I migrate DynamoDB tables with different names?
Yes, the migration process allows you to specify a new table name in the destination account. You simply define the target table name during the import phase, and the schema can be adjusted if necessary, though keeping the schema identical is recommended for simplicity.
Does this method preserve TTL settings?
No, Time-To-Live (TTL) settings are not automatically transferred during the data export and import process. You must manually re-enable TTL on the destination table after the migration is complete.
Is there a limit to the table size I can migrate?
While there is no hard limit imposed by the migration tool itself, very large tables may take significant time to export and import. It is advisable to plan for extended maintenance windows and monitor throughput limits to avoid throttling during the transfer.
Do I need to stop writes to the table during migration?
Strictly speaking, no, but it is highly recommended to pause writes or implement a dual-write strategy to ensure data consistency. Migrating live data without synchronization mechanisms can result in lost updates or duplicate entries.
Can I migrate only a portion of the data?
The standard export-import method migrates all items in the table. To migrate a subset, you would need to filter the data during the export process using a custom script or by exporting to a filtered S3 location, which requires additional development effort.