如何在Bigquery GCP中使用地形创建多个表



我是地形的新手。请告诉我哪里做错了。下面是我的主要.tf代码:

resource "google_bigquery_dataset" "demo" {
dataset_id                  = "${var.db_id}"
friendly_name               = "${var.friendly_name}"
location                    = "${var.region}"
default_table_expiration_ms = "3600000"
labels = {
env = "default"
}
}
resource "google_bigquery_table" "demo" {
dataset_id = "${var.db_id}"
table_id   = "${var.table}"
time_partitioning {
type = "DAY"
}
labels = {
env = "default"
}
schema = <<EOF
[
{
"name": "br_op_inventory_a",
"type": "STRING",
"mode": "NULLABLE"
},
{
"name": "state",
"type": "STRING",
"mode": "NULLABLE"
}
]
EOF
}
resource "google_bigquery_table" "devops" {
dataset_id = "${var.db_id}"
table_id   = "${var.table_1}"
time_partitioning {
type = "DAY"
}
labels = {
env = "default"
}
schema = <<EOF
[
{
"name": "br_op_inventory_a",
"type": "STRING",
"mode": "NULLABLE"
},
{
"name": "state",
"type": "STRING",
"mode": "NULLABLE"
}
]
EOF
}

这是我的变量.tf代码:

variable "friendly_name" {
default="test"
}
variable "region" {
default="asia-south1"
}
variable "db_id" {
default="operation_performance_test_2"
}
variable "table" {
default="sample_demo"
}
variable "table_1" {
default="price_inventory"
}

当我运行terraform init和terraform plan命令时,一切都很好。但是,当我尝试运行地形应用命令时,我会得到以下错误:

google_bigquery_table.demo: Creating...
google_bigquery_table.devops: Creating...
google_bigquery_dataset.demo: Creation complete after 4s [id=projects/midyear-cursor-264117/datasets/operation_performance_test_2]
Error: googleapi: Error 404: Not found: Dataset midyear-cursor-264117:operation_performance_test_2, notFound
on main.tf line 10, in resource "google_bigquery_table" "demo":
10: resource "google_bigquery_table" "demo" {

Error: googleapi: Error 404: Not found: Dataset midyear-cursor-264117:operation_performance_test_2, notFound
on main.tf line 35, in resource "google_bigquery_table" "devops":
35: resource "google_bigquery_table" "devops" {

当我再次尝试运行terraform apply命令时,两个表都会被创建。下面是控制台:

google_bigquery_table.devops: Creating...
google_bigquery_table.demo: Creating...
google_bigquery_table.devops: Creation complete after 3s [id=projects/midyear-cursor-264117/datasets/operation_performance_test_2/tables/price_inventory]
google_bigquery_table.demo: Creation complete after 4s [id=projects/midyear-cursor-264117/datasets/operation_performance_test_2/tables/sample_demo]
Apply complete! Resources: 2 added, 0 changed, 0 destroyed

为什么我第一次运行terraform apply命令时没有创建表?请帮我解决这个问题。

我引用我的评论作为答案,以支持更多的贡献者对同一主题的研究。

在您当前的场景中,dataset的创建在Bigquery运行时有点延迟,因此所有具有tables扩展的请求都会因上述错误而暂停。您可以考虑在所需的运行路径中使用dependents_on参数来调整Terraform代码。

最新更新