过滤 terraform 列表中的特定值

Ham*_*med 3 terraform terraform-provider-gcp terraform0.12+

我有一个 terraform 代码块,可以生成 gcp 区域列表

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

output "name" {
  value = data.google_compute_regions.available.names
}
Run Code Online (Sandbox Code Playgroud)
  ~ 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",
    ]
Run Code Online (Sandbox Code Playgroud)

但是,我只想过滤掉欧洲地区。

这样做

output "names" {
  value = [for s in data.google_compute_regions.available.names : s if s == "europe-west1"]
}
Run Code Online (Sandbox Code Playgroud)

只给了我一个区域,这不是我想要的。

+ names = [
    + "europe-west1",
  ]
Run Code Online (Sandbox Code Playgroud)

我不确定是否支持通配符或如何编写它的逻辑。

我已经尝试使用此处描述的文件管理器,但我似乎没有得到正确的结果。

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

  filter {
    name   = "name"
    values = ["europe-*"]
  }
}
Run Code Online (Sandbox Code Playgroud)

但我遇到了错误 Blocks of type "filter" are not expected here.

Mar*_*k B 10

由于某种原因,该数据源似乎不支持过滤器,因此您需要在输出中执行正则表达式,如下所示:

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