Files
gcp_solution_architecture_a…/skills/google-cloud-iac-modules/references/cloud-sql.md
Jonathan Boniface ef37f59bad feat: opt-in published Terraform modules from approved sources (iteration 8)
Infrastructure code is plain resources by default. An Execution may opt in
with terraform_modules; the deployment default is TERRAFORM_MODULES, off.

- A module is accepted only from the registry namespaces the provider bundle
  lists (terraform-google-modules), at one exact version. Any other source is
  refused before Terraform runs; until now nothing stopped generated code
  naming any source for terraform init to download.
- terraform_module looks up a module's inputs, outputs and submodules from
  the registry and says when it cannot be used with the pinned provider.
- skills/google-cloud-iac-modules: when a module fits, and four modules with
  examples a test validates against hashicorp/google 8.6.0.
- Opted in, the IaC author and validator get the Skill and the lookup. Not
  opted in, they get neither and a module block is refused in code.
- A Change request follows its Baseline. execution.json records the choice.
- case-07 runs case 03's request with modules opted into.

Azure (aztfm) and AWS (terraform-aws-modules) sources are recorded in the
plan for when those Cloud providers are added. No case has run with this.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 22:09:29 +01:00

103 lines
3.2 KiB
Markdown

# Cloud SQL for PostgreSQL on a private address
A PostgreSQL instance reachable only inside a VPC, with the private services access it needs. Replaces a reserved range, a service networking connection, the instance, its database and its user.
- The instance must wait for the peering: pass `module.private_access.peering_completed` in `module_depends_on`.
- `ip_configuration.ipv4_enabled = false` is what removes the public address. `private_network` is the network's self link.
- `availability_type = "REGIONAL"` gives a standby in a second zone.
- `deletion_protection` is the Terraform guard; `deletion_protection_enabled` is the guard in the API. Set both from a variable.
- With `enable_default_user = true` the module generates a password and exposes it as an output. Prefer IAM database authentication: set `enable_default_user = false`, add the `cloudsql.iam_authentication` flag and list `iam_users`.
- `random_instance_name = true` adds a suffix, which lets an instance be recreated: a deleted instance's name is reserved for a week.
```hcl
variable "project_id" {
type = string
description = "Project to deploy into."
}
variable "region" {
type = string
description = "Region for regional resources."
}
variable "network_name" {
type = string
description = "Name of the VPC the database is reached from."
}
variable "network_self_link" {
type = string
description = "Self link of that VPC."
}
variable "app_service_account_email" {
type = string
description = "Service account that connects to the database."
}
variable "deletion_protection" {
type = bool
description = "Whether the instance is protected from deletion."
default = true
}
module "private_access" {
source = "terraform-google-modules/sql-db/google//modules/private_service_access"
version = "28.3.0"
project_id = var.project_id
vpc_network = var.network_name
prefix_length = 20
}
module "database" {
source = "terraform-google-modules/sql-db/google//modules/postgresql"
version = "28.3.0"
project_id = var.project_id
region = var.region
name = "workload"
random_instance_name = true
database_version = "POSTGRES_16"
edition = "ENTERPRISE"
tier = "db-custom-2-7680"
availability_type = "REGIONAL"
disk_autoresize = true
deletion_protection = var.deletion_protection
deletion_protection_enabled = var.deletion_protection
ip_configuration = {
ipv4_enabled = false
private_network = var.network_self_link
ssl_mode = "ENCRYPTED_ONLY"
}
backup_configuration = {
enabled = true
start_time = "02:00"
point_in_time_recovery_enabled = true
retained_backups = 14
retention_unit = "COUNT"
}
database_flags = [
{
name = "cloudsql.iam_authentication"
value = "on"
}
]
db_name = "app"
enable_default_user = false
iam_users = [
{
id = "app"
email = var.app_service_account_email
}
]
module_depends_on = [module.private_access.peering_completed]
}
```