google_alloydb_instance

A managed alloydb cluster instance.

To get more information about Instance, see:

Example Usage - Alloydb Instance Basic

resource "google_alloydb_instance" "default" {
  cluster       = google_alloydb_cluster.default.name
  instance_id   = "alloydb-instance"
  instance_type = "PRIMARY"

  machine_config {
    cpu_count = 2
  }

  depends_on = [google_service_networking_connection.vpc_connection]
}

resource "google_alloydb_cluster" "default" {
  cluster_id = "alloydb-cluster"
  location   = "us-central1"
  network    = google_compute_network.default.id

  initial_user {
    password = "alloydb-cluster"
  }
}

data "google_project" "project" {}

resource "google_compute_network" "default" {
  name = "alloydb-network"
}

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

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

Example Usage - Alloydb Secondary Instance Basic

resource "google_alloydb_cluster" "primary" {
  cluster_id = "alloydb-primary-cluster"
  location   = "us-central1"
  network    = google_compute_network.default.id
}

resource "google_alloydb_instance" "primary" {
  cluster       = google_alloydb_cluster.primary.name
  instance_id   = "alloydb-primary-instance"
  instance_type = "PRIMARY"

  machine_config {
    cpu_count = 2
  }

  depends_on = [google_service_networking_connection.vpc_connection]
}

resource "google_alloydb_cluster" "secondary" {
  cluster_id   = "alloydb-secondary-cluster"
  location     = "us-east1"
  network      = google_compute_network.default.id
  cluster_type = "SECONDARY"

  continuous_backup_config {
    enabled = false
  }

  secondary_config {
    primary_cluster_name = google_alloydb_cluster.primary.name
  }

  deletion_policy = "FORCE"

  # Need lifecycle.ignore_changes because instance_type is an immutable field.
  # And when promoting cluster from SECONDARY to PRIMARY, the instance_type of the associated secondary instance also changes and becomes PRIMARY.
  # And we do not want terraform to destroy and create the instance because the field is immutable
  lifecycle {
    ignore_changes = [instance_type]
  }

  depends_on = [google_alloydb_instance.primary]
}

resource "google_alloydb_instance" "secondary" {
  cluster       = google_alloydb_cluster.secondary.name
  instance_id   = "alloydb-secondary-instance"
  instance_type = google_alloydb_cluster.secondary.cluster_type

  machine_config {
    cpu_count = 2
  }

  depends_on = [google_service_networking_connection.vpc_connection]
}

data "google_project" "project" {}

resource "google_compute_network" "default" {
  name = "alloydb-secondary-network"
}

resource "google_compute_global_address" "private_ip_alloc" {
  name          =  "alloydb-secondary-instance"
  address_type  = "INTERNAL"
  purpose       = "VPC_PEERING"
  prefix_length = 16
  network       = google_compute_network.default.id
}

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

Argument Reference

The following arguments are supported:


The query_insights_config block supports:

The read_pool_config block supports:

The machine_config block supports:

The client_connection_config block supports:

The ssl_config block supports:

The network_config block supports:

The authorized_external_networks block supports:

Attributes Reference

In addition to the arguments listed above, the following computed attributes are exported:

Timeouts

This resource provides the following Timeouts configuration options:

Import

Instance can be imported using any of these accepted formats:

In Terraform v1.5.0 and later, use an import block to import Instance using one of the formats above. For example:

import {
  id = "projects/{{project}}/locations/{{location}}/clusters/{{cluster}}/instances/{{instance_id}}"
  to = google_alloydb_instance.default
}

When using the terraform import command, Instance can be imported using one of the formats above. For example:

$ terraform import google_alloydb_instance.default projects/{{project}}/locations/{{location}}/clusters/{{cluster}}/instances/{{instance_id}}
$ terraform import google_alloydb_instance.default {{project}}/{{location}}/{{cluster}}/{{instance_id}}
$ terraform import google_alloydb_instance.default {{location}}/{{cluster}}/{{instance_id}}