1
votes

I had a main.tf custom TF module, the module will deploy a lists of another custom modules. I can deploy single region by passing single value into main.tf, but when I tried multi-region which passing an array of regions and loop in main.tf's custom module. It doesn't work.

main.tf

locals {
  regions = ["asia-east2", "asia-southeast1"]
}

module "main" {
  source = "./modules/main"
  for_each = local.regions
  region = each.value
}

Error:

The name "for_each" is reserved for use in a future version of Terraform.`

Notes

I know Terraform 0.12 doesn't support this feature. Is there a way to loop through multi-region without passing down into child module's resource level ?

1
The error implies you are not on one of the versions of Terraform that supports iteration in this way. - Matt Schuchard
You are correct, Terraform (even v0.12) doesn't support the for_each attribute for Modules. So what you are asking to do is not possible at the moment. - GreenyMcDuff

1 Answers

0
votes

I know Terraform 0.12 doesn't support this feature. Is there a way to loop through multi-region without passing down into child module's resource level ?

No. There is no way to loop through local.regions and pass them into modules in Terraform 0.12 because count and for_each for modules were added in Terraform 0.13.

Your only option is to make multiple references.

locals {
  regions = ["asia-east2", "asia-southeast1"]
}

module "main_0" {
  source = "./modules/main"
  region = local.regions[0]
}

module "main_1" {
  source = "./modules/main"
  region = local.regions[1]
}