IaC Bazaar
Google CloudPlan-validated

Cloud SQL (PostgreSQL/MySQL) HA Instance

Regional-HA Cloud SQL with private IP (PSA/PSC), automated backups, PITR, read replicas and IAM database auth.

terraformGCP#gcp

Compare Managed Relational Database across clouds →

gcp-cloud-sqlterraform v1.7

Verification

Plan-validated

Passed: module logic verified on a mocked plan — inputs, validation rules, conditional creation and outputs resolve (no real provider, no cloud).

Conformance

  • Static validation (fmt · validate · tflint)
  • Security scan: findings disclosed (Checkov)
  • Plan tests (mocked: validation rules · outputs)

Provenance

  • SHA-256 checksum
  • Signature (pending)

Functional

  • Live test pending (no cloud run yet)

Last verified 2026-06-28 · how we verify

Documentation

gcp-cloud-sql

Regional-HA Cloud SQL (PostgreSQL/MySQL) with private IP via Private Service Access, automated backups, point-in-time recovery, read replicas and IAM database auth — the PSA networking footgun solved for you. Works with Terraform and OpenTofu (>= 1.6), Google provider >= 7.0, < 8.0.

Secure defaults:

  • No public IP (ipv4_enabled = false); private IP through a PSA peering the module can create for you (reserved range + servicenetworking connection)
  • TLS-only client connections (ssl_mode = "ENCRYPTED_ONLY")
  • deletion_protection = true
  • Regional HA (availability_type = "REGIONAL"), daily backups + PITR (engine-aware: WAL on PostgreSQL, binlog on MySQL)
  • IAM database authentication on, with the engine-correct flag spelling

Usage

module "cloud_sql" {
  source = "./gcp-cloud-sql"

  project_id       = "my-project"
  name             = "prod-postgres"
  region           = "us-central1"
  database_version = "POSTGRES_17"
  tier             = "db-custom-2-8192"
  private_network  = "projects/my-project/global/networks/prod-vpc"

  databases      = ["app"]
  users          = { app = { type = "BUILT_IN" } }
  user_passwords = { app = var.db_password }

  read_replicas = {
    reporting = { region = "us-east1" }
  }
}

Inputs

NameTypeDefaultDescription
project_idstringHost project (required)
namestringInstance name (required)
regionstringPrimary region (required)
database_versionstringPOSTGRES_17POSTGRES_* or MYSQL_*
tierstringdb-custom-2-8192Machine tier
editionstringENTERPRISEENTERPRISE or ENTERPRISE_PLUS
availability_typestringREGIONALREGIONAL (HA) or ZONAL
deletion_protectionbooltrueSet false in disposable envs
disk_typestringPD_SSDPD_SSD / PD_HDD / HYPERDISK_BALANCED
disk_size_gbnumber20Initial disk size
disk_autoresizebooltrueAuto-grow disk
disk_autoresize_limitnumber0Autoresize cap (0 = none)
ipv4_enabledboolfalsePublic IP
private_networkstringnullVPC self link for private IP
create_private_service_accessbooltrueCreate PSA range + peering
psa_prefix_lengthnumber16Reserved-range prefix length
ssl_modestringENCRYPTED_ONLYTLS enforcement
enable_private_path_for_google_cloud_servicesboolfalsePrivate path for Google services
authorized_networkslist(object)[]Public-IP CIDR allow-list
backup_enabledbooltrueDaily backups
backup_start_timestring03:00Backup window (UTC)
point_in_time_recoverybooltruePITR
transaction_log_retention_daysnumber7PITR log retention
retained_backupsnumber7Backups kept
maintenance_windowobjectday 7, hour 3, stableMaintenance preferences
query_insights_enabledbooltrueQuery Insights
iam_authenticationbooltrueIAM DB auth flag
database_flagsmap(string){}Extra flags
read_replicasmap(object){}Replicas keyed by suffix
databaseslist(string)[]Logical databases
usersmap(object){}Users (type, host)
user_passwordsmap(string){}Sensitive; keyed like users
labelsmap(string){}user_labels on all instances

Outputs

instance_name, connection_name, self_link, private_ip_address, public_ip_address, replica_connection_names, replica_private_ip_addresses, psa_range_name, database_names.

Notes

  • PSA: a VPC can hold only one servicenetworking.googleapis.com connection. If yours already has one, set create_private_service_access = false and the instance reuses it.
  • The instance needs at least one network path — the module fails the plan if both ipv4_enabled = false and private_network = null.
  • Read replicas are always ZONAL (Cloud SQL does not support HA replicas) and require backup_enabled = true plus PITR logging on the primary.
  • Instance names cannot be reused for ~7 days after deletion — suffix names in ephemeral environments.

Requirements

  • Terraform or OpenTofu >= 1.6
  • hashicorp/google >= 7.0, < 8.0
  • APIs: sqladmin.googleapis.com, plus servicenetworking.googleapis.com and compute.googleapis.com when using private IP

Verification

Static-validated (fmt, validate, tflint). Live apply/destroy testing pending cloud sandbox availability — see catalog status.

License

Commercial — IaC Bazaar EULA. © IaC Bazaar. Original work (not derived from a third-party module).