How to create a Terraform with if, else, elsif statement?

ConscriptMR picture ConscriptMR · Apr 7, 2019 · Viewed 36.3k times · Source

I'm setting up a terraform module to create an aurora cluster.
I need to have an option for cross region replication so i need to decide the region of the replica in relation to the source region.
Is there any way to do a multiple option conditional in terraform?

Answer

victor m picture victor m · Apr 7, 2019

This is one way using the coalesce() function:

locals{
  prod = "${var.environment == "PROD" ? "east" : ""}"
  prod2 = "${var.environment == "PROD2" ? "west2" : ""}"
  nonprod = "${var.environment != "PROD" && var.environment != "PROD2" ? "west" : ""}"
  region = "${coalesce(local.prod,local.prod2, local.nonprod)}"
}