Search code examples
google-cloud-platformterraformterraform-provider-gcpgke-networking

Terraform - refering to list object's value as output


I have the following main.tf in my Terraform submodule located in ./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"           },
    ]
  }
}

I would like to expose values of range_name of both secondary ranges as ip_range_pods_gke and ip_range_ser_gke accordingly. So my output.tf files looks like this:

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
}

my main.tf file, located in root folder calling this module.

module "networking"{

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

}

However when I running plan command I'm getting the following error.

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".

What is the correct way to expose range_name for each of secondary_ranges?

Update: My folder structure looks like this (I know that state file should be in bucket but this is just test):

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

Solution

  • 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.

    If this is not the output you want, you have to fork the module, and modify its outputs.