DEV Community

Cover image for How-to setup a HA/DR database in AWS? [8 - Multiple instances in multiple regions]
Maxime Guilbert
Maxime Guilbert

Posted on • Updated on

How-to setup a HA/DR database in AWS? [8 - Multiple instances in multiple regions]

In this part of the serie, we will see how to create in one Terraform script and in a single execution, how to create elements in multiple regions.

This kind of tips is really helpful if you want to create at once all your infra and/or if you want to setup some disaster recovery.


How to do it?

To do it, it's pretty simple. We have 2 steps to follow :

  • declare multiple providers
  • declare which provider we want to use for each resource

Declare multiple providers

In your current script to create something on AWS, you should have an AWS Provider with the region where it should be created.

So, copy/paste this block for all your regions. Then add an alias to each of them to be able to differentiate them.

provider "aws" {
  alias  = "frankfurt"
  region = "eu-central-1"
}

provider "aws" {
  alias  = "sydney"
  region = "ap-southeast-2"
}
Enter fullscreen mode Exit fullscreen mode

Declare which provider we want to use for each resource

In each resource you have, add the provider parameter to link each of them to the correct provider.

resource "aws_rds_cluster_instance" "test_frankfurt" {
  provider = aws.frankfurt
  ...
}

resource "aws_rds_cluster_instance" "test_sydney" {
  provider = aws.sydney
  ...
}
Enter fullscreen mode Exit fullscreen mode

And that's it! You are now able to deploy your complete infra on multiple regions at once!

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)