Terraform - 将列表对象值作为输出

Terraform - refering to list object's value as output

我的 Terraform 子模块中有以下 main.tf 位于 ./network

module "test-vpc-module" {
  source       = "terraform-google-modules/network/google"
  version      = "~> 3.3.0"
  project_id   = var.project_id
  network_name = var.network_name
  mtu          = 1460

  subnets = [
     {
      subnet_name           = "xxx-private"
      subnet_ip             = "172.16.3.0/24"
      subnet_region         = "us-central1"
      subnet_private_access = "true"
    },{
      subnet_name           = "us-central1-gke"
      subnet_ip             = "172.16.5.0/24"
      subnet_region         = "us-central1"
      subnet_private_access = "true"
    },  
  ]

  secondary_ranges = {
  us-central1-gke = [
      {
          range_name    = "gke-cluster-pods"
          ip_cidr_range = "10.188.0.0/14"
      },
      {
          range_name    = "gke-cluster-services"
          ip_cidr_range = "10.192.0.0/20"           },
    ]
  }
}

我想将两个次要范围的 range_name 的值相应地公开为 ip_range_pods_gkeip_range_ser_gke。所以我的 output.tf 文件看起来像这样:

output "ip_range_pods_gke" {
  description = "GKE PODs IP ranage."
  value       = module.test-vpc-module.secondary_ranges.us-central1-gke[0].range_name
}

output "ip_range_services_gke" {
  description = "GKE Services IP ranage."
  value       = module.test-vpc-module.secondary_ranges.us-central1-gke[1].range_name
}

我的 main.tf 文件,位于调用此模块的根文件夹中。

module "networking"{

    source = "./network"
    project_id = "xxxxx-project"
    network_name = "xxxxx"

}

然而,当我使用 运行 plan 命令时,出现以下错误。

Error: Unsupported attribute
│
│   on network/outputs.tf line 4, in output "ip_range_pods_gke":
│    4:   value       = module.test-vpc-module.secondary_ranges.us-central1-gke[0].range_name
│     ├────────────────
│     │ module.test-vpc-module is a object, known only after apply
│
│ This object does not have an attribute named "secondary_ranges".

为每个 secondary_ranges 公开 range_name 的正确方法是什么?

更新: 我的文件夹结构如下所示(我知道状态文件应该在存储桶中,但这只是测试):

├── main.tf
├── network
│   ├── main.tf
│   ├── outputs.tf
│   └── variables.tf
├── terraform.tfstate
└── terraform.tfstate.backup

terraform-google-modules/network/google does not have outputs such as secondary_ranges. Instead, that output that you may be after is called subnets_secondary_ranges.

如果这不是您想要的输出,您必须分叉模块并修改其输出。