- Published on
Dynamo DB Cross Region Migration
- Authors
- Name
- Chloe McAree (McAteer)
- @ChloeMcAteer3
When setting up a database such as DynamoDB it is important to choose a region that is closest to where your users are located in order to reduce latency for sending/retrieving data. However, if you have realised that your database is not in the optimal region for your users, it is possible to do a cross region restore with DynamoDB. In this blog, I am going to take you through restoring a table and updating it with Cloud Formation templates!
Overview of steps we will take:
Create a backup from the Point-in-Time Recovery
Restore the backup to a new region
Import the newly restored table into Cloud Formation
Update the Cloud Formation stack for the new table with the template of the original table
Creating the backup
When you create an on-demand backup, a timestamp of the request is catalogued. The backup is created asynchronously by applying all of the changes until the time of the request to the last full table snapshot.
To create a backup of a table, click on the Backups tab on the left hand side panel in the DynamoDB console and click on the blue Create backup button at the top.
This will then prompt you for the name of the table you wish to backup and also for a name for the backup itself:
Once the backup has been created you will see it listed on the console and you are able to select it:
With the backup selected you can then press the Restore backup button at the top!
For more information on backups check out the docs here!
Cross Region Restore
To restore the table you’ll need to provide a name for the new table, in this case I kept it the same as the original as it will be moving regions:
To restore this table in to a different region all you have to do is select Cross Region and specify the region you want to restore it to — in this case I want to move it to EU West:
You are presented with an overview of the restore and then you can simply hit Restore at the bottom!
And now if you go to the region you selected, you will be able to see that the table is being restored:
Once it is restored, the tables status will be set to Active:
However, the restored table that is created is not actually identical to the original table. It is missing the following items:
Point-In-Time-Recovery
Auto scaling policies
AWS IAM policies
CloudWatch metrics & alarms
Tags
Stream settings
Time to Live
To set the above items to the same as what the original table had we will want to use the Cloud Formation template of the original table!
For more information on restores check out the docs here!
Importing to Cloud Formation
As the table has already been created through the restore, it doesn’t have a Cloud Formation template associated with it yet. However, we can import this resource into a Cloud Formation stack through the following steps:
In the Cloud Formation console in the selected region (in this case Ireland), click Create Stack and in the drop down menu select With existing resources
Once selected you will need to specify a template — for this you can select Upload a template file. When importing resources into a stack, no changes are allowed to the existing resources, so we can’t add any of the tags or extra permissions at this point, we need to add a template that reflects the restored table at this given time! I have created an example Cloud Formation template that matches the exact specifications of the table that is created during a restore and the role associated with it, which can be viewed here, so you can use it and select next!
Now you will need to provide an identifier to map the logical IDs in the template with the existing resources, in this case I used the name of the DynamoDB table and the name of the role:
Then you just have to give the stack a name:
And your presented an overview of the changes that will occur — so in this case the imports required:
Once the import is complete, we can now move onto updating the stack!
For more information on importing existing resources to cloud formation check out the docs here!
Updating the Cloud Formation Template
Now the resources are imported into a stack in Cloud Formation, we can update them to use the same template that the original DynamoDB and attached role had — to do this select the new stack and select Update at the top right hand side:
For this, we just want to Replace current template and give it the template we used to create the original table to make sure everything is kept consistent. The template for my original table can be found here.
You’ll then see the resources that are going to be modified and you can go ahead and select Update Stack at the bottom:
And that’s it! The table has now been migrated from one region to another, with identical settings 🎉
You can check the tags and permissions of the table in the new region and you will see that they have been updated!