Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
342 views
in Technique[技术] by (71.8m points)

azure - 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
}
question from:https://stackoverflow.com/questions/66062321/for-each-in-terraform-module-block-returns-for-each-argument-value-is-unsuit

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...