Expressions, loops, and functions
Terraform 004 · Lesson 8 of 18 · 57:12
YouTube loads only after you press play. This chapter opens at its timestamp in the full course. Watch on YouTube ↗
Learning with these videos? Subscribe for new courses and exam-style questions.
Subscribe on YouTube (opens in a new tab)In this lesson
Prepare for the HashiCorp Terraform Associate (004) exam. In this lesson: conditional and for expressions, splat expressions, count and for_each, dynamic blocks, and built-in functions, tested with terraform console. Includes three original practice questions, with Bluefin Logistics, a fictional parcel delivery company, as the example.
Study notes
The text of this lesson, to read at your own pace. Information as of November 2026.
Twelve depots
Bluefin has twelve depots, and each one needs a bucket, a few firewall rules, and a name built from its city. Writing twelve copies of every block would be slow and fragile. This lesson covers expressions, count and for each, dynamic blocks, and built-in functions.
Expressions
Expressions compute values in a configuration, and you have already seen references and literal values. A conditional expression chooses between two values: a condition, a question mark, the value if true, a colon, and the value if false. Strings can include expressions with a dollar sign and curly braces, which is called interpolation. A for expression transforms a collection: in square brackets it builds a list, in curly braces it builds a map, and an if clause filters the elements. And a splat expression, with a star in square brackets, collects one attribute from every element of a list, like the IDs of all the instances.
count and for each
The count meta-argument creates several instances of a resource from one block: count equals three gives three instances, numbered from zero. Inside the block, count dot index gives the number of each instance, and its address ends with that index in square brackets. The for each meta-argument takes a map, or a set of strings, and creates one instance per element, with each dot key and each dot value inside the block. A block can use count or for each, but not both. And when instances differ by name, prefer for each: with count, removing an element from the middle of a list shifts every later index, and Terraform plans to change or replace those instances, while for each keeps stable keys.
Dynamic blocks
Some resources contain nested blocks, like the ingress rules of a security group, and a dynamic block repeats them. Its label names the nested block, its for each argument gives the collection, and its content block describes one nested block. Inside the content, the label works as the iterator, with dot key and dot value, like ingress dot value. HashiCorp advises using dynamic blocks sparingly, because they make a configuration harder to read.
Functions
Terraform has many built-in functions, called with a name and arguments in parentheses, like upper, join, merge, or length. They cover strings, collections, numbers, encoding, like JSON encode, files, like file and template file, IP networks, like CIDR subnet, and type conversions. You cannot define your own functions in the language, but providers can offer extra functions, which you call with provider, two colons, the provider name, two colons, and the function. And terraform console opens an interactive prompt, where you can try expressions and functions before you use them. Two functions help with missing values: try, which returns the first argument that works, and coalesce, which returns the first value that is not null or empty.
Recap
Let's recap. Conditionals choose a value, for expressions transform collections into lists or maps, and splats collect an attribute. Count creates numbered instances, for each creates keyed instances from a map or a set, and its keys stay stable when an element is removed. Dynamic blocks repeat nested blocks, and should be used sparingly. And Terraform has built-in functions and provider functions, but no functions of your own, and terraform console lets you test them.
Quick checks for this lesson
The 3 questions of this lesson, as plain text. The button above runs them with shuffled choices and the reason behind every choice.
var.depots is a list of depot names. Which expression gives a list of the same names, in upper case?
- [for d in var.depots : upper(d)]
- {for d in var.depots : upper(d)}
- var.depots[*].upper
- upper(var.depots)
Show the answer
[for d in var.depots : upper(d)]
A for expression in square brackets builds a list, here by applying upper to each name. “{for d in var.depots : upper(d)}” is written with curly braces, which build a map, and they need a key, an arrow, and a value. “var.depots[*].upper” is a splat, which collects an attribute of every element, and upper is not an attribute. And “upper(var.depots)” is wrong, because the upper function takes one string, not a list.
Three depot buckets are created with count, from a list of names. After the second name is removed, the plan wants to change the third depot's bucket. What avoids this?
- Use depends_on between the buckets
- Sort the list before using it
- Use for_each over a set of the names
- Add create_before_destroy to the bucket
Show the answer
Use for_each over a set of the names
With for each over a set of names, each bucket has a stable key, so removing one name only destroys that bucket. “Use depends_on between the buckets” is wrong, because depends on changes the order of operations, not the addresses. “Sort the list before using it” is wrong, because a sorted list still has positions that shift when an element is removed. And “Add create_before_destroy to the bucket” is wrong, because create before destroy changes how a replacement happens, without avoiding it.
Bluefin wants to write its own function, in the Terraform language, to build bucket names for every depot. Which statement is true?
- A function block in the root module defines it
- Terraform has no user-defined functions
- A module can export it to its callers
- terraform console can save it for later
Show the answer
Terraform has no user-defined functions
Terraform only has built-in functions, plus functions offered by providers, so Bluefin would use a local value built with those functions instead. “A function block in the root module defines it” is wrong, because the language has no function block. “A module can export it to its callers” is wrong, because modules return values through outputs, not functions. And “terraform console can save it for later” is wrong, because terraform console only evaluates expressions, for testing.