How-to setup a HA/DR database in AWS? [6 - Create from snapshot]
Tags: terraform, sre, aws, devops
Published: 2022-01-18 17:40:11

In this post we will see how to create an AWS RDS Global database from a snapshot.

---

## Why create a database from a snapshot if we already have the DR?
For sure, if you have a DR system, you might be have to create your global database from a snapshot every day *(I hope for you so)*.

But when you are delivering a huge update on your database, you maybe want to have something to go back to the last state before, or if you are migrating data from a database to another.

---

## How to create a global database from a snapshot ?
If you have checked the documentation, you have seen a parameter called **snapshot_identifier** in the **aws_rds_cluster** definition.

If you define a value here and run your script, you will have all the elements created but the cluster with the snapshot won't be linked to the global database.

In the solution presented in the last post, we did :
1 - the creation of the global cluster
2 - then we create the principal cluster
3 - and then we create all the other clusters

But to make it work, we need to change this order and the links.

We need to create first the principal cluster from the snapshot, without links to the global database.

*This one*
```
global_cluster_identifier = aws_rds_global_cluster.example.id
```

Then, we have to create the global cluster. But this time, we will say to the global cluster that it needs to be based on the primary cluster.
```terraform
resource "aws_rds_global_cluster" "example" {
  global_cluster_identifier = "global-test"

  source_db_cluster_identifier = aws_rds_cluster.default.arn
  force_destroy = true
}
```

In this global cluster definition, we can see that a lot of parameters desapeared *(like engine, database name...)* and it's because the global cluster will take the configurations of the primary cluster!

> Note : If you are creating a cluster from a snapshot, the **master_username** won't be override by the one you defined in your script, it will keep the one defined in the snapshot, but **master_username** yes.

---

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>