004 · Domain 4: Terraform configuration · 13% of the exam
Task 4.4: Understand and use complex types
Lists, sets, tuples, maps and objects, optional object attributes with defaults, type conversion, and choosing the right type constraint for a variable.
Study it
Complex types: lists, sets, tuples, maps, objects and optional attributes
Lesson coming
Sample questions
Try each one before opening the answer. Every option is explained, with the HashiCorp documentation page that proves it.
Question 1 · choose 1
A module takes a settings object. Callers must always give a name, may give a retention_days number, and should get 30 when they leave it out. Which type constraint expresses this?
Amap(string) with a default of { retention_days = "30" }
Bobject({ name = string, retention_days = optional(number, 30) })
Cobject({ name = string, retention_days = number }) with nullable = true
Dtuple([string, number]) with a default of ["", 30]
Show the answer and why
Amap(string) with a default of { retention_days = "30" }
Incorrect
A map has no required keys, and a variable default would be replaced entirely by the caller's map.
Bobject({ name = string, retention_days = optional(number, 30) })
Correct
optional() marks an object attribute as optional, and its second argument is the default used when the caller omits it.
Cobject({ name = string, retention_days = number }) with nullable = true
Incorrect
nullable allows the whole variable to be null; it does not make one attribute optional.
Dtuple([string, number]) with a default of ["", 30]
Incorrect
A tuple has no named attributes, and a variable default is replaced as a whole when the caller sets the variable, so no per-attribute default applies.
optional(type, default) is the way to build object inputs with sensible defaults per attribute.