↓メインコンテンツへスキップ
  1. Blogs/

TerraformでBigQuery DatasetとTableを作成してみた

2 分
Terraform GoogleCloud
0222-nnn
著者
0222-nnn
猫が好き
目次
Terraform-GoogleCloud - この記事は連載の一部です
パート 13: この記事

概要
#

BigQueryのDatasetとTableをコンソールで作ると、Schema設定を画面上で1つずつ入力することになります。同じ構成をステージング・本番でも統一したいのに、手作業では差分が生まれます。

Terraformで管理すれば、Schema定義をコードとして残せます。ここではDatasetとTableを作成し、id、message、created_atのSchemaを定義してbq CLIで確認します。

Terraformの基本操作は前提としています。BigQueryが初めての方向けです。

Query実行、View、Dataset間のデータ転送は扱いません。

検証すること
#

  • BigQuery DatasetとTableを作成できること
  • Schema、Partition、有効期限をTerraformで設定できること
  • bqコマンドでDatasetとTable定義を確認できること

前提環境
#

  • Google Cloud CLI、Terraform、bqコマンド
  • Billingが有効な検証用Google Cloud Project
  • BigQueryを操作できるGoogleアカウント
  • 検証時のバージョン:Terraform 1.14.3、google 7.43.0

今回の構成
#

DatasetはlocationをもつコンテナでTableを含みます。Schemaは Table の属性です。

flowchart TB
    subgraph GCP["Google Cloud"]
        subgraph Project["Project"]
            subgraph DS["Dataset: tf_example_dataset
location: asia-northeast1"] subgraph Table["Table: messages"] Schema["id STRING REQUIRED
message STRING NULLABLE
created_at TIMESTAMP NULLABLE"] end end end end

Datasetのlocationはあとから変更できません。作成時に決めた場所でTableも保持されます。

使用するTerraformコード
#

Dataset
#

resource "google_bigquery_dataset" "example" {
  project                    = var.project_id
  dataset_id                 = var.dataset_id
  location                   = var.location
  delete_contents_on_destroy = var.delete_contents_on_destroy

  depends_on = [
    google_project_service.bigquery,
    google_project_service.cloudresourcemanager,
  ]
}

DatasetのLocationは、Query対象や他サービスのLocationと合わせます。作成後のLocation変更は容易ではありません。

delete_contents_on_destroy = trueならTableが存在してもDatasetを削除できます。学習環境では後片付けしやすい一方、本番データの誤削除につながるため慎重に設定します。

Schema付きTable
#

resource "google_bigquery_table" "example" {
  dataset_id          = google_bigquery_dataset.example.dataset_id
  table_id            = var.table_id
  deletion_protection = false

  schema = jsonencode([
    { name = "id",         type = "STRING",    mode = "REQUIRED" },
    { name = "message",    type = "STRING",    mode = "NULLABLE" },
    { name = "created_at", type = "TIMESTAMP", mode = "NULLABLE" },
  ])
}

HCLのObject Listをjsonencodeし、BigQueryが受け取るJSON Schemaへ変換します。学習用にdeletion_protection = falseですが、本番では保護とデータ保持方針を見直します。

設定と実行
#

cd Basic-Examples/12-bigquery
cp terraform.tfvars.example terraform.tfvars
terraform init
terraform fmt -check
terraform validate
terraform plan
terraform apply
Apply complete! Resources: 4 added, 0 changed, 0 destroyed.

Outputs:

dataset_id = "tf_example_dataset"
dataset_reference = "YOUR_PROJECT_ID:tf_example_dataset"
table_id = "messages"
table_reference = "YOUR_PROJECT_ID:tf_example_dataset.messages"

デフォルトではasia-northeast1にtf_example_dataset.messagesを作成します。

bq CLIで確認する
#

bq show "$(terraform output -raw dataset_reference)"
bq show "$(terraform output -raw table_reference)"

Table Schemaを詳細表示します。

bq show --schema --format=prettyjson \
  "$(terraform output -raw table_reference)"

後片付けと料金
#

terraform destroy

BigQueryは保存容量とQueryで読み取ったData量などに応じて課金されます。Datasetを削除すると内部のTableとDataも失われるため、delete_contents_on_destroyとBackup方針を確認します。

まとめ
#

  • DatasetのLocationと削除方針をTerraformで管理できる
  • jsonencodeでTable Schemaを定義できる
  • RequiredとNullable Fieldを使い分けられる
  • bq showで実リソースを確認できる

参考資料
#

次回
#

次はCloud SQL for PostgreSQLのInstanceとDatabaseを作成します。

Terraform-GoogleCloud - この記事は連載の一部です
パート 13: この記事

関連記事

Terraform StateをCloud Storage Remote Backendへ移行してみた
2 分
Terraform GoogleCloud CloudStorage
TerraformからGoogle Cloudへ接続してProject情報を取得してみた
3 分
Terraform GoogleCloud
TerraformでArtifact RegistryのDocker Repositoryを作成してみた
2 分
Terraform GoogleCloud Docker
TerraformでArtifact RegistryのコンテナをCloud Runへデプロイしてみた
2 分
Terraform GoogleCloud CloudRun ArtifactRegistry
TerraformでCloud KMSのKeyRingとCryptoKeyを作成してみた
2 分
Terraform GoogleCloud
TerraformでCloud MonitoringのAlert Policyを作成してみた
2 分
Terraform GoogleCloud