[A-00139]TerraformでAlloyDBを作成してみる

google cloud AlloyDBをterraformを使って作ってみます。

・API有効化

下記のURLより、AlloyDB APIを有効にします。

https://console.cloud.google.com/apis/api/alloydb.googleapis.com/overview

・Service Network API有効化

こちらも有効化が必要

https://console.cloud.google.com/flows/enableapi?apiid=servicenetworking.googleapis.com&_ga=2.212548368.583953121.1693422346-1180479067.1693071751&_gac=1.195351134.1693422346.Cj0KCQjw0bunBhD9ARIsAAZl0E1uCUCw5we94rkGZydijkllYczeJIHymb-meRzcLHpz43BWsvjuVvEaAgK_EALw_wcB

・Terraform作成

provider "google" {
    project = var.project_id
}

resource "google_compute_network" "default" {
  name = "test-vpc"
}

resource "google_compute_global_address" "default" {
  name = "${var.project_id}-alloydb-cluster-ip"
  address_type = "INTERNAL"
  purpose = "VPC_PEERING"
  prefix_length = 16
  network = google_compute_network.default.id
}

resource "google_service_networking_connection" "default" {
  network = google_compute_network.default.id
  service = "servicenetworking.googleapis.com"
  reserved_peering_ranges = [ google_compute_global_address.default.name ]
}

resource "google_alloydb_cluster" "default" {
  cluster_id = "${var.project_id}-alloydb-cluster"
  network = google_compute_network.default.id
  location = "asia-northeast1"
}

resource "google_alloydb_instance" "default" {
  instance_id = "${var.project_id}-alloydb-primary-instance"
  instance_type = var.alloydb_instance_type
  cluster = google_alloydb_cluster.default.name
  
  database_flags = {
    "alloydb.logical_decoding" = "on"
  }

  depends_on = [
    google_service_networking_connection.default
  ]
}
variable "project_id" {
  default = <your_project_id>
}

variable "alloydb_instance_type" {
  default = "PRIMARY"
}

上記を作成してterraformコマンドを実行します。

terraform init
terraform plan
terraform apply

下記のように作成されます。

・Appendix

公式ドキュメントはこちら

https://cloud.google.com/alloydb/docs/project-enable-access

https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/alloydb_cluster

https://registry.terraform.io/modules/GoogleCloudPlatform/alloy-db/google/latest

参考文献はこちら

https://registry.terraform.io/modules/GoogleCloudPlatform/alloy-db/google/latest

https://qiita.com/baeyear/items/f1ce46973511ad999f9a

https://github.com/hashicorp/terraform-provider-google/issues/11754

https://github.com/GoogleCloudPlatform/terraform-google-alloy-db/blob/main/main.tf

https://dev.classmethod.jp/articles/alloydb-for-postgresql-is-generally-available/

https://medium.com/google-cloud/alloy-db-terraform-module-b427ae079ea

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

*