ADR-026: Terraform as the Infrastructure-as-Code Layer
Context
The GCP estate — Cloud Run services, runtime service accounts and IAM, Artifact Registry, Secret Manager secrets and bindings, log-based monitoring and alerting (ADR-011), budgets, the billing export dataset, and every runtime feature flag (ADR-016) — must be reproducible (dev and prod structurally the same system), reviewed (infrastructure changes flow through the same PR trail as code, with a human-approvable diff before anything is applied), and keylessly applied by CI via short-lived OIDC credentials. Eight other ADRs assume "a Terraform variable" or "a Terraform apply" as their control plane, but the tool choice itself was never recorded — this ADR closes that gap retroactively.
Decision
Terraform is the sole infrastructure-as-code layer.
* Layout & state isolation: a shared cloud_run module (main.tf, monitoring.tf, finops_budget.tf) defines the service shape once; terraform/environments/{dev,prod} roots instantiate it with their own variables, each with an isolated GCS state prefix (env/dev, env/prod) so one environment's plan can never touch the other's resources. Project-shared resources (artifact_registry.tf, billing_export.tf, logging_madrid.tf, project_services.tf, firebase_hosting.tf) are declared once, in the prod root; each environment owns its own Firestore database (prod/firestore.tf for (default), dev/firestore.tf for portfolio-dev), and bootstrap resources live in terraform/bootstrap/.
* Pinned toolchain: required_version >= 1.7 in prod (for_each import blocks), hashicorp/google and google-beta ~> 8.5 (lockfiles pin exact versions, kept current by Dependabot), and committed .terraform.lock.hcl files with cross-platform provider checksums — CI and local machines resolve identical providers, and the provider cache keys on the lockfile (ADR-012). CI pins the Terraform version.
* Pipeline integration: the PR terraform plan runs after Node CI and only when backend paths changed; pushes to main apply dev when backend or frontend paths changed (or on manual dispatch); a v* tag, created by publishing the release draft, applies prod with the promoted image digest (ADR-008, ADR-012, ADR-021).
* Drift and adoption: a weekly refresh-only plan (drift.yml) fails if live infrastructure no longer matches state, and import blocks adopt hand-made resources such as the production error-log alert policy and the Firestore database with its TTL policies.
* Runtime configuration flows through Terraform: feature flags and service parameters are Terraform variables materialized as Cloud Run env vars, so every flip is a reviewed infrastructure change pinned to a revision (ADR-016). Secret values never enter Terraform — it binds Secret Manager references only (ADR-009).
Deliberate exceptions: the deployer service account and WIF pool are bootstrapped out-of-band, and the billing export toggle has no public API.
Amended 2026-10-31: the Firestore (default) database (in prod) and the isolated portfolio-dev database (in dev), their eight TTL policies and their security rules moved into Terraform (firestore.tf), adopted with import blocks so nothing is replaced. Delete protection is now on, Terraform abandons rather than deletes the database, and any plan that would replace it fails. Point-in-time recovery stays off: everything stored is a cache, session or counter with a TTL of 90 days or less. Removing a TTL policy fails the apply instead of silently switching it off. The rules deny every client request, since all access goes through the Admin SDK on the server, which bypasses rules. The deployer gained index and rules admin roles plus a custom role that can change database settings but cannot read data or delete the database.
Consequences
Benefits:
* One review trail for application and infrastructure; the plan diff on every PR that touches the backend or Terraform is the approval artifact.
* Environments converge by construction — the shared module means dev genuinely rehearses prod.
* Disaster recovery is terraform apply: the whole estate is reconstructible from the repository plus the state bucket.
Trade-offs:
* The state bucket is a critical singleton — mitigated by GCS versioning and per-env prefixes bounding the blast radius.
* Provider upgrades are their own maintenance stream, and Trivy scans the Terraform definitions for misconfiguration on every PR (ADR-020).
* Bootstrap circularity: the deployer service account and WIF pool that let CI run Terraform must be created once out-of-band (docs/deployer-sa-bootstrap.md).
Amended 2026-10-31 (ADR-043): the bootstrap is now its own root module, terraform/bootstrap/, applied by a human. State bucket versioning, relied on above, was in fact off until 2026-10-30; that stack now keeps it on.
Alternatives considered
1. gcloud CLI scripts: Rejected — imperative, no plan/diff to review, silently drifts, and every script grows environment conditionals the module system solves declaratively.
2. Pulumi: Rejected — a second language runtime in the toolchain and a thinner GCP resource surface than the mature Google provider, for no capability this estate needs.
3. GCP Config Connector: Rejected — requires a standing GKE cluster to host the controllers, violating the zero-standby-cost baseline (ADR-001).
4. Console click-ops: Rejected outright — unreviewable, unreproducible, and incompatible with keyless CI-only privileged access.