Interview Prep/Terraform/Variables, Locals, Outputs and Types

How does `optional()` work in object types?

IntermediateConceptVariables, Locals, Outputs and Types
Answer

Since 1.3 you can mark attributes optional and give defaults.

variable "db" {
  type = object({
    engine  = string
    size_gb = optional(number, 50)
    backup  = optional(bool, true)
  })
}

Callers can omit size_gb and backup; without a default an omitted optional attribute is null.

All Terraform questions Roadmap: Terraform

More on Variables, Locals, Outputs and Types