Skip to content
CloudOps
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.
Newsletter

Free: the DevOps AI Incident-Triage Cheat Sheet

Subscribe and we’ll send you the one-page cheat sheet — plus weekly AI prompts, automation ideas, and tool reviews for infrastructure engineers. One email a week. No spam, unsubscribe anytime.

  • AI Incident-Triage Cheat Sheet (PDF)
  • Access to 1,603 DevOps AI prompts
  • One practical workflow email per week