我添加了一个 json 文件,其中包含我要创建的所有表:
表格.json
"tables": {
"table1": {
"dataset_id": "dataset1",
"table_id": "table1",
"schema_path": "folder/table1.json"
},
"table2": {
"dataset_id": "dataset2",
"table_id": "table2",
"schema_path": "folder/table2.json"
}
}
然后在 Terraform 资源上使用 foreach,我想动态创建这些表:
本地.tf文件
locals {
tables = jsondecode(file("${path.module}/resource/tables.json"))["tables"]
}
变量.tf文件
variable "project_id" {
description = "Project ID, used to enforce providing a project id."
type = string
}
variable "time_partitioning" {
description = "Configures time-based partitioning for this table."
type = map(string)
default = {
type = "DAY"
field = "my_field"
}
}
main.tf 文件
resource "google_bigquery_table" "tables" {
for_each = local.tables
project = var.project_id
dataset_id = each.value["dataset_id"]
table_id = each.value["table_id"]
dynamic "time_partitioning" {
for_each = [
var.time_partitioning
]
content {
type = try(each.value["partition_type"], time_partitioning.value["type"])
field = try(each.value["partition_field"], time_partitioning.value["field"])
expiration_ms = try(time_partitioning.value["expiration_ms"], null)
require_partition_filter = try(time_partitioning.value["require_partition_filter"], null)
}
}
schema = file("${path.module}/resource/schema/${each.value["schema_path"]}")
}
架构文件包含经典的 bigquery 架构,例如:
[
{
"name": "field",
"type": "STRING",
"mode": "NULLABLE",
"description": "My field"
}
]
表的创建效果很好,但是当我在模式上添加一个新的可为空字段时,Terraform 建议“替换表”(销毁并重新创建)而不是“更新表”。
在这种情况下,原生 Bigquery 和 Terraform 的正常行为是更新表。
当使用相同的 Terraform 资源但没有 forEach 进行相同的测试时,Terraform 具有预期的行为并建议更新表。
带有“forEach”的 Terraform 日志示例:
# google_bigquery_table.tables["table1"] must be replaced
-/+ resource "google_bigquery_table" "tables" {
~ creation_time = 1616764894477 -> (known after apply)
dataset_id = "dataset1"
deletion_protection = true
~ etag = "G9qwId8jgQS8nN4N61zqcA==" -> (known after apply)
~ expiration_time = 0 -> (known after apply)
~ id = "projects/my-project/datasets/dataset1/tables/table1" -> (known after apply)
- labels = {} -> null
~ last_modified_time = 1617075251337 -> (known after apply)
~ location = "EU" -> (known after apply)
~ num_bytes = 0 -> (known after apply)
~ num_long_term_bytes = 0 -> (known after apply)
~ num_rows = 0 -> (known after apply)
project = "project"
~ schema = jsonencode(
~ [ # forces replacement
{
description = "Field"
mode = "NULLABLE"
name = "field"
type = "STRING"
}
.....
+ {
+ description = "Field"
+ mode = "NULLABLE"
+ name = "newField"
+ type = "STRING"
}
Terraform 正确显示并检测要为表添加的新列,但指示替换而不是版本。
我再说一遍,在没有 forEach 且在单个 Bigquery 表上使用相同 Terraform 资源进行的完全相同的测试效果很好(相同的架构,相同的更改)。我创建了表,当添加一个新的可为 null 的列时,Terraform 提出了一个版本(预期行为)。
我查看了 Terraform 文档和网络,没有看到一些使用 Terraform 管理表列表的示例。
是否无法使用已配置的表和 foreach 创建和更新表?
感谢您的帮助。
最佳答案
这听起来像是提供商错误。我找到了这个 issue在terraform-provider-google似乎与您的问题相关的存储库。 fix仅在 13 小时前(撰写本文时)合并。所以,也许您可以等待下一个版本 (v3.63.0
),看看它是否能解决您的问题。
仅供引用:您可能想要验证修复提交是否确实包含在下一个版本中。之前遇到过在release之前合并到master里面的东西,实际上并没有发布。
关于google-cloud-platform - Terraform Bigquery 创建表替换表而不是编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66884998/