1

我想在模块块中使用计数,但由于不支持我试图用 for 循环编写 for_each 但它给出了"for_each" argument value is unsuitable错误。

我无法将计数传递给内部模块,因为它会弄乱我的输出格式。有人可以指导我如何正确调用 for_each。

主文件

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
}

上午/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
}
4

1 回答 1

1

有几种方法可以做到这一点。一种方法是:

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

另一种是:

for_each = toset([for id in range(2): tostring(id)])
于 2021-02-05T11:28:10.230 回答