Terraform Check Blocks & Post-Apply Assertions Prompt
Add `check` blocks with scoped data sources and assertions that validate live infrastructure after apply — endpoints reachable, TLS valid, DNS resolving — turning silent misconfigurations into loud plan/apply warnings.
- Target user
- Platform engineers who want guardrails beyond a green apply
- Difficulty
- Intermediate
- Tools
- Claude, ChatGPT
The prompt
You are a Terraform reliability engineer who treats a successful apply as necessary but not sufficient — you assert the resulting infrastructure actually works. I will provide: - The resources I create (load balancers, DNS records, certs, databases) - The invariants I care about (HTTPS 200, cert not expiring soon, DNS resolves to the LB) - Where this runs (CI vs local vs Terraform Cloud) Your job: 1. **When `check` beats `precondition`/`postcondition`** — explain the difference: check blocks emit warnings (non-fatal) and run on every plan/apply, ideal for continuous validation, while pre/postconditions are hard gates tied to a resource. Tell me which to use for each invariant. 2. **Scoped data sources** — write `check` blocks containing scoped `data` sources (e.g., `data "http"`) that probe the just-created endpoint. Show why scoping the data source inside the check avoids breaking plans when the target doesn't exist yet. 3. **Assertions** — author `assert` blocks with meaningful `condition` and `error_message` for: HTTP 200/expected body, TLS cert `>30d` to expiry, DNS A record matches the LB IP, DB port reachable. 4. **Handling not-yet-created** — show the pattern for first apply where the resource doesn't exist, so the check warns instead of erroring and blocking creation. 5. **CI behavior** — explain how warnings surface in `terraform plan` output and how to optionally fail CI on check warnings with a JSON-plan post-processor (since checks themselves don't fail apply). 6. **External health** — integrate a `data "external"` probe for things HTTP can't cover (e.g., a smoke test script), with timeouts and retries noted. 7. **Anti-patterns** — checks that flap, checks that depend on eventual consistency without retry, checks that duplicate monitoring (know where Terraform ends and your monitoring stack begins). Output: (a) 3-4 real `check` blocks for my resources, (b) the CI snippet that parses `terraform show -json` to fail on check warnings if I opt in, (c) guidance on which invariants belong in Terraform vs external monitoring. Bias toward: high-signal assertions, graceful first-apply behavior, and not reinventing my monitoring.
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 Check Block Scoped Data Source Prompt
Design `check` blocks that use scoped data sources to assert post-apply runtime health (endpoints, DNS, cert expiry) as non-blocking warnings instead of hard plan failures.
-
Resource Precondition and Postcondition Design Prompt
Add lifecycle precondition and postcondition blocks that fail a plan or apply early with clear messages instead of producing broken infrastructure
-
Terraform Variable Type Constraints Design Prompt
Design precise complex type constraints for module inputs using object, map, and optional() so misuse fails fast at plan time with clear errors.
-
Terraform Variable Validation Prompt
Add Terraform variable validation — types, validation blocks, sensitive, nullable, custom error messages.
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.