Lesson 7 of 9 · 25m
CI/CD and Review Workflows for Terraform
Design pipelines where plans are reviewable, applies are controlled, and environment promotion is deliberate instead of accidental.
Terraform automation fails in two opposite ways. In the first, every pull request magically applies to production and one bad merge becomes an outage. In the second, plans run nowhere consistently and engineers apply from laptops with god-mode credentials. A good team workflow puts review and evidence between those extremes.
The basic pipeline shape
For each pull request touching Terraform:
terraform fmt -check
terraform init -backend=false
terraform validate
terraform plan -input=false -out=tfplanPublish the human-readable plan as a PR artifact or comment. Require review for meaningful changes. Apply only after merge, from a controlled runner, with locked state and least-privilege credentials. The important boundary is psychological as much as technical: authors propose, automation records, reviewers approve, controlled runners execute.
Plans must correspond to real backends
Initializing with -backend=false is fine for validation, but a useful plan needs real backend and provider context. If CI cannot read production state, it cannot produce a production plan. This is why many teams use short-lived cloud credentials through OIDC federation rather than copying long-lived keys into CI secrets.
permissions:
id-token: write
contents: readA GitHub Actions workflow can exchange that OIDC token for an AWS role. Scope roles by repository, branch, and environment. A pull request from a fork should never receive production write credentials.
Promotion beats parallel improvisation
Deploy lower environments automatically if you want fast feedback, but require explicit approval or release promotion for production. Importantly, use the same module versions and configuration shape across the path. If staging is a fantasy environment, passing staging proves little.
A practical sequence is:
- PR plan and policy checks
- merge to main
- apply staging
- smoke tests or drift checks
- manual production approval
- production apply and post-apply verification
Apply output is operational evidence
Store plans and apply logs for the retention period your change process requires. They answer "what did Terraform believe it was doing?" when later debugging. Do not post full output publicly if it may contain sensitive attribute values; redaction is easier before the artifact leaves the runner.
Field notes
- Use
-input=falsein automation; a hanging prompt is a broken pipeline, not a control. - Concurrent applies to the same state should serialize by design, not because the lock eventually errors.
- Review plans for identity changes and replacements; counting added resources is not review.
- A pipeline everyone bypasses in emergencies needs an emergency lane, not moralizing documentation.
Was this lesson helpful?