004 · Domain 4: Terraform configuration · 13% of the exam
Task 4.5: Write dynamic configuration using expressions and functions
Conditionals, for expressions, splats, dynamic blocks, string templates, and built-in functions such as lookup, merge, flatten, try and templatefile; testing them in terraform console.
Study it
Expressions and functions: conditionals, for, dynamic blocks, templates
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
var.users is a list of objects with name and is_admin attributes. A local value must be a map from each admin's name to that user object. Which expression builds it?
A[ for u in var.users : u.name => u if u.is_admin ]
B{ for u in var.users : u if u.is_admin }
C{ for u in var.users : u.name => u if u.is_admin }
Dlookup(var.users, "is_admin", true)
Show the answer and why
A[ for u in var.users : u.name => u if u.is_admin ]
Incorrect
Square brackets produce a list, which cannot use the key => value form.
B{ for u in var.users : u if u.is_admin }
Incorrect
A map-producing for expression needs a key => value pair.
C{ for u in var.users : u.name => u if u.is_admin }
Correct
A for expression in braces produces a map with key => value, and the if clause filters elements.
Dlookup(var.users, "is_admin", true)
Incorrect
lookup reads one key from a map; it does not filter or transform a list.
Brackets make lists, braces make maps, and if filters either.
var.networks maps each network to a list of subnets. A resource must have one instance per network-subnet pair. Which function turns the nested lists into one list to build the for_each map?
Amerge
Bzipmap
Ccompact
Dflatten
Show the answer and why
Amerge
Incorrect
merge combines maps by key.
Bzipmap
Incorrect
zipmap builds a map from a list of keys and a list of values.
Ccompact
Incorrect
compact removes null and empty strings from a list.
Dflatten
Correct
flatten replaces nested lists with their elements, producing a single flat list.
Nested structures for for_each are usually flattened with a for expression and flatten, then turned into a map.
An instance's user_data is a shell script that needs the database address and a port inserted. The script lives in its own file. Which function renders it?
Atemplatefile, with a map of variables
Bfile, which replaces variables automatically
Cjsonencode, which encodes the script with its variables
Dformat, which reads the file and fills in placeholders
Show the answer and why
Atemplatefile, with a map of variables
Correct
templatefile reads a file and renders it as a template with the given variables.
Bfile, which replaces variables automatically
Incorrect
file returns the contents unchanged.
Cjsonencode, which encodes the script with its variables
Incorrect
jsonencode produces JSON text.
Dformat, which reads the file and fills in placeholders
Incorrect
format formats a string argument; it does not read files.
Keep templates in files and render them with templatefile.
An engineer wants to try cidrsubnet() with different arguments before using it in configuration. Which approach fits, and can a custom function be defined if the built-ins fall short?
AUse terraform validate; custom functions go in a functions block
BUse terraform plan -target; custom functions are defined in locals
CUse terraform fmt; custom functions are written in Go next to the configuration
DUse terraform console; only built-in and provider-defined functions exist
Show the answer and why
AUse terraform validate; custom functions go in a functions block
Incorrect
There is no functions block for user-defined functions.
BUse terraform plan -target; custom functions are defined in locals
Incorrect
Locals hold values, not function definitions.
CUse terraform fmt; custom functions are written in Go next to the configuration
Incorrect
Configurations cannot define functions.
DUse terraform console; only built-in and provider-defined functions exist
Correct
You can experiment with functions in terraform console; the language does not support user-defined functions.
console is the scratch pad for expressions and functions.