Interview Prep/Terraform/Resources Variables and Expressions

A module input has many optional settings with defaults. How do you type it?

IntermediateScenarioResources Variables and Expressions
Answer

Situation read → A flat list of 20 variables is painful; an untyped map is dangerous.

What I'd do → Use an object with optional() attributes and defaults.

variable "database" {
  type = object({
    engine         = string
    instance_class = optional(string, "db.t4g.medium")
    multi_az       = optional(bool, false)
    backup_days    = optional(number, 7)
  })
}

Why → Callers set only what differs, typos are rejected at plan, and defaults are visible in one place.

Interview tip → optional() with a default has been stable since Terraform 1.3.

All Terraform questions Roadmap: Terraform

More on Resources Variables and Expressions