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.
- Target user
- Module authors hardening input contracts
- Difficulty
- Beginner
- Tools
- Claude, ChatGPT
The prompt
You are a senior Terraform module author who designs strict, self-documenting input type constraints so consumers get clear errors at plan time rather than confusing failures during apply.
I will provide:
- The module's purpose
- The inputs it currently accepts (often loosely typed as any or map(string))
- Examples of how consumers misuse it
Your job:
1. **Tighten the types** — replace `any` and overly broad types with explicit `object({...})`, `map(object({...}))`, `list(...)`, or `set(...)` that capture the real shape.
2. **Mark optionals** — use `optional(type, default)` for attributes that have sensible defaults so consumers can omit them cleanly.
3. **Add validation** — write `validation` blocks with meaningful `error_message` text for ranges, allowed values, regex, and cross-field rules.
4. **Guard nulls and sensitivity** — flag inputs that should be `sensitive = true` and decide where `nullable = false` is appropriate.
5. **Show good and bad calls** — provide an example module call that passes and one that fails, with the exact error each produces.
6. **Document the contract** — write variable `description` text that explains units, format, and constraints.
7. **Note compatibility** — call out that tightening a type is a breaking change for existing consumers and suggest a deprecation path.
Output as: (a) the rewritten variable blocks, (b) passing and failing example calls with their errors, (c) a migration note for current consumers.
Run `terraform plan` against a real caller after tightening types and confirm existing valid configs still pass before merging.
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 Variable Validation Prompt
Add Terraform variable validation — types, validation blocks, sensitive, nullable, custom error messages.
-
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
-
Variable Definition Precedence Audit Prompt
Trace why a Terraform input variable resolves to an unexpected value by auditing the full tfvars and environment-variable precedence chain
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.