具有嵌套资源的 Terraform for_each
Terraform for_each with nested resources
我在本地使用 for_each,在 bigquery 中创建多个数据集。
另一个 for_each 在之前的数据集中创建 table。
问题是我必须用 dataset_ressource.dataset_id.id 之类的数据集引用 table 但我使用 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}")
}
```
第二次有效,因为google_bigquery_dataset
和google_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]
}
我在本地使用 for_each,在 bigquery 中创建多个数据集。 另一个 for_each 在之前的数据集中创建 table。
问题是我必须用 dataset_ressource.dataset_id.id 之类的数据集引用 table 但我使用 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}")
}
```
第二次有效,因为google_bigquery_dataset
和google_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]
}