Terraform modules source with a condition

218 Views Asked by At

I have a question about the terraform modules. Is it possible to have a condition set in the source while using terraform modules?

example:

module "infra_deployment" {
  source = (local.region == "SFO" ? ./modules/SFO_repo : ./modules/standard_repo)

  something here
}
2

There are 2 best solutions below

1
alex On

With pure Terraform this is not possible but can do this with Terragrunt by using generate to conditionally generate the module definition:

https://terragrunt.gruntwork.io/docs/reference/config-blocks-and-attributes/#generate

0
Mark B On

Using pure Terraform, I would define two modules, and use count to determine which one gets used:

module "infra_deployment_standard" {
  count  = local.region == "SFO" ? 0 : 1
  source = "./modules/standard_repo"

  something here
}

module "infra_deployment_sfo" {
  count  = local.region == "SFO" ? 1 : 0
  source = "./modules/SFO_repo"

  something here
}