GCP中使用Terraform创建多个表

GCP中使用Terraform创建多个表

本文介绍了如何在Bigquery GCP中使用Terraform创建多个表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Terraform.请让我知道我在哪里做错了.下面是我的main.tf代码:

I am new to terraform. Please let me know where I am doing wrong. Below is my main.tf code:

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代码:

and here is my variables.tf code:

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命令时,出现以下错误:

everything works fine when I run terraform init and terraform plan commands. But, when I try to run terraform apply command, I get the following error:

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命令时,两个表都被创建.下面是控制台:

When I again try to run terraform apply command, both the tables get created. Below is the console:

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命令时未创建表?请帮我解决这个问题.

Why doesn't the tables get created when I run the terraform apply command the first time ?Please help me solve this issue.

推荐答案

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

I'm quoting my comment as an answer in order to support further contributors with their research on the same subject.

在您当前的情况下,dataset的创建在Bigquery运行时中有些延迟,因此,所有扩展了tables的请求都因上​​述错误而被暂停.您可以考虑使用 depends_on 来调整Terraform代码.利用所需运行路径中的依赖性的参数.

In your current scenario dataset creation is a bit delayed in Bigquery runtime, thus all the requests with tables spreading are just suspended with the above mentioned error. You can consider adjusting Terraform code with depends_on argument leveraging dependency in the desired run path.

这篇关于如何在Bigquery GCP中使用Terraform创建多个表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 14:53