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:


  1. 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.


  2. 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.


Fill out our cloud cost optimization questionnaire and get a free consultation session with a cloud solution architect — click here



  1. 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).


  2. 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.

PointInTimeRecoverySpecification:
PointInTimeRecoveryEnabled:
trueRegion:
us-east-1- PointInTimeRecoverySpecification:
PointInTimeRecoveryEnabled: 
trueRegion: 
us-east-2- PointInTimeRecoverySpecification:
PointInTimeRecoveryEnabled: 
trueRegion: 
eu-central-1

* [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):

MyDyanmoDBTable:
  Type: AWS::DynamoDB::Table
  DeletionPolicy: Retain


[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:

  MyDyanmoDBTable:
    Type: AWS::DynamoDB::Table
    UpdateReplacePolicy: Retain
    DeletionPolicy: Retain
    Properties:
      KeySchema:
        - AttributeName: key
          KeyType: HASH
      AttributeDefinitions:
        - AttributeName: key
          AttributeType: S
      BillingMode: PAY_PER_REQUEST
      PointInTimeRecoverySpecification:
        PointInTimeRecoveryEnabled: true
      TableName: MyTable


[4] After:

MyDyanmoDBTable:
    Type: AWS::DynamoDB::GlobalTable
    UpdateReplacePolicy: Retain
    DeletionPolicy: Retain
    Properties:
      AttributeDefinitions:
        - AttributeName: key
          AttributeType: S
      KeySchema:
        - AttributeName: key
          KeyType: HASH
      Replicas:
        - PointInTimeRecoverySpecification:
            PointInTimeRecoveryEnabled: true
          Region: us-east-1
        - PointInTimeRecoverySpecification:
            PointInTimeRecoveryEnabled: true
          Region: us-east-2
        - PointInTimeRecoverySpecification:
            PointInTimeRecoveryEnabled: true
          Region: eu-central-1
      BillingMode: PAY_PER_REQUEST
      TableName: MyTable


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

Subscribe to receive the latest blog posts to your inbox every week.

Subscribe to receive the latest blog posts to your inbox every week.

By subscribing you agree to with our

Master Your Sticky
Note Strategy!

Master Your Sticky Note Strategy!

Master Your Sticky Note Strategy!

Discover the key to increased productivity! Explore our streamlined solution designed to simplify your workflow, boost collaboration, and propel you towards success with ease.

Discover the key to increased productivity! Explore our streamlined solution designed to simplify your workflow, boost collaboration, and propel you towards success with ease.

Discover the key to increased productivity! Explore our streamlined solution designed to simplify your workflow, boost collaboration, and propel you towards success with ease.

© 2024 anan tools by ARG Ventures ltd. All rights reserved.

© 2024 ANAN Tools by ARG Ventures ltd. All rights reserved.

© 2024 ANAN Tools by ARG Ventures ltd. All rights reserved.