无法使用for_each遍历列表

时间:2020-03-17 15:50:55

标签: terraform terraform-provider-gcp

我想保留一个IP,然后再使用它。如果我为每个IP创建一个单独的google_compute_address块,则效果很好。但是由于我想使代码尽可能干燥和优化,所以我正在学习如何循环和使用for_each

我的 main.tf 看起来像这样

module "nat" {
  source  = "../../modules/nat"

  reserved_ips = [
    {
      name = "gke-frontend-prod-lb"
      ip = "10.238.232.10"
    },
    {
      name = "gke-frontend-test-lb"
      ip = "10.238.232.11"
    }
  ]
} 

如您所见,我想形成一个具有名称和IP的保留IP的列表。

现在让我们看看我的模块

我的 variables.tf 外观

variable "reserved_ips" {
  type        = list(object({
    name = string
    ip = string
  }))
  description = <<EOF
Reserved IPs.
EOF
}

模块的 main.tf 如下

locals {
  ips = {
    # for_each needs transform to map
    for ip in var.reserved_ips : "${ip.name}" => "${ip.ip}"
  }
}


resource "google_compute_address" "gke-frontend" {
  for_each = local.ips

  name         = "${each.value.name}"
  subnetwork   = "mysubnet"
  address_type = "INTERNAL"
  address      = "${each.value.ip}"
}

但是运行代码可以给我

Error: Unsupported attribute

  on ../../modules/nat/main.tf line 11, in resource "google_compute_address" "gke-frontend":
  11:   name         = "${each.value.name}"
    |----------------
    | each.value is "10.238.232.10"

This value does not have any attributes.


Error: Unsupported attribute

  on ../../modules/nat/main.tf line 11, in resource "google_compute_address" "gke-frontend":
  11:   name         = "${each.value.name}"
    |----------------
    | each.value is "10.238.232.11"

This value does not have any attributes.


Error: Unsupported attribute

  on ../../modules/nat/main.tf line 14, in resource "google_compute_address" "gke-frontend":
  14:   address      = "${each.value.ip}"
    |----------------
    | each.value is "10.238.232.10"

This value does not have any attributes.


Error: Unsupported attribute

  on ../../modules/nat/main.tf line 14, in resource "google_compute_address" "gke-frontend":
  14:   address      = "${each.value.ip}"
    |----------------
    | each.value is "10.238.232.11"

This value does not have any attributes.

我很困惑我到底在这里想念什么。

1 个答案:

答案 0 :(得分:2)

问题是您的ips当地人将列表转换为map(string)(即具有字符串值的映射)

locals {
  ips = {
    # for_each needs transform to map
    for ip in var.reserved_ips : "${ip.name}" => "${ip.ip}"
  }
}

请注意,在=>的右侧,您有"${ip.ip}"

for_eachmap上循环时,它将each.key分配给每个键(一个string),并将each.value分配给映射中的每个对应值(在这种情况下"${ip.ip}也是string)。

所以,我认为在这种情况下,您想要的是以下内容

  # ...
  name         = each.key
  # ...
  address      = each.value
  # ...