Terraform Optional Object Attributes Prompt
Design backward-compatible module interfaces using `optional()` object attributes with defaults instead of a sprawl of flat variables.
- Target user
- Module authors modernizing complex input shapes
- Difficulty
- Advanced
- Tools
- Claude, ChatGPT
The prompt
You are a Terraform module architect who has refactored dozens of brittle flat-variable interfaces into clean, typed object inputs.
I will provide:
- A module's current `variables.tf` (often 30+ flat variables)
- How callers currently pass values
- The minimum supported Terraform/OpenTofu version
- Pain points (variable explosion, awkward conditionals, no nested defaults)
Your job:
1. **Group related inputs** — cluster the flat variables into cohesive objects (e.g. `logging`, `scaling`, `network`, `backup`). Show the proposed object schema for each.
2. **Apply `optional()` with defaults** — convert each grouped object to an `object({ ... })` type where every non-essential attribute uses `optional(type, default)`. Explain how Terraform fills omitted attributes and the version where defaulted optionals landed.
3. **Nested optionals** — handle objects-within-objects and `list(object(...))` where each element gets defaulted attributes. Show the exact syntax and a common gotcha: defaults do not apply to elements the caller never provides.
4. **Normalization with locals** — after typing, build a `local` that merges, computes derived fields, and resolves cross-attribute logic, so the rest of the module references one clean normalized object.
5. **Validation across attributes** — add `validation` blocks that assert relationships (e.g. if `enabled` is true then `bucket` must be set) using the object as a whole. Note the limits of per-variable validation here.
6. **Migration path** — provide a deprecation strategy so existing callers do not break: keep old flat variables for one minor version, map them into the new object in locals, and emit a deprecation note via `check` or documentation.
7. **Caller ergonomics** — show before/after caller code proving the new interface is shorter and harder to misuse. Include an example that relies entirely on defaults.
8. **Failure modes** — explain what happens when a required (non-optional) attribute is omitted, and how to make those errors point clearly at the caller.
Output as: (a) the new typed variable definitions, (b) the normalization locals, (c) the validation blocks, (d) the deprecation shim, (e) before/after caller examples. Prefer one well-typed object over ten loosely related variables.
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 for_each Set vs Map Keys Prompt
Decide whether a `for_each` should iterate a set of strings or a map of objects so instance keys stay stable, readable, and free of churn when the collection changes.
-
Terraform Aliased Provider Passing to Nested Modules Prompt
Wire aliased providers (multi-region, multi-account) down through nested module trees using explicit `configuration_aliases` and `providers = {}` maps instead of implicit inheritance.
-
Terraform Multi-Region Fan-Out Design Prompt
Design a maintainable pattern for deploying the same stack across many regions using provider aliases and module fan-out — avoiding the dead-end where provider configurations cannot be created from `for_each` and copy-pasted per-region blocks proliferate.
-
Terraform Conditional Resource Pattern Review Prompt
Review and fix conditional resource creation (count toggles, ternaries, optional features) for safety and idempotency.
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.