Project Management
March 19, 2024
Migrating a DynamoDB table to a Global table with 4 easy steps. No downtime no data loss.
Introduction
To make a production environment multi-region in AWS, we want to transform the DynamoDB single-region tables into global tables.
Similar to the manual method, go to DynamoDB and create a replica in another region under the “Global Table” tab. When working with a CloudFormation stack, changing the AWS::DynamoDB::Table resource in the CloudFormation template to AWS::DynamoDB::GlobalTable is not a viable solution.
Unfortunately, this approach will most likely result in the deletion of your existing tables, creation of new global tables, and a complete data loss. Obviously, this is not a scenario we could accept.
After some research, we found a super-easy solution to achieve our goal. Our migration plan works as follows wirh only 4 simple steps:
Update your CloudFormation template to include a “DeletionPolicy Retain” [1] for each resource. The “DeletionPolicy: Retain” ensures that even if the CloudFormation template is deleted, the resources associated with the stack will not be deleted.
Delete DynamoDB stack. As a part of the migration plan, you will carefully delete the exisiting DynamoDB stack, resulting in a standalone regular DynamoDB table that was no longer associated with any provisioning.
Create replicas in your FailOver regions under the “global table” tab in the DynamoDB console, and so your DyanmoDB table will become a global table. This action is easy and safe — there is no chance of data loss and no downtime (only some lags and slowness on the replicas while being created).
With CDK - generate a new CloudFormation template [1] for DynamoDB, designed specifically for global tables. The previous template, which defined a regular table, contained a resource of type AWS::DynamoDB::Table In contrast, the new template was structured around the AWS::DynamoDB::GlobalTabletype, and included a “Replicas” property to define all the replicas we had created.
* [2] Below you can see a full example of the before and after
Now that you have created a new CloudFormation stack with the generated CloudFormation template that supports a global table, the new stack can use the "import existing resources" method rather than creating a new resource.
Using the CloudFormation console, we will find that it is straightforward to select 'Create Stack' and choose from the existing (import) resources. We will then upload the template and specify the name of the table we want to import. Making the process hassle-free.
You now successfully linked your DynamoDB global table to a CloudFormation stack, generated using the AWS Cloud Development Kit (CDK).
Every time you will change the code with the CDK, which updates the CloudFormation template and results in a deployment, your GlobalTable will reflect these updates.
Side notes:
[1] In order to add a deletion policy, under the type add DeletionPolicy (indented the same in the same tab as the type):
[2] To generate a global DynamoDB table using the CDK we used the “CfnGlobalTable” class, this is the preferred class to use since
the “GlobalTable” class generated a weird yaml with all kind of dependencies.
[3] Before:
[4] After:
We hope this article is useful to you!
If you require any assistance, please don’t hesitate to reach out to us at adam@anan.tools or fill out
this form for a FREE advisory session on cloud optimization with a solution Architect.
https://forms.gle/i1ScUgbaJ4txKbzQ9
Conclusion
Subscribe to newsletter
By subscribing you agree to with our
Other sticky posts
Explore valuable insights and dive into relevant topics.
Project Management
Migrating a DynamoDB table to a Global table with 4 easy steps. No downtime no data loss.
This article is intended for organizations that have a DynamoDB table associated with a CloudFormation stack and want to convert it into a global DynamoDB table without experiencing downtime or data loss. The article also includes CloudFormation templates generated with CDK.
Productivity Tips
You are probably paying AWS too much!
A complete guide on cloud cost optimization
Productivity Tips
You are probably paying AWS too much — Part 2
Part 2: Leverage DynamoDB to your advantage