如何在 Terraform 代码中引用次要范围?

How do I reference the secondary ranges in Terraform code?

我正在尝试引用另一个资源中的次要范围:

resource "google_compute_subnetwork" "mysubnetwork" {
  name          = "mysubnetwork"
  ....
  secondary_ip_range {
    range_name    = "mysecondary"
    ip_cidr_range = "10.0.0.0/24"
  }
  ....

resource "google_container_cluster" "my-cluster" {
....
ip_allocation_policy{
    cluster_secondary_range_name = <reference my existing secondary ranges>
    services_secondary_range_name = <reference my existing secondary ranges>
}

试过这个:google_compute_subnetwork.mysubnetwork.secondary_ip_range.range_name["mysecondary"]

但得到了This value does not have any attributes.

我试过这个:google_compute_subnetwork.mysubnetwork.secondary_ip_range.range_name.mysecondary

但得到了同样的错误。

这很复杂,我还没有测试过,但看起来它会起作用 - 我会 运行 解决这个问题吗?想知道有没有更好的方法。为清楚起见,引用的范围应该很明显(因此不使用索引号)

variable "vpc_secondary_ip_ranges" {
  type = map(object({
    secondary_range = object({
      range_name = string
      ip_cidr_range = string
    })
  }))
  default = {
    gke-pods = {
      secondary_range = {
        range_name = "gke-pods"
        ip_cidr_range = "10.1.0.0/14"
      }
    }
    gke-services = {
      secondary_range = {
        range_name = "gke-services"
        ip_cidr_range = "10.2.2.0/24"
      }
    }
  }
}

// flatten for secondary_ip_range parameter
resource "google_compute_subnetwork" "bombora-default-subnetwork-us-west1" {
  secondary_ip_range = [for r in var.vpc_secondary_ip_ranges : r.secondary_range]
  ...

...
resource "google_container_cluster" "mycluster" {
  // index into map for names of secondaries for ip_allocation_policy
  ip_allocation_policy {
    cluster_secondary_range_name = var.vpc_secondary_ip_ranges.gke-pods.secondary_range.range_name
    services_secondary_range_name = var.vpc_secondary_ip_ranges.gke-services.secondary_range.range_name
  }
....