terraform 模块块中的 for_each 返回 - “for_each”参数值不合适

问题描述

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

我无法将 count 传递给内部模块,因为它会弄乱我的输出格式。有人可以指导我如何正确调用 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
}

解决方法

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

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

另一个是:

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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...