Files
solution_architecture_agent/skills/google-cloud-iac-patterns/references/cloud-sql.md
Jonathan Boniface c84dbdea20 feat: the solution architecture agent on the shared Execution engine
The reference agent's subject, cloud solution architecture, as an agent in
the operating model agent's pattern: the Execution engine (from
orchestration_engine) and one Workflow in app/solution_architecture,
built for the settings and provider bundle by assembly.workflow_for.

Copied from gcp_solution_architecture_agent at 8b397cd without its own
engine: its role files, Skills, provider bundle, ADRs, vocabulary, scripts
and evaluation datasets. The image takes the engine from a named build
context and installs Terraform. It is called through the Orchestration
engine, and its files are what the operating model agent digests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-21 19:49:28 +01:00

3.7 KiB

Cloud SQL

A PostgreSQL instance with a private address, backups and IAM database authentication.

  • A private address needs a reserved range and a service networking connection on the VPC first; the instance must depends_on the connection.
  • availability_type = "REGIONAL" is high availability; "ZONAL" is a single zone.
  • Backups, point-in-time recovery, flags and the address are all in settings.
  • deletion_protection on the resource guards Terraform; settings.deletion_protection_enabled guards the API.
  • Take the database password from Secret Manager at run time, or use IAM database authentication as here. Never put a password in the configuration.
variable "project_id" {
  type        = string
  description = "Project to deploy into."
}

variable "region" {
  type        = string
  description = "Region for regional resources."
}

variable "deletion_protection" {
  type        = bool
  description = "Whether Terraform is prevented from deleting the instance."
  default     = true
}

resource "google_compute_network" "main" {
  project                 = var.project_id
  name                    = "main"
  auto_create_subnetworks = false
}

resource "google_compute_global_address" "private_services" {
  project       = var.project_id
  name          = "private-services"
  purpose       = "VPC_PEERING"
  address_type  = "INTERNAL"
  prefix_length = 16
  network       = google_compute_network.main.id
}

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

resource "google_service_account" "app" {
  project      = var.project_id
  account_id   = "orders-app"
  display_name = "Orders application"
}

resource "google_sql_database_instance" "orders" {
  project             = var.project_id
  name                = "orders"
  region              = var.region
  database_version    = "POSTGRES_16"
  deletion_protection = var.deletion_protection

  settings {
    tier                        = "db-custom-2-7680"
    edition                     = "ENTERPRISE"
    availability_type           = "REGIONAL"
    disk_type                   = "PD_SSD"
    disk_autoresize             = true
    deletion_protection_enabled = var.deletion_protection

    ip_configuration {
      ipv4_enabled    = false
      private_network = google_compute_network.main.id
      ssl_mode        = "ENCRYPTED_ONLY"
    }

    backup_configuration {
      enabled                        = true
      point_in_time_recovery_enabled = true
      start_time                     = "02:00"

      backup_retention_settings {
        retained_backups = 14
      }
    }

    database_flags {
      name  = "cloudsql.iam_authentication"
      value = "on"
    }

    maintenance_window {
      day  = 7
      hour = 3
    }
  }

  depends_on = [google_service_networking_connection.private_services]
}

resource "google_sql_database" "orders" {
  project  = var.project_id
  name     = "orders"
  instance = google_sql_database_instance.orders.name
}

resource "google_sql_user" "app" {
  project  = var.project_id
  name     = trimsuffix(google_service_account.app.email, ".gserviceaccount.com")
  instance = google_sql_database_instance.orders.name
  type     = "CLOUD_IAM_SERVICE_ACCOUNT"
}

resource "google_project_iam_member" "app_connects" {
  project = var.project_id
  role    = "roles/cloudsql.client"
  member  = google_service_account.app.member
}

resource "google_project_iam_member" "app_logs_in" {
  project = var.project_id
  role    = "roles/cloudsql.instanceUser"
  member  = google_service_account.app.member
}