This Blog has been moved from Medium to blogs.tensult.com. All the latest content will be available there. Subscribe to our newsletter to stay updated.

This blog is to discuss the implementation of Cross-region and Cross account RDS MySQL DB replication. If you are new to the topic then it would be good to refer this blog to get a basic idea about it. Replication is mainly done so as to protect your data in case of any failures.

The below image shows the architecture to be implemented for Cross-region and Cross account replication.

The source and the target are kept in separate VPCs in different AWS regions. The target is kept inside the private subnets as it is the best practice to do so. The target and source are connected by VPC peering connection so that data can be replicated.

Process

Define a new VPC for the target in a region different from where the source is kept at. In this example we have chosen the region as Singapore (ap-southeast-1).

Creating VPC

2. Create a total of three subnets inside the VPC. One subnet is for the DMS replication instance. The other two are to form a database subnet group and so they have to be in different availability zones. All these subnets are private and so you don’t need to attach an Internet Gateway (IGW).

3. Create one route table for the subnets and associate the subnets with the route table.

4. Now you can add a database to the VPC. First create the subnet group for the database by selecting Subnet Groups options in RDS service and then selecting the appropriate subnets to be added.

5. Now create the RDS DB instance by clicking Launch Instances from Instances option. Select the engine you want and follow the setup process to launch a DB instance. Please make a note of the username and password you have assigned to your DB.

6. Now that you have created the DB instance, you can move on to set up the data replication instance. Click on Get Started tab in the Data Migration Services. You will be redirected to a page asking for the details of the replication instance.

Creating replication instance

When you scroll down you will see the Advanced and Maintenance settings which you can set as per your needs. Click next after entering the details.

Advanced and Maintenance settings

7. The next step is to connect the source and the target endpoints. Enter the fields with the required details. After entering the details you can test to see if the connection works (Note: you can test only if you have set up the VPC Peering connection prior to this step).

Connecting source and target endpoints

8. The next step in setting up the Data Migration Service is to set up a task. Fill in the required fields with your requirements. After filling in all the required fields, click Create Task to finish setting up the Data Migration Service.

Setting up a task

9. The next step is to connect the source and target VPCs by VPC Peering connection. Select the VPC service and choose Create Peering Connection from Peering Connections tab. Fill in the required details and click Create Peering Connection.

Select Create Peering Connection

Create Peering Connection

10. After setting the VPC connection you have to accept the VPC Peering connection from the account you have send the request to. Choose Peering Connections from VPC and you’ll be able to see the request, which you should accept.

11. After the VPC Peering Connection has been made, you need to edit the route table of the source and target VPCs. In each, you have to add the route with the CIDR block of the other VPC as the Destination and the VPC Peering Connection you just created as the Target and save it.

Adding routes in route table of source and target VPC

12. The last step you have to do is to add a rule in the security group of the source and target. For the source instance security group add a custom TCP rule for the port of the database (3306 by default) with the source as the CIDR block of the target VPC. In the target, edit the security group of the database to add a customer TCP rule for the port of the database with source as the security group of the DMS replication instance. The security group of the target VPC should allow all the traffic for all port ranges and all protocols as shown below.

Security group of source VPC

Security group of RDS in target VPC

Security group of target VPC

The above steps show you how to set up a Data Migration Service for a MySQL RDS Database. Depending on how you have configured it, the Data Migration Service will replicate the data once onto the target database or will keep replicating as you make changes to the source database. This is a powerful tool for disaster recovery as it can recover data that is lost in many scenarios like hardware failure, AWS Region failure, accidental deletion etc except when the data is deleted by a bug in the code.