terraformterraform-provider-gcpterraform0.12+

Filter specific values in a list


I've got a block of terraform code that produces a list of gcp regions

data "google_compute_regions" "available" {
  project = var.project
}

output "name" {
  value = data.google_compute_regions.available.names
}
  ~ name = [
      + "asia-east1",
      + "asia-east2",
      + "asia-northeast1",
      + "asia-northeast2",
      + "asia-northeast3",
      + "asia-south1",
      + "asia-southeast1",
      + "asia-southeast2",
      + "australia-southeast1",
      + "europe-north1",
      + "europe-west1",
      + "europe-west2",
      + "europe-west3",
      + "europe-west4",
      + "europe-west6",
      + "northamerica-northeast1",
      + "southamerica-east1",
      + "us-central1",
      + "us-east1",
      + "us-east4",
      + "us-west1",
      + "us-west2",
      + "us-west3",
      + "us-west4",
    ]

However, I want to filter out only the regions in europe.

Doing this

output "names" {
  value = [for s in data.google_compute_regions.available.names : s if s == "europe-west1"]
}

only gives me one region, which is not what I want.

+ names = [
    + "europe-west1",
  ]

I'm not sure if wildcards are supported or how to write the logic for it.

I've tried using filers as described here but I don't seem to be getting it right.

data "google_compute_regions" "available" {
  project = var.project

  filter {
    name   = "name"
    values = ["europe-*"]
  }
}

But I run into the error Blocks of type "filter" are not expected here.


Solution

  • Since it looks like filter is not supported on that data source for some reason, you will need to do a regex in the output, like this:

    output "names" {
      value = [for s in data.google_compute_regions.available.names : s if length(regexall("europe.*", s)) > 0]
    }