首页 > 解决方案 > Terraform GCP bigquery 外部数据配置模块

问题描述

我正在为带有外部数据配置的 bigquery 表编写 terraform 模块。我有嵌套对象,下面的代码不起作用。


  tables = { for table in var.tables : table["table_id"] => table }
}

locals {
  tables_from_sheets = { for table_from_sheets in var.tables_from_sheets : table_from_sheets["table_id"] => table_from_sheets }
}


resource "google_bigquery_dataset" "main" {

  dataset_id                  = var.dataset_id
  friendly_name               = var.friendly_name
  description                 = var.description
  location                    = var.location
  default_table_expiration_ms = var.default_table_expiration_ms
  project                     = var.project
  labels                      = var.dataset_labels
}

resource "google_bigquery_table" "main" {
  for_each        = local.tables
  dataset_id      = google_bigquery_dataset.main.dataset_id
  friendly_name   = each.key
  table_id        = each.key
  labels          = each.value["labels"]
  schema          = each.value["schema"]
  expiration_time = each.value["expiration_time"]
  project         = var.project
  deletion_protection = each.value["deletion_protection"]

  dynamic "external_data_configuration" {
    for_each = each.value["external_data_configuration"] != null ? [each.value["external_data_configuration"]] : []
    content {
      autodetect               = external_data_configuration.value["autodetect"]
      source_format            = external_data_configuration.value["source_format"]
      source_uris              = external_data_configuration.value["source_uris"]

      dynamic "google_sheets_options" {
        for_each = external_data_configuration.value.google_sheets_options
        content {
          range =  google_sheets_options.value["range"]
          skip_leading_rows = google_sheets_options.value["skip_leading_rows"]
     }
    }
   }
  }
}

变量.tf

variable "tables" {
  description = "A list of objects which include table_id, schema, clustering, time_partitioning, expiration_time and labels."
  default     = []
  type = list(object({
    table_id   = string,
    schema     = string, // Either pass json file  or actual schema
    deletion_protection = string,
    external_data_configuration = object({
      autodetect    = bool,
      source_format = string,
      source_uris   = list(string),
      google_sheets_options = object({
        skip_leading_rows = number,
        range = string,
    }),
    }),
    expiration_time = string,
    labels          = map(string),
  }))
}

但是 terraform plan 有以下错误:

但是 terraform plan 有以下错误:

│错误:无效索引││在../../module/bquery/main.tf第42行,资源“google_bigquery_table”“main”中:│42:range = google_sheets_options.value[“range”]│├── ────────────── │ │ google_sheets_options.value 是 1 │ │ 这个值没有任何索引。

请让我知道如何解决此问题。我的理解可能对这些价值观有误。

标签: google-bigqueryterraform

解决方案


推荐阅读