【问题标题】:Terraform for_each with nested resources具有嵌套资源的 Terraform for_each
【发布时间】:2021-07-21 12:34:06
【问题描述】:

我在本地使用 for_each,在 bigquery 中创建倍数数据集。 另一个 for_each 用于创建表,在以前的数据集中。

问题是我必须使用 dataset_ressource.dataset_id.id 等数据集引用表,但我使用 each.value.dataset1。

所以,它只有在我运行“terraform apply”两次时才有效

locals {
  bq_settings = {
    "${var.dataset1}" = {description = "description dataset 1"},
  } 
} 

locals {
  table_settings = {
    "${var.table1}" = {dataset_id = "dataset1_test", file = "table1.json"},
  } 
} 



resource "google_bigquery_dataset" "map" {
  for_each  = local.bq_settings
  dataset_id                  = each.key
  description                 = each.value.description
  location                    = var.location
  default_table_expiration_ms = 3600000
  
  labels = {
    env = "default"
  }
}

resource "google_bigquery_table" "map" {
  for_each  = local.table_settings

  dataset_id = each.value.dataset_id   
  table_id   = each.key
  deletion_protection = false

  time_partitioning {
    type = "DAY"
  }

  labels = {
    env = "default"
  }

  schema = file("${path.module}/schema-bq/${each.value.file}")
  
} 
```

【问题讨论】:

    标签: terraform terraform-provider-gcp terraform0.12+


    【解决方案1】:

    它第二次起作用了,因为google_bigquery_datasetgoogle_bigquery_table 之间没有直接关系。您可以使用depends_on 显式添加这样的关系:

    resource "google_bigquery_table" "map" {
      for_each  = local.table_settings
    
      dataset_id = each.value.dataset_id   
      table_id   = each.key
      deletion_protection = false
    
      time_partitioning {
        type = "DAY"
      }
    
      labels = {
        env = "default"
      }
    
      schema = file("${path.module}/schema-bq/${each.value.file}")
    
      depends_on = [google_bigquery_table.map]  
    } 
    

    【讨论】:

    • 没问题。如果答案有帮助,我们将不胜感激。
    猜你喜欢
    • 2021-10-06
    • 2021-08-02
    • 1970-01-01
    • 2021-11-06
    • 2020-07-07
    • 2021-02-13
    • 2021-10-04
    • 1970-01-01
    • 2020-01-31
    相关资源
    最近更新 更多