Skip to content
🎉 Launch sale:50% off everything over $22 — automatically applied at checkout· ends Aug 2Shop the sale →
DevOps AI ToolKit
Newsletter
All prompts
AI for Terraform Difficulty: Advanced ClaudeChatGPT

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

More Terraform prompts & error guides

Browse every Terraform prompt and troubleshooting guide in one place.

Free download · 368-page PDF

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.