gpt4 book ai didi

google-cloud-platform - Terraform - 遍历文件夹以创建单个资源

转载 作者:行者123 更新时间:2023-12-05 09:32:10 24 4
gpt4 key购买 nike

我有一个文件夹queries,用户可以在其中添加、删除和修改yaml 文件。每个 yaml 文件代表 GCP 上的一个 terraform 资源,一个调度查询

循环查询文件夹并相应地在主 main.tf 中生成适当数量的 terraform 资源的最干净的方法是什么?如果更容易的话,我可以使用 Python 生成 main.tf

1 signel 资源示例:

queries/alpha.yaml

display_name: "my-query"
data_source_id: "scheduled_query"
schedule: "first sunday of quarter 00:00"
destination_dataset_id: "results"
destination_table_name_template: "my_table"
write_disposition: "WRITE_APPEND"
query: "SELECT name FROM tabl WHERE x = 'y'"

这应该在我的 ma​​in.tf

中创建此资源
resource "google_bigquery_data_transfer_config" "query_config" {
display_name = "my-query"
data_source_id = "scheduled_query"
schedule = "first sunday of quarter 00:00"
destination_dataset_id = "results"
params = {
destination_table_name_template = "my_table"
write_disposition = "WRITE_APPEND"
query = "SELECT name FROM tabl WHERE x = 'y'"
}
}

最佳答案

你可以读取你的locals中的所有文件:

locals {

query_files = fileset(path.module, "queries/*.yaml")

queries = {for query_file in local.query_files:
query_file => yamldecode(file(query_file))}
}

然后使用for_each 来创建您的资源:

resource "google_bigquery_data_transfer_config" "query_config" {

for_each = local.queries

display_name = each.value.display_name
data_source_id = each.value.data_source_id
schedule = each.value.schedule
destination_dataset_id = each.value.destination_dataset_id
params = {
destination_table_name_template = each.value.destination_table_name_template
write_disposition = each.value.write_disposition
query = each.value.query
}
}

关于google-cloud-platform - Terraform - 遍历文件夹以创建单个资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68375151/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com