1---2name: terraform-patterns3description: Terraform reference for designing and reviewing infrastructure changes — module structure, state and environments, safe plans, capacity and scaling settings, networking, secrets, and change safety. Use when a feature's HLD or LLD changes Terraform-managed infrastructure.4allowed-tools: Read, Grep, Glob, Bash5tags: [stack:terraform, patterns, infra]6metadata:7 source: https://skillmd.com/skills/keyvaluesoftwaresystems/terraform-patterns8---910# terraform-patterns1112## Read the existing layout first1314- Find the root modules (directories with a backend/provider block) and which environment each15 one deploys (`envs/<env>`, workspaces, or `*.tfvars` per environment). A change usually lands in16 a shared module plus a per-environment value.17- Find the modules the feature's services already use (compute, database, queue, cache, DNS) and18 extend those instead of adding a parallel resource.19- Note the state backend and locking (S3 + DynamoDB, GCS, Terraform Cloud). Never design a change20 that needs manual state edits.2122## Designing a change2324- **New resources go in a module** with typed variables, defaults that are safe for production,25 and outputs the application needs (endpoints, ARNs/ids, secret names) — never hard-coded ids.26- **Capacity is a variable per environment**: instance counts, autoscaling min/max and target,27 database instance class and storage, queue retention, cache node size. State the production28 value in the HLD and why it meets the PRD's targets.29- **Networking**: new services join the existing VPC/subnets and security groups; open only the30 ports and sources the interface needs; private by default, public only behind the existing31 load balancer or gateway.32- **Secrets** come from the existing secret manager (Secrets Manager, Vault, SSM, Secret33 Manager) by reference; never in variables, tfvars or state outputs marked non-sensitive.34- **Data stores**: enable backups and point-in-time recovery, deletion protection, encryption at35 rest; size connections for the instance count times pool size.36- **IAM**: least privilege per service role; one role per workload, no wildcard actions on37 production resources.38- **Tags/labels**: follow the repository's tagging convention (owner, environment, cost centre).3940## Change safety4142- Read the plan for every environment: any `destroy` or `forces replacement` on a stateful43 resource (database, volume, bucket, queue) is a design problem, not a detail — use44 `create_before_destroy`, `moved` blocks, or a migration step.45- Renames use `moved {}` blocks; never delete-and-recreate to rename.46- Roll out to non-production first; production changes that cause downtime need a window or a47 blue/green path stated in the HLD.48- Pin provider and module versions; upgrade them in their own change.4950## Verifying5152- `terraform fmt -check`, `terraform validate`, and a plan per environment in CI; a policy or lint53 step (tflint, checkov, tfsec) when the repository has one.54- The LLD for an infrastructure repository names each resource added or changed, its per-environment55 values, and the plan output it expects (adds/changes, no destroys of stateful resources).