Engineering Note

Architectural rules for bounding Terraform blast radius, preventing plaintext state leaks, and separating read-only PR plans from OIDC-authenticated applies.

Bounding Blast Radius by Rate of Change

A single monolithic Terraform state file covering VPCs, IAM roles, databases, and application workloads turns routine service changes into high-risk operations. Every plan must refresh hundreds of unrelated API objects, lock contention blocks parallel deployments across teams, and a misconfigured destroy can impact foundational networking.

Partition root states along ownership and rate-of-change boundaries: place stable networking and identity foundations in a base layer, shared clusters and databases in a platform layer, and application-specific resources in service-owned states. Pass non-sensitive integration endpoints via AWS SSM Parameter Store or managed config stores rather than coupling IAM permissions with terraform_remote_state.

Why sensitive = true Does Not Protect State

Marking an input variable or output as sensitive = true only redacts its value from CLI stdout and CI logs. Terraform still serializes the plaintext value inside terraform.tfstate so it can detect future diffs.

Mitigate state exposure with defense in depth: store state in private, versioned remote backends with KMS encryption and native locking (such as S3 use_lockfile = true in Terraform 1.10+), keep short-lived tokens out of state using ephemeral blocks and write-only arguments (Terraform 1.10/1.11+), or adopt OpenTofu 1.7+ client-side state encryption.

HCL
terraform {
  required_version = ">= 1.10.0"

  backend "s3" {
    bucket       = "org-prod-tfstate"
    key          = "platform/ap-southeast-1/terraform.tfstate"
    region       = "ap-southeast-1"
    encrypt      = true
    use_lockfile = true
  }
}

Secretless OIDC with Split Plan and Apply Roles

Never store static cloud access keys in CI/CD repository secrets. Configure OpenID Connect (OIDC) workload identity federation so runners exchange a short-lived job token for temporary cloud credentials scoped to the repository, branch, and environment.

Enforce least privilege across pipeline stages by binding pull-request jobs to a read-only IAM role for terraform plan, and restricting the mutating apply role exclusively to protected main-branch runs gated by manual environment approval.

Related Engineering Notes

/production-ready-apis

Designing Production-Ready APIs

A comprehensive guide to designing resilient REST APIs: deterministic contracts, idempotency keys, RFC 7807 problem details, and defense-in-depth rate limiting.

/avoiding-n-plus-one

Avoiding N+1 Database Queries

Techniques for eliminating silent database query amplification in ORMs through query planning, batching, and join projection.

← Back to engineering notes