DEV Community

Cover image for How-to setup a HA/DR database in AWS? [5 - DR database]
Maxime Guilbert
Maxime Guilbert

Posted on • Updated on

How-to setup a HA/DR database in AWS? [5 - DR database]

In this part of the serie, we will see how to (finally) create a database with a system of disaster recovery.


Create a global database

Now that we have a cluster of databases, we can easily create multiple of them in multiple regions and link them to a Global Database.

So at least, we need to create 2 clusters in 2 regions and we will create an aws_rds_global_cluster.

Definition of aws_rds_global_cluster

resource "aws_rds_global_cluster" "example" {
  global_cluster_identifier = "global-test"
  engine                    = "aurora"
  engine_version            = "5.6.mysql_aurora.1.22.2"
  database_name             = "example_db"
}
Enter fullscreen mode Exit fullscreen mode

In this example, we can see that some common parameters are defined here as the engine, its version or the db name to keep an uniformity among all the databases.


Updates in the clusters definitions

Due to the fact that you will use a global database, you have to link your clusters to the global one.

global_cluster_identifier = aws_rds_global_cluster.example.id
Enter fullscreen mode Exit fullscreen mode

Then, on all the secondary cluster, you can add depends_on to be sure that they will be created after the global one and the primary one.

depends_on = [
    aws_rds_global_cluster.example,
    aws_rds_cluster.default
  ]
Enter fullscreen mode Exit fullscreen mode

Also, some parameters are not required anymore (because they are now defined in the global database) like :

  • master_username
  • master_password
  • database_name

Then, if you wanted to created all the elements in a single Terraform script, you will have to declare multiple providers for each region where you want to create a cluster. (The complete point will be explain in a following post)


Using the Global cluster

Access to the database

The global cluster don't have a specific endpoint to expose the database with Read/Write rights and another for all the read-only databases.

Each cluster will create its own endpoints.

For the primary cluster, both endpoints will work well.

For the other cluster, only the read-only endpoint will work.

It's normal, it's to be sure to have only one main entry point and replicate the update on all the other databases.

But a region is not accessible anymore or if you manually do a failover to change the primary region, the read/write endpoint of the first region will be disabled and the one of the new primary region will be enabled.

How to do manually a failover?

In the AWS web console, select your global database and in the actions you will have Fail over global database. Click on it and the switch will go on!

Links


I hope it will help you! 🍺

And see you soon for the next part of this serie. 😀


Serie link


You want to support me?

Buy Me A Coffee

Top comments (0)