Testing and CI for Terraform¶
Status: AI draft, 2026-11-16. Not yet verified by me. Written from the Terraform and tool documentation; nothing was run.
In one line¶
Check Terraform in layers: fmt and validate, a linter (TFLint), a misconfiguration scanner (Trivy or Checkov), terraform test and preconditions for logic, then in CI plan on every pull request, review the plan, and apply only the saved plan on merge, with a protected environment, approval for production, and short-lived OIDC credentials instead of stored keys.
The plain idea¶
Infrastructure code fails in expensive ways: a wrong CIDR, a public bucket, a database replaced. Each kind of mistake is cheapest to catch at a different point:
- Typos and syntax: before the commit.
- Bad practice and insecure settings: in the pull request, without touching AWS.
- What will actually change: the plan, read by a person.
- Who may change production: the pipeline's permissions and approvals.
How it works¶
The checks, cheapest first¶
| Check | Catches | Needs AWS? |
|---|---|---|
terraform fmt -check -recursive |
Formatting drift | No |
terraform validate |
Syntax, wrong types, missing arguments | No (after init -backend=false) |
| TFLint with the AWS ruleset | Invalid instance types, deprecated syntax, unused declarations | No |
Trivy (trivy config) or Checkov |
Misconfigurations: public S3, unencrypted volumes, open security groups (Quality and security gates) | No |
Variable validation, precondition, postcondition |
Bad inputs and broken assumptions, at plan or apply | Partly |
terraform test |
Module logic: run blocks with assertions, command = plan (no resources) or apply (real, temporary resources), optional mocked providers |
Only for apply runs |
terraform plan |
The real difference against real state | Yes (read) |
| Policy on the JSON plan (OPA, Sentinel, a script) | Forbidden actions: deleting databases, opening 0.0.0.0/0 |
No (reads the plan) |
A terraform test file, using a plan-only run:
# tests/naming.tftest.hcl
variables {
environment = "dev"
}
run "bucket_name_has_environment" {
command = plan
assert {
condition = aws_s3_bucket.uploads.bucket == "sample-app-dev-uploads"
error_message = "Bucket name must include the environment."
}
}
The pipeline¶
flowchart TD
PR["Pull request"] --> S["fmt, validate, TFLint,<br/>Trivy or Checkov"]
S --> P["terraform plan -out=tfplan<br/>with a read-only role (OIDC)"]
P --> C["Plan posted on the pull request,<br/>policy check on the JSON plan"]
C --> R["Human review and merge"]
R --> A["Apply job: protected environment,<br/>approval for prod, deploy role (OIDC)"]
A --> X["terraform apply tfplan"]
- Plan on pull request, apply on merge: reviewers approve a specific plan, and only the main branch can change infrastructure.
- Apply the saved plan: if the state changed since, apply fails as stale instead of doing something unreviewed. If the plan was made before the merge, many teams re-plan after merge and require approval of that plan for production.
- Credentials: the pipeline assumes IAM roles through OIDC; the plan role can only read, the apply role can write, and only jobs from the protected branch and environment can assume it (OIDC from pipelines to AWS).
- One state at a time: a concurrency group per state stops two pipelines racing for the lock (GitHub Actions in depth).
- Protect the state: only the apply role can write the state bucket; versioning on (Terraform state).
pre-commit hooks¶
The pre-commit framework runs checks before each commit on the developer's machine; the community pre-commit-terraform hooks wrap fmt, validate, TFLint, Trivy and docs generation. They are a convenience: CI must run the same checks, because hooks can be skipped.
A concrete example¶
A GitHub Actions setup for envs/prod/network:
- On pull request:
fmt -check,validate, TFLint,trivy config .; thenplan -out=tfplanwith roleterraform-plan(read-only) via OIDC;terraform show -no-color tfplanposted as a comment; a script fails the job if the JSON plan deletes anyaws_db_instance. - On merge to
main: the apply job targets theproductionenvironment, which requires a platform engineer's approval, assumesterraform-apply, plans again, and applies that saved plan. - Nightly:
plan -detailed-exitcodeper state for drift (Import, moved and drift).
Why not just...?¶
- ...let engineers apply from laptops? No review, no audit trail, credentials with write access on many machines, and the plan they saw may not match the code in Git. Laptops plan; the pipeline applies.
- ...rely on the scanner alone? Scanners catch known bad settings, not "this replaces the NAT gateways". Plans still need a human or a policy that looks at actions.
- ...run
terraform testwithapplyfor every module on every pull request? Real resources cost time and money and can leak if a run fails mid-way. Usecommand = planand mocks for most tests; runapplytests on a schedule in a sandbox account with cleanup.
Commands to know¶
These were not run for this page.
| Command | What it does |
|---|---|
terraform fmt -check -recursive |
Fails if any file is not formatted. |
terraform init -backend=false && terraform validate |
Validate without touching the real backend. |
tflint --init && tflint |
Install the configured rule plugins, then lint. |
trivy config . |
Scan the folder's IaC for misconfigurations. |
checkov -d . |
The same with Checkov. |
terraform test |
Run .tftest.hcl files in the module. |
terraform show -json tfplan |
The plan as JSON, for policy checks. |
pre-commit run --all-files |
Run the configured hooks on everything. |
How it breaks¶
| Symptom | Layer | Check | Likely cause |
|---|---|---|---|
Apply job fails Saved plan is stale |
Saved plan | Time between plan and apply | Another apply changed the state; plan again |
| Two pipelines fail on the state lock | Concurrency | Pipeline runs | No concurrency limit per state |
Plan works in CI, apply fails AccessDenied |
IAM | Which role the job assumed | Apply job used the read-only role, or the trust policy rejects the branch or environment |
| Scanner blocks a deliberate setting | Policy | Rule ID | Allowed exception not documented; add an inline skip with a reason |
terraform test leaves resources behind |
Test cleanup | Sandbox account | An apply run failed mid-way; run tests in a sandbox with periodic cleanup |
Interview questions¶
1. Describe a CI/CD pipeline for Terraform.
Answer
- Pull request: format, validate, lint, security scan, plan with a read-only role, plan posted for review, policy checks.
- Merge: apply the reviewed (or re-planned and approved) saved plan with a write role, in a protected environment with approval for production.
- Credentials through OIDC; one concurrent run per state.
2. Why apply a saved plan, and what if it is stale?
Answer
- It guarantees the apply does what was reviewed.
- If the state changed since, Terraform refuses it as stale.
- Plan again and review again; do not fall back to a fresh unreviewed apply.
3. What do TFLint, Trivy and Checkov each add over terraform validate?
Answer
validate: syntax and types only.- TFLint: provider-aware mistakes (invalid instance types) and style rules.
- Trivy and Checkov: security misconfigurations against known rules, such as public buckets or open ports.
4. What is terraform test and when would you use command = plan?
Answer
- Test files with run blocks and assertions against a module.
command = planchecks logic without creating anything: fast and free.applyruns create real resources and clean them up; keep them for scheduled tests in a sandbox.
5. How do you make sure only reviewed code can change production?
Answer
- Only the pipeline's apply role can write to production and to the state bucket.
- That role trusts only jobs from the protected branch and environment through OIDC conditions.
- Production environment requires approval; laptops have read-only access.
6. A plan in CI shows changes nobody made in this pull request. What do you do?
Answer
- It is drift, a provider or module change, or someone else's merged change not yet applied.
- Run a refresh-only plan and check the lock file and module refs.
- Resolve it separately before approving this pull request's apply.
Try it¶
Without Terraform:
- Write the GitHub Actions job list for the pipeline above, with which role each job assumes and which branch or environment it may run from (GitHub Actions basics).
- Write a
.tftest.hclthat checks a subnet module returns three subnets when given three zones. - List five Trivy or Checkov findings you would expect in a first scan of a hand-written S3 bucket resource.
Zoom out¶
Terraform CI is the general pipeline pattern (build, test, scan, review, deploy with approval) applied to infrastructure, with the plan as the artifact under review (Concepts). The same OIDC and environment protections secure application deploys (Pipeline security).