Skip to content

002 · Domain 1: Scalable Terraform Configuration Design · 20% of the exam

Task 1.5: Predict the value of an expression that uses a Terraform function

Reading a function call and knowing its result: collection functions such as merge, flatten, setproduct, zipmap and lookup, string and numeric functions, type conversion, and try and can for values that might fail.

Study it

  • Predicting function results: collections, strings, numbers, try and can

    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

What is the value of local.merged.tags? locals { base = { env = "prod", tags = { team = "core" } } extra = { tags = { cost_center = "42" } } merged = merge(local.base, local.extra) }

  1. A{ team = "core", cost_center = "42" }
  2. B{ team = "core" }
  3. CAn error about the two tags objects having different attribute types
  4. D{ cost_center = "42" }
Show the answer and why
  • A{ team = "core", cost_center = "42" }

    Incorrect

    That would be a deep merge; merge does not combine nested objects.

  • B{ team = "core" }

    Incorrect

    merge gives precedence to later arguments, not earlier ones.

  • CAn error about the two tags objects having different attribute types

    Incorrect

    Arguments of different types are allowed; the result is an object that follows the merging rules.

  • D{ cost_center = "42" }

    Correct

    When two arguments define the same key, the later one takes precedence, and the whole value under that key is replaced.

merge works one level deep: the later tags value replaces the earlier one entirely. To combine nested maps, merge them explicitly, for example merge(local.base.tags, local.extra.tags).

Question 2 · choose 1

What does this expression return? [for i, z in sort(["10", "9", "100"]) : "${i}:${z}"]

  1. A["0:10", "1:100", "2:9"]
  2. B["0:9", "1:10", "2:100"]
  3. C["1:10", "2:100", "3:9"]
  4. D["0:100", "1:10", "2:9"]
Show the answer and why
  • A["0:10", "1:100", "2:9"]

    Correct

    sort orders strings lexicographically by code point, so "10" < "100" < "9", and the two-symbol form gives the zero-based index first.

  • B["0:9", "1:10", "2:100"]

    Incorrect

    That is numeric order; sort compares the values as strings.

  • C["1:10", "2:100", "3:9"]

    Incorrect

    List indexes in a for expression start at zero.

  • D["0:100", "1:10", "2:9"]

    Incorrect

    Lexicographic order compares character by character, so "10" sorts before "100", not after it.

sort works on strings, so numeric-looking strings sort by characters, not value. In a for expression over a list, the first of two symbols is the index.

Question 3 · choose 1

What does element(["a", "b", "c"], 3) return?

  1. AAn index out of range error
  2. B"c"
  3. Cnull
  4. D"a"
Show the answer and why
  • AAn index out of range error

    Incorrect

    element wraps an index greater than the length instead of failing.

  • B"c"

    Incorrect

    "c" is at index 2; index 3 wraps to 0.

  • Cnull

    Incorrect

    element returns an element of the list; it does not return null.

  • D"a"

    Correct

    The index is taken modulo the length of the list, so 3 becomes 0.

Use the index syntax list[i] in most cases; element is for the wrap-around behavior, and -1 selects the last element.

Question 4 · choose 1

What does coalesce(null, "", "fallback") return?

  1. A""
  2. B"fallback"
  3. Cnull
  4. DAn error: null cannot be passed to coalesce
Show the answer and why
  • A""

    Incorrect

    coalesce skips empty strings as well as nulls.

  • B"fallback"

    Correct

    coalesce returns the first argument that is neither null nor an empty string.

  • Cnull

    Incorrect

    A null argument is skipped, not returned.

  • DAn error: null cannot be passed to coalesce

    Incorrect

    null is a valid argument; coalesce simply moves past it.

coalesce is handy for "first non-empty value" defaults; with a list of candidates, expand it with coalesce(list...).

Question 5 · choose 1

What does flatten([["a", "b"], [], [["c"]]]) return?

  1. A["a", "b", "c"]
  2. B["a", "b", ["c"]]
  3. C["a", "b", "", "c"]
  4. DAn error
Show the answer and why
  • A["a", "b", "c"]

    Correct

    flatten replaces nested lists with their contents, recursively, so the empty list adds nothing and the doubly nested "c" is lifted too.

  • B["a", "b", ["c"]]

    Incorrect

    Directly nested lists inside nested lists are flattened as well.

  • C["a", "b", "", "c"]

    Incorrect

    An empty list contributes no elements; it does not become an empty string.

  • DAn error

    Incorrect

    flatten handles any depth of directly nested lists.

flatten is often paired with nested for expressions to build one element per child object for for_each; lists inside maps are not flattened.

Question 6 · choose 1

What does zipmap(["a", "b", "a"], [1, 2, 3]) return?

  1. AAn error
  2. B{ a = 1, b = 2 }
  3. C{ a = 3, b = 2 }
  4. D{ a = [1, 3], b = 2 }
Show the answer and why
  • AAn error

    Incorrect

    Duplicate keys are allowed; one of the values wins.

  • B{ a = 1, b = 2 }

    Incorrect

    The first value for a repeated key does not win.

  • C{ a = 3, b = 2 }

    Correct

    When a key appears more than once, the value with the highest index is used.

  • D{ a = [1, 3], b = 2 }

    Incorrect

    zipmap does not group values; that is what grouping mode in a for expression does.

zipmap pairs keys and values by position. Make keys unique before calling it when every value matters.

Practice domain 1 →Practice all domains →