简体   繁体   中英

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
}

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)])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM