1

我正在创建一个模块,以便在 GCP 中轻松配置 BigQuery 表。我的模块正在工作,但现在我正在尝试添加基于外部数据创建表的选项,例如 GCS 存储桶。

在文档(https://www.terraform.io/docs/providers/google/r/bigquery_table.html#external_data_configuration)中说支持此配置,但我只收到此错误:

Acquiring state lock. This may take a few moments...

Error: Unsupported block type

  on ../../modules/bq_table/main.tf line 24, in resource "google_bigquery_table" "default":
  24:   external_data_configuration {

Blocks of type "external_data_configuration" are not expected here.

我在 Mac OS 中使用最新的 Terraform 版本(0.12.5)和谷歌提供程序 v2.10.0。

这是我在 HCL2 中的模块代码:

resource "google_bigquery_table" "default" {
  dataset_id  = "${terraform.workspace}_${var.bq_dataset_id}"
  table_id    = "${terraform.workspace}_${var.bq_table_id}"
  project     = (var.project_id != "" ? var.project_id : null)
  description = (var.bq_table_description != "" ? var.project_id : null)
  expiration_time = (var.bq_table_expiration_time != null ? var.project_id : null)
  friendly_name = (var.bq_table_name != "" ? var.project_id : null)

  dynamic "external_data_configuration" {
    for_each = var.bq_table_external_data_configuration
    content {
      autodetect = true
      source_format = "NEWLINE_DELIMITED_JSON"
      source_uris = [external_data_configuration.value]
    }
  }

  time_partitioning {
    type = "DAY"
    field = var.bq_table_partition_field
  }

  labels = var.bq_table_labels

  schema = (var.bq_table_schema != "" ? var.bq_table_schema : null)

  dynamic "view" {
    for_each = (var.bq_table_view_query != "" ? {query = var.bq_table_view_query} : {})
    content {
      query = view.value
    }
  }

  depends_on = ["null_resource.depends_on"]
}

以上即时使用动态块,但尝试正常使用,错误是一样的。

4

1 回答 1

0

块内的for_each属性dynamic需要一个数组值。尝试将输入变量包装在一个数组中:

dynamic "external_data_configuration" {
    for_each = var.bq_table_external_data_configuration ? [var.bq_table_external_data_configuration] : []
    content {
      autodetect = true
      source_format = "NEWLINE_DELIMITED_JSON"
      source_uris = [external_data_configuration.value]
    }
}

即使在 Terraform 0.12 之后,条件块仍然有点麻烦;在这里阅读更多。

于 2019-08-07T13:37:36.320 回答