Search code examples
azureterraformterraform-provider-azureazure-rmterraform-modules

for_each in terraform module block returns - "for_each" argument value is unsuitable


I want to use count in module block but since it is not supported im trying to write for_each with a for loop but it is giving "for_each" argument value is unsuitable error.

I can't pass count to the inner module because it will mess my output format. can someone guide me how to properly call for_each.

main.tf

module "test" {
  for_each = toset([for id in range(2): {
      index = id
    }])

  source = "./am"
  name = each.value
}

output "all" {
  depends_on = [ module.test ]
  value = module.one
}

am/test.tf

variable "name" {
  type = string
}

resource "azurerm_public_ip" "ip" {
  name                = ..
  resource_group_name = ..
  location            = ..
  allocation_method   = ..
}

output "one" {
  description = "one_value"
  value = azurerm_public_ip.ip.ip_address
}

Solution

  • There are few ways to do this. One way is:

    for_each = {for id in range(2): id=>id}
    

    other one is:

    for_each = toset([for id in range(2): tostring(id)])