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)
}
A{ team = "core", cost_center = "42" }
B{ team = "core" }
CAn error about the two tags objects having different attribute types
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).
What does this expression return?
[for i, z in sort(["10", "9", "100"]) : "${i}:${z}"]
A["0:10", "1:100", "2:9"]
B["0:9", "1:10", "2:100"]
C["1:10", "2:100", "3:9"]
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.