Expressions and functions
Unlock the complete study guide + 1,040 practice questions across 16 full exams.
Bundled into the existing HashiCorp Certified: Terraform Associate premium course — no separate purchase.
14-day money-back guarantee — no questions asked.
Included in this chapter:
- The value layer: expressions and five tools
- Built-in functions and terraform console
- Choosing values: conditionals, for, and splat
- dynamic blocks: one nested block per element
- String templates: ${ }, %{ }, and heredocs
- Exam-pattern recognition
Choosing an expression tool
| Expression tool | Syntax form | What it produces | Reach for it when |
|---|---|---|---|
| Built-in function | `name(arg1, arg2)` | A single computed value | The config needs numeric, string, or collection work a literal cannot state |
| Conditional (ternary) | `cond ? a : b` | One of two values of a common type | A value depends on a boolean, with no `if`/`else` keywords available |
| `for` expression | `[for x in c : f(x)]` / `{for k, v in c : k => f(v)}` | A new tuple `[ ]` or object `{ }` | You must map or filter every element of a collection |
| Splat operator | `list[*].attr` | A tuple of one attribute per element | You want just one attribute from every element of a list |
| `dynamic` block | `dynamic "b" { for_each = c content { ... } }` | Repeated nested blocks (one per element) | A nested block must repeat per collection element |
| Interpolation | `"${ expr }"` inside a string | Text with the value inserted | A computed value must appear inside string text, with no logic needed |
| Template directive | `%{ if ... }...%{ endif }` / `%{ for ... }...%{ endfor }` | Templated string text | You need conditionals or loops inside a string, beyond plain `${ }` |
Decision tree
Cheat sheet
Unlock with Premium — includes all practice exams and the complete study guide.
References
- Expressions - Terraform configuration language
- Built-in functions - Terraform configuration language
- Function calls - Terraform configuration language
- terraform console command
- Conditional expressions - Terraform configuration language
- For expressions - Terraform configuration language
- Splat expressions - Terraform configuration language
- Dynamic blocks - Terraform configuration language
- Strings and templates - Terraform configuration language