Packer HCL2 object variables support optional attributes
Introduced 2026-07-24 · score 0.99
HOW TO USE IT
When an object variable attribute may be omitted, declare it with `optional(<TYPE>, <DEFAULT>)`; Packer uses its default when the field is omitted.
variable "<name>" {
type = object({
<attribute> = optional(<TYPE>, <DEFAULT>)
})
}WHY THIS HELPS
When declaring a Packer object variable with attributes that may be omitted, assign defaults to those attributes with `optional()`.
REQUIREMENTS AND LIMITATIONS
- Version: v1.16.0
- optional(<TYPE>, <DEFAULT>): Marks an object type attribute as optional and supplies its default when omitted.
- Prerequisite: Use Packer v1.16.0 or later; this feature is stated in the v1.16.0 release notes.
- Omitted fields are supported when they have a default set.
FIRST-PARTY EVIDENCE
- https://github.com/hashicorp/packer/releases/tag/v1.16.0
- https://developer.hashicorp.com/packer/docs/templates/hcl_templates/variables
Check the installed version and the source before using it. Entry JSON