首页 > 解决方案 > 如何在 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 命令时没有创建表?请帮我解决这个问题。

标签: google-cloud-platformgoogle-bigqueryterraform

解决方案


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

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


推荐阅读