How-to setup a HA/DR database in AWS? [5 - DR database]
Tags: devops, sre, aws, terraform
Published: 2022-01-18 17:40:03

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
```terraform
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"
}
```

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.

```terraform
global_cluster_identifier = aws_rds_global_cluster.example.id
```

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.

```terraform
depends_on = [
    aws_rds_global_cluster.example,
    aws_rds_cluster.default
  ]
```

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
- Terraform documentation : https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/rds_global_cluster
- AWS failover global database documentation : https://aws.amazon.com/blogs/database/managed-planned-failovers-with-amazon-aurora-global-database/

---

I hope it will help you! 🍺

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


---

## Serie link
- **1 - Start** : https://dev.to/adaendra/how-to-setup-a-hadr-database-in-aws-1-1ko7
- **2 - Definitions** : https://dev.to/adaendra/how-to-setup-a-hadr-database-in-aws-2-definitions-93p
- **3 - Simple database** : https://dev.to/adaendra/how-to-setup-a-hadr-database-in-aws-3-simple-database-a9o
- **4 - HA Database** : https://dev.to/adaendra/how-to-setup-a-hadr-database-in-aws-4-ha-database-4kek
- **5 - DR database** : https://dev.to/adaendra/how-to-setup-a-hadr-database-in-aws-5-dr-database-278b
- **6 - Create from snapshot** : https://dev.to/adaendra/how-to-setup-a-hadr-database-in-aws-6-create-from-snapshot-2mbf
- **7 - Dynamic Terraform backend definition** : https://dev.to/adaendra/how-to-setup-a-hadr-database-in-aws-7-dynamic-terraform-backend-definition-3aga
- **8 - Multiple instances in multiple regions** : https://dev.to/adaendra/how-to-setup-a-hadr-database-in-aws-8-multiple-instances-in-multiple-regions-210d
- **9 - Generate a random value** : https://dev.to/adaendra/how-to-setup-a-hadr-database-in-aws-9-generate-a-random-value-5g8a

---

<center><h3>You want to support me?</h3></center>
<a href="https://www.buymeacoffee.com/mxguilbert7" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/default-orange.png" alt="Buy Me A Coffee" height="41" width="174"></a>