Dynamo DB Cross Region Migration
Published: 2020-10-24 10:05:35


![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/n8u6lkqjp9q47a0ztgm5.png)

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:
1. Create a backup from the Point-in-Time Recovery
2. Restore the backup to a new region
3. Import the newly restored table into Cloud Formation
4. 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.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/bl9mrhrh33nnx5z9v58x.png)

This will then prompt you for the name of the table you wish to backup and also for a name for the backup itself:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/3mrmpu9mdtbclhni2bxv.png)

Once the backup has been created you will see it listed on the console and you are able to select it:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/h7rtpyrpw41olf35ka4t.png)

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](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Backup.Tutorial.html)!  
 
## 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:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/g9slwxnx09gdmqiwug15.png)

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:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/9ndbyhvgi60rdexqw0h5.png)

You are presented with an overview of the restore and then you can simply hit `Restore` at the bottom!

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/i1k0igmtt4oh43sp37ld.png)

And now if you go to the region you selected, you will be able to see that the table is being restored:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/huusr270cdth223oimry.png)

Once it is restored, the tables status will be set to `Active`:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/gszlbva1kms0pwnvt1yi.png)

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](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Restore.Tutorial.html)!  
 

## 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`

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/94nb1ltvdxmrykxli8x9.png)

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](https://github.com/chloeMcAteer/blog-resources/blob/main/restoreTable.yaml), so you can use it and select next!

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/6aazd29c8qky4zi7qrnv.png)

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:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/37tvzdeirntd3w1gjmd9.png)

Then you just have to give the stack a name:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/2wku3qcvl04z61rf2ijx.png)

And your presented an overview of the changes that will occur — so in this case the imports required:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/3aw2a51f94qftuzkafu4.png)

Once the import is complete, we can now move onto updating the stack!

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/yl66hqv41i7wbdgeonab.png)  

> 💡 For more information on importing existing resources to cloud formation check out the docs [here](https://aws.amazon.com/blogs/aws/new-import-existing-resources-into-a-cloudformation-stack/)!  
 

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/3p7yljyruswadmjseamo.png)

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](https://github.com/chloeMcAteer/blog-resources/blob/main/testBlogTable.yaml).

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/nz8iswetxm75fgxds5bs.png)

You’ll then see the resources that are going to be modified and you can go ahead and select `Update Stack` at the bottom:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/t4cd33cibrdpzzmp5xzg.png)

***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!