google-cloud-platform - 如何在 Bigquery GCP 中使用 terraform 创建多个表?
问题描述
我是 terraform 的新手。请让我知道我在哪里做错了。下面是我的 main.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
}
这是我的 variables.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 命令时,一切正常。但是,当我尝试运行 terraform apply 命令时,出现以下错误:
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
传播的请求都会因上述错误而暂停。您可以考虑利用所需运行路径中的依赖项来调整带有depends_on参数的 Terraform 代码。
推荐阅读
- docker - Docker 卷数据未保存在本地
- react-native - ListView 已从 ReactNative 中移除
- python - Apache Beam / Dataflow 作业中是否可以有非并行步骤?
- javascript - 如何设置 Atom 编辑器以仅使用 react-native-community eslint-config?
- angularjs - 将 Vue 应用程序集成到 Angular js 应用程序中
- javascript - 使用 Javascript 处理 HTML 和 Swift 之间的交互
- c# - 如何比较两个文本并识别哪些词被添加和哪些被删除
- html - 我想将页脚设置到窗口底部,但它与我的其他 div 重叠?
- mongodb - MongoDB,NodeJs。由于目标机器主动拒绝,无法建立连接。:
- sql - 我需要将 SQL 中的所有活动分配给一个 ID,但目前每个活动都有三个 ID