如何 use/reference BigQuery Schema 文件中的 Terraform 输出值

How to use/reference Terraform output values in BigQuery Schema file

使用 Terraform BigQuery 模块部署 BQ 架构。尝试定义政策标签,但不确定如何在我的 JSON 架构中引用新创建的分类法和政策标签 ID。下面是我的 schema.json 如何使用链接到字段

的政策标签的虚拟摘录

问题: 下面的架构将分类法和政策标签的 ID 引用为

   ${google_data_catalog_taxonomy.my_taxonomy.id}

但是当我应用 TF 时,它不会替换值并抛出异常

Error 400: Invalid value for policyTags: 
projects/my_project/locations/europe- 
west2/taxonomies/${google_data_catalog_taxonomy.my_taxonomy.id}/policyTags/${google_data_catalog_policy_tag.PII.id} is not a valid value. Expected value should follow the format "projects/<projectId>/locations/<locationId>/taxonomies/<taxonomyId>/policyTags/<policyTagId>". 

Table_1.json 看起来像下面

 {
      "fields": [
        {
          "mode": "NULLABLE",
          "name": "Email",
          "type": "STRING",
          "policyTags":{
            "names": [
              "projects/my_project/locations/europe-west2/taxonomies/${google_data_catalog_taxonomy.my_taxonomy.id}/policyTags/${google_data_catalog_policy_tag.PII.id}"
              ]
          }
        },
        {
          "mode": "NULLABLE",
          "name": "Mobile",
          "type": "STRING",
          "policyTags":{
            "names": [
              "projects/my_project/locations/europe-west2/taxonomies/${google_data_catalog_taxonomy.my_taxonomy.id}/policyTags/${google_data_catalog_policy_tag.PII.id}"
              ]
          }
        },
 }

我正在输出分类法和策略标签,如下所示。任何人都可以建议如何在 schema.json 文件中引用它。

outputs.tf

output "my_taxonomy" {
   value = google_data_catalog_taxonomy.my_taxonomy.id
 }

output "PII" {
   value = google_data_catalog_policy_tag.PII.id
 }

编辑:

我正在使用 TF BigQuery 模块,其中我的 table 架构存在于单独的文件中。

main.tf

 module "bigquery" {
 source  = "terraform-google-modules/bigquery/google"
 dataset_id                  = "my_Dataset"
 dataset_name                = "my_Dataset"
 description                 = "my_Dataset"
 project_id                  = "my_project_id"
 location                    = "europe-west2"
 default_table_expiration_ms = 3600000

 tables = [
{
table_id           = "table_!",
**schema             =  "table_1.json",**
time_partitioning  = null,

range_partitioning = null,
expiration_time = null,
clustering      = null,
labels          = {
env  = "dev"
  
       }
      }
    },
  ]

}

您可以使用 templatefile( path ,vars ) 导入 json 模板。

编辑您的 json 以使用 ${ ... } 语法包含变量。

{
      "fields": [
        {
          "mode": "NULLABLE",
          "name": "Email",
          "type": "STRING",
          "policyTags":{
            "names": [
              "projects/my_project/locations/europe-west2/taxonomies/${my_taxonomy}/policyTags/${PII}"
              ]
          }
        },
        {
          "mode": "NULLABLE",
          "name": "Mobile",
          "type": "STRING",
          "policyTags":{
            "names": [
              "projects/my_project/locations/europe-west2/taxonomies/${my_taxonomy}/policyTags/${PII}"
              ]
          }
        },
 }

在您的 terraform 配置中编辑 schema 以使用 templatefile 函数

 module "bigquery" {
 source  = "terraform-google-modules/bigquery/google"
 dataset_id                  = "my_Dataset"
 dataset_name                = "my_Dataset"
 description                 = "my_Dataset"
 project_id                  = "my_project_id"
 location                    = "europe-west2"
 default_table_expiration_ms = 3600000

 tables = [
{
table_id           = "table_!",
schema             =  templatefile(
                        "${path.module}/table_1.json",
                        { 
                            my_taxonomy = "${google_data_catalog_taxonomy.my_taxonomy.id}",
                            PII         = "${google_data_catalog_policy_tag.PII.id}"
                        }),

time_partitioning  = null,
range_partitioning = null,
expiration_time = null,
clustering      = null,
labels          = {
env  = "dev"
  
       }
      }
    },
  ]

}