替代方法,因为 for_each 和 count 不能在同一资源中

alternative way, since for_each and count cannot be in same resource

我的配置需要同时使用count和for_each。 这是配置块

variable "all_zone" {
  type    = list(any)
  default = ["asia-southeast1-a", "asia-southeast1-b", "asia-southeast1-c"]
}

variable "backends" {
  description = "Map backend indices to list of backend maps."
  type = map(object({
    neg_name  = string
   }))
}

data "google_compute_network_endpoint_group" "get_neg" {
  for_each = var.backends
  count   = length(var.all_zone)
  zone    = var.all_zone[count.index]
  name    = lookup(each.value, "neg_name")
  
}

resource "google_compute_backend_service" "default" {
. . .
dynamic "backend" {
    for_each = [for b in data.google_compute_network_endpoint_group.get_neg[*].id : b]
    content {
      group = backend.value
    }
  }
}

有没有办法做到这一点?

更新:这里是示例 var.backends

backends = {
    default = {
        neg_name = 'name-1'
    }
}

更新:感谢@marcin 提供的解决方案。但我在访问这些数据时遇到了另一个问题。

在我使用之前helper_map


data "google_compute_network_endpoint_group" "get_neg" {
  count   = length(var.all_zone)
  zone    = var.all_zone[count.index]
  name    = 'name-1'
  
}

此处输出数据的结构:

neg = [
      + {
          + id                    = "projects/k8s-playground-public/zones/asia-southeast1-a/networkEndpointGroups/name-1"
          + name                  = "name-1"
          + zone                  = "https://www.googleapis.com/compute/v1/projects/k8s-playground-public/zones/asia-southeast1-a"
        },
      + {
          + id                    = "projects/k8s-playground-public/zones/asia-southeast1-b/networkEndpointGroups/name-1"
          + name                  = "name-1"
          + zone                  = "https://www.googleapis.com/compute/v1/projects/k8s-playground-public/zones/asia-southeast1-b"
        },
      + {
          + id                    = "projects/k8s-playground-public/zones/asia-southeast1-c/networkEndpointGroups/name-1"
          + name                  = "name-1"
          + zone                  = "https://www.googleapis.com/compute/v1/projects/k8s-playground-public/zones/asia-southeast1-c"
        },
    ]

这是我访问数据

for_each = [for b in data.google_compute_network_endpoint_group.get_neg[*].id : b]

使用后map_helper

neg = [
      + {
          + default-asia-southeast1-a  = {
              + id                    = "projects/k8s-playground-public/zones/asia-southeast1-a/networkEndpointGroups/k8s1-e051d246-default-gclb-poc-8080-ef51ff1c"
              + name                  = "name-1"
              + zone                  = "https://www.googleapis.com/compute/v1/projects/k8s-playground-public/zones/asia-southeast1-a"
            }
          + default-asia-southeast1-b  = {
              + id                    = "projects/k8s-playground-public/zones/asia-southeast1-b/networkEndpointGroups/name-1"
              + name                  = "name-1"
              + zone                  = "https://www.googleapis.com/compute/v1/projects/k8s-playground-public/zones/asia-southeast1-b"
            }
          + default-asia-southeast1-c  = {
              + id                    = "projects/k8s-playground-public/zones/asia-southeast1-c/networkEndpointGroups/name-1"
              + name                  = "name-1"
              + zone                  = "https://www.googleapis.com/compute/v1/projects/k8s-playground-public/zones/asia-southeast1-c"
            }
        },
    ]

以及我如何访问这些 id 数据。

我不确定你到底想用你的数据结构实现什么,但解决你的问题的一种方法是创建本地帮助变量,这将是backendsall_zone 的组合。例如:


variable "backends" {

  description = "Map backend indices to list of backend maps."
  
  type = map(object({
    neg_name  = string
   }))
   
   default = {
        default = {
          neg_name = "name-1"
        }
  }
}


variable "all_zone" {
  type    = list(any)
  default = ["asia-southeast1-a", "asia-southeast1-b", "asia-southeast1-c"]
}

locals {

  helper_map = merge([
          for backend_key, backend_value in var.backends:
            {
              for zone in var.all_zone: 
              "${backend_key}-${zone}" => {
                  backend_value = backend_value.neg_name
                  zone = zone
              }
            }
        ]...)


}

给出:

{
  "default-asia-southeast1-a" = {
    "backend_value" = "name-1"
    "zone" = "asia-southeast1-a"
  }
  "default-asia-southeast1-b" = {
    "backend_value" = "name-1"
    "zone" = "asia-southeast1-b"
  }
  "default-asia-southeast1-c" = {
    "backend_value" = "name-1"
    "zone" = "asia-southeast1-c"
  }
}

然后,您可以轻松迭代此连接结构(仅示例):

data "google_compute_network_endpoint_group" "get_neg" {
  for_each = local.helper_map
  zone    = each.value.zone
  name    = each.value.backend_value
  
}

以上内容可能需要进一步调整以匹配您的数据结构,但使用局部辅助变量的总体思路保持不变。