glux
glux

Reputation: 532

Terraform - Extract a value based on text from output

I have the following module that creates subnets on GCP:

/******************************************
    Subnet configuration
 *****************************************/
resource "google_compute_subnetwork" "subnetwork" {
  count = "${length(var.subnets)}"

  name                     = "${lookup(var.subnets[count.index], "subnet_name")}"
  ip_cidr_range            = "${lookup(var.subnets[count.index], "subnet_ip")}"
  region                   = "${lookup(var.subnets[count.index], "subnet_region")}"
  private_ip_google_access = "${lookup(var.subnets[count.index], "subnet_private_access", "false")}"
  enable_flow_logs         = "${lookup(var.subnets[count.index], "subnet_flow_logs", "false")}"
  network                  = "${google_compute_network.network.name}"
  project                  = "${var.project_id}"

  secondary_ip_range = "${var.secondary_ranges[lookup(var.subnets[count.index], "subnet_name")]}"
}

data "google_compute_subnetwork" "created_subnets" {
  count = "${length(var.subnets)}"

  name    = "${element(google_compute_subnetwork.subnetwork.*.name, count.index)}"
  region  = "${element(google_compute_subnetwork.subnetwork.*.region, count.index)}"
  project = "${var.project_id}"
}

My output looks like this:

output "subnets_self_links" {
  value       = "${google_compute_subnetwork.subnetwork.*.self_link}"
  description = "The self-links of subnets being created"
}

This output produces a list of subnets.

I need to be able to extract the following by searching for the subnet name. In this case it is "subnet-01":

subnetwork = "https://www.googleapis.com/compute/v1/projects/abc-network-hub/regions/us-central1/subnetworks/subnet-01"

How can I construct my lookup to search by text?

subnetwork = "${module.test-vpc.subnets_self_links}"

Above returns:

"module.compute-o057qdb2-l30.var.subnetwork: variable subnetwork in module compute-o057qdb2-l30 should be type string, got list"

subnetwork = "${lookup(module.test-vpc.subnets_self_links, "subnet-01, 0")}"

Above returns:

  • module.compute-o057qdb2-l30.var.subnetwork: At column 3, line 1: lookup: argument 1 should be type map, got type list in:

${lookup(module.test-vpc.subnets_self_links, "subnet-01, 0")}

subnetwork = "${module.test-vpc.subnets_self_links[0]}"

Above works because there is only one subnet created and I can reference by the list index. I need to be able to search by subnet name. I feel like I should be able to extract the values from "data".

This is the module I am using: https://github.com/terraform-google-modules/terraform-google-network

Upvotes: 2

Views: 1011

Answers (1)

Robert Jordan
Robert Jordan

Reputation: 1103

Terraform 0.12 makes this easy:

variable "subnets" {
    default = [
        {
            name = "subnet-01"
            id = 1
        },
        {
            name = "subnet-02"
            id = 2
        }
    ]
}

output "subnet-one" {
    value = [
        for subnet in var.subnets:
            subnet.id if subnet.name == "subnet-01"
    ][0]
}

Prior to HCL 2 this kind of thing is faily cumbersome.

Upvotes: 1

Related Questions