Skip to content

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?

  1. Amap(string) with a default of { retention_days = "30" }
  2. Bobject({ name = string, retention_days = optional(number, 30) })
  3. Cobject({ name = string, retention_days = number }) with nullable = true
  4. 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.

Question 2 · choose 1

A variable holds tags such as Owner = "team-a" and CostCenter = "1234", looked up by name. Which type fits?

  1. Alist(string)
  2. Bmap(string)
  3. Cset(string)
  4. Dtuple([string, string])
Show the answer and why
  • Alist(string)

    Incorrect

    A list is positional; tags are looked up by name.

  • Bmap(string)

    Correct

    A map holds values of one type identified by string keys.

  • Cset(string)

    Incorrect

    A set has no keys.

  • Dtuple([string, string])

    Incorrect

    A tuple is a fixed positional sequence.

Key-value pairs of one type are a map.

Question 3 · choose 1

A variable is declared with type = map(string), and a caller passes { port = 8080, debug = true }. What does Terraform do?

  1. ARejects the value, because every value must already be a string
  2. BStores the values as number and bool inside the map
  3. CConverts the values to the strings "8080" and "true"
  4. DConverts the map to an object type
Show the answer and why
  • ARejects the value, because every value must already be a string

    Incorrect

    Automatic conversion applies where possible.

  • BStores the values as number and bool inside the map

    Incorrect

    A map(string) holds only strings.

  • CConverts the values to the strings "8080" and "true"

    Correct

    Terraform converts values to the required type when a conversion exists, and numbers and bools convert to strings.

  • DConverts the map to an object type

    Incorrect

    The declared type is used; the value is converted to it.

Type constraints convert compatible values rather than rejecting them.

Question 4 · choose 1

A variable has type object({ name = string }). A caller passes { name = "api", owner = "team-a" }. What happens to owner?

  1. AIt is kept and can be read as var.settings.owner like a declared one
  2. BTerraform reports an error for the unknown attribute
  3. CIt is discarded during conversion to the object type
  4. DIt becomes a separate variable named owner
Show the answer and why
  • AIt is kept and can be read as var.settings.owner like a declared one

    Incorrect

    Undeclared attributes do not survive the conversion.

  • BTerraform reports an error for the unknown attribute

    Incorrect

    Extra attributes are dropped, not rejected.

  • CIt is discarded during conversion to the object type

    Correct

    A larger object can convert to an object type that has fewer attributes; the extra attributes are discarded.

  • DIt becomes a separate variable named owner

    Incorrect

    Attributes never become variables.

Object types define what a module will read; anything else is dropped.

Question 5 · choose 1

A variable is declared with type = list(any). Which statement about the value it accepts is true?

  1. AAny element type is accepted, but all must convert to one type
  2. BEach element can be a different type that stays as given
  3. CThe value may also be a map, which Terraform turns into a list
  4. DTerraform skips type checking entirely
Show the answer and why
  • AAny element type is accepted, but all must convert to one type

    Correct

    any is a placeholder that Terraform replaces with a single concrete type that all elements can convert to.

  • BEach element can be a different type that stays as given

    Incorrect

    A list has one element type; any is resolved to a single type.

  • CThe value may also be a map, which Terraform turns into a list

    Incorrect

    list(any) still requires a list-like value.

  • DTerraform skips type checking entirely

    Incorrect

    The collection type is still enforced.

any means "Terraform will pick one type", not "anything goes per element".

Question 6 · choose 1

A list ["a", "b", "a"] is passed to toset(). What is the result?

  1. AA set containing "a", "b" and "a"
  2. BAn error, because sets cannot be built from lists with duplicates
  3. CA list sorted to ["a", "a", "b"]
  4. DA set containing "a" and "b"
Show the answer and why
  • AA set containing "a", "b" and "a"

    Incorrect

    Sets cannot contain duplicates.

  • BAn error, because sets cannot be built from lists with duplicates

    Incorrect

    Duplicates are discarded silently.

  • CA list sorted to ["a", "a", "b"]

    Incorrect

    toset returns a set, not a sorted list.

  • DA set containing "a" and "b"

    Correct

    When a list is converted to a set, duplicate values are removed.

Converting to a set deduplicates, which is why toset is common before for_each.

Practise domain 4 →Practise all domains →