【问题标题】:How to define a list of output when using list and for_each in terraform在terraform中使用list和for_each时如何定义输出列表
【发布时间】:2020-06-28 05:31:51
【问题描述】:

我正在尝试使用 terraform 部署 BigQuery 计划查询,但我不知道如何使用 list 和 for_each 语法。

我有这个模板文件。

SELECT *
FROM mydataset.mytable_${suffix}

然后使用 template_file 模块生成查询列表。 var.suffix_list 这里是一个字符串列表。

data "template_file" "query" {
  for_each = var.suffix_list # list(string)
  template = "file/path/to/template/file"
  vars = {
    suffix    = each.value
  }
}

我想从这个模块生成一个查询列表。

output "query" {
  value = # a list of rendered query
}

此输出将是google_bigquery_data_transfer_config 资源的输入。

resource "google_bigquery_data_transfer_config" "query_config" {
  for_each                 = # a list of rendered query
  data_refresh_window_days = var.data_refresh_window_days
  data_source_id           = "scheduled_query"
  display_name             = var.display_name
  disabled                 = var.disabled
  destination_dataset_id   = var.destination_dataset_id
  location                 = var.query_location
  project                  = var.gcp_project
  schedule                 = var.query_schedule
  params = {
    destination_table_name_template = var.destination_table_name_template
    query                           = each.value
  }
}

如何输出google_bigquery_data_transfer_config 的定义和输入? 提前致谢。

【问题讨论】:

    标签: terraform terraform-provider-gcp


    【解决方案1】:

    要完成这项工作,需要做一些事情。

    for_each 的参数应该是一个集合,所以你需要使用

      for_each = toset(var.suffix_list)
    

    在您的模板本身中,变量被命名为suffix,但是,您正在传递变量schema。这些应该匹配。

    template 参数实际上需要是模板的文本。因此,您需要使用file 函数读取文件,例如

      template = file("path/to/template/file")
    

    然后,最后,要编写您的实际输出,您可以使用列表推导

    output "query" {
      value = [for query in data.template_file.query : query.rendered]
    }
    

    【讨论】:

    • 您能否告诉我如何使用资源“google_bigquery_data_transfer_config”中的输出“查询”?
    猜你喜欢
    • 2021-07-09
    • 2023-02-25
    • 2021-03-31
    • 2020-06-23
    • 1970-01-01
    • 2021-03-07
    • 2021-01-18
    • 2021-05-12
    • 1970-01-01
    相关资源
    最近更新 更多