Terraform Conftest Plan-JSON Gating Prompt
Wire conftest (OPA/Rego) into CI to evaluate the `terraform show -json` plan output, so policy violations fail the pipeline on the actual planned changes rather than on static config — with clear pass/warn/deny tiers and readable failure messages.
- Target user
- Platform engineers gating Terraform pipelines with policy-as-code
- Difficulty
- Intermediate
- Tools
- Claude, ChatGPT
The prompt
You are a senior Terraform/IaC engineer who knows that evaluating policy against the JSON plan (`resource_changes`) catches what static `.tf` scanning misses — computed values, counts, and the actual create/update/delete actions.
I will provide:
- The pipeline steps and where plan runs (and how I produce `plan.json`)
- The policy intents I want enforced (e.g. no public ingress, required tags, instance-size caps)
- Which intents are hard-deny vs. warn-only
Your job:
1. **Generate the plan JSON** — show the exact `terraform plan -out` then `terraform show -json` sequence and the shape of `resource_changes` your rules will query.
2. **Write the Rego** — author `deny`/`warn` rules against `input.resource_changes`, filtering on `.change.actions` (create/update/delete) and resource type/values; keep messages actionable.
3. **Handle plan quirks** — account for unknown ("known after apply") values, no-op changes, and resources behind `count`/`for_each` so rules don't false-positive or miss instances.
4. **Structure tiers** — separate `deny` (fail build) from `warn` (report only), and show the conftest invocation and exit-code handling for each.
5. **Test the policies** — write conftest unit tests with fixture plan JSON covering a passing plan, a denied plan, and an unknown-value edge case.
6. **Wire into CI** — give the pipeline step that runs conftest after plan and gates the apply, plus how violations surface in the PR.
Output as: (a) the plan-to-JSON commands, (b) the Rego policies with messages, (c) the conftest test fixtures, (d) the CI step with exit-code/tier handling, (e) notes on unknown-value and `for_each` edge cases.
Caution: policy passing on the plan is not a license to auto-apply — a human should still review the plan; treat conftest as a gate, not a substitute for plan review.
Run this prompt with AI
Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.
Related prompts
-
Terraform Backend State Lock Timeout Tuning Prompt
Tune Terraform state locking — lock-timeout, DynamoDB/backend contention, CI concurrency, and safe recovery from stuck locks.
-
Terraform Native Test Run Block Ordering Prompt
Sequence `run` blocks in a `.tftest.hcl` file so cheap plan-only assertions gate before expensive applies, state carries between runs correctly, and cleanup is reliable.
-
Terraform CLI Configuration (.terraformrc) Hardening Prompt
Audit and design a hardened CLI configuration file for provider mirrors, plugin cache, and credential helpers across dev and CI
-
Plan JSON Dependency Graph Export Analysis Prompt
Export and analyze the resource dependency graph from terraform graph and plan JSON to explain ordering and blast radius
More Terraform prompts & error guides
Browse every Terraform prompt and troubleshooting guide in one place.
Reading prompts? Get all 500 in one free PDF
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.