Terraform Bigquery Table 架构在单独的 JSON 文件中

Terraform Bigquery Table Schema in a separate JSON file

我知道我们可以在 Terraform 文件中定义 table 模式。我的问题是,有没有一种方法可以在单独的文件中定义架构并在 运行 时间将其导入 Terraform。这样它将提供更好的管理和可读性

 resource "google_bigquery_table" "default" {
 dataset_id = google_bigquery_dataset.default.dataset_id
 table_id   = "bar"

  time_partitioning {
   type = "DAY"
  }

 labels = {
  env = "default"
  }

 **schema = <<EOF
[
 {
   "name": "permalink",
   "type": "STRING",
   "mode": "NULLABLE",
   "description": "The Permalink"
 }
]** 
EOF

}

所以基本上我要问的是如何将模式部分移动到单独的文件,并在 运行 时间 TF 导入它。

如果模式不会动态生成,那么您可以使用 file function 来达到此目的:

schema = file("${path.module}/nested_path_to/schema.json")

schema.json:

[
  {
    "name": "permalink",
    "type": "STRING",
    "mode": "NULLABLE",
    "description": "The Permalink"
  }
]

如果模式将动态生成,那么您应该为此目的使用 templatefile function

schema = templatefile("${path.module}/nested_path_to/schema.json.tmpl", { variable_name = variable_value } )