内容
Terraformを使って、GCP Project上にSpannerを作成します。
Spannerの作成
Terraformコード
terraform {
required_providers {
google = {
source = "hashicorp/google"
version = "5.8.0"
}
}
}
provider "google" {
project = var.project_id
region = "asia-northeast1"
}
variable "project_id" {
description = "The project ID to deploy into"
}
resource "google_spanner_instance" "this" {
config = "regional-us-central1"
display_name = "Spanner Instance"
num_nodes = 1
}
こちらがSpanner Instanceを作成するためのTerraformのコードです。
Terraform Apply
上記コードの作成が完了したら、terraform initとterraform applyを実行します。
$ terraform init
$ terraform apply
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
# google_spanner_instance.this will be created
+ resource "google_spanner_instance" "this" {
+ config = "regional-us-central1"
+ display_name = "Spanner Instance"
+ effective_labels = (known after apply)
+ force_destroy = false
+ id = (known after apply)
+ name = (known after apply)
+ num_nodes = 1
+ processing_units = (known after apply)
+ project = "project-name"
+ state = (known after apply)
+ terraform_labels = (known after apply)
}
Plan: 1 to add, 0 to change, 0 to destroy.
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value: yes
google_spanner_instance.this: Creating...
google_spanner_instance.this: Still creating... [10s elapsed]
google_spanner_instance.this: Creation complete after 19s [id=project-name/tfgen-spanid-202405061234234]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
※Databaseの起動のため、Apply後は数分時間がかかります。
APIエラー
google_spanner_instance.this: Creating...
╷
│ Error: Error creating Instance: googleapi: Error 403: Cloud Spanner API has not been used in project project-name before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/spanner.googleapis.com/overview?project=project-name then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.
というエラーが出たら、エラーログに書いてあるURLにアクセスしてAPIを有効化してください。
GCP上にリソースができていることを確認する
GCP上でSpannerのページにアクセスすると、上記のように今回作成したSpanner Instanceが作成されていることを確認できます。