Domain 4 of 8 · Chapter 5 of 8

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 toolSyntax formWhat it producesReach for it when
Built-in function`name(arg1, arg2)`A single computed valueThe config needs numeric, string, or collection work a literal cannot state
Conditional (ternary)`cond ? a : b`One of two values of a common typeA 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 elementYou 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 stringText with the value insertedA computed value must appear inside string text, with no logic needed
Template directive`%{ if ... }...%{ endif }` / `%{ for ... }...%{ endfor }`Templated string textYou need conditionals or loops inside a string, beyond plain `${ }`

Decision tree

Repeat a nested block?dynamic blockfor_each + contentYesLogic inside a string?template directive%{ if } / %{ for }YesNoInsert a value in text?interpolation${ ... }YesNoOne attribute per element?splat operatorlist[*].attrYesNoMap or filter a collection?for expression[ ] tuple / { } objectYesNoChoose one of two values?conditionalcond ? a : bYesNobuilt-in functionname(args), closed libraryNo

Cheat sheet

  • No user-defined functions
  • Function call syntax and ellipsis
  • Function categories
  • terraform console interactive REPL
  • Ternary conditional syntax
  • Conditional result type
  • For expression syntax
  • Splat operator
  • dynamic blocks for repeatable nested blocks
  • String interpolation, heredocs, and template directives

Unlock with Premium — includes all practice exams and the complete study guide.

References

  1. Expressions - Terraform configuration language
  2. Built-in functions - Terraform configuration language
  3. Function calls - Terraform configuration language
  4. terraform console command
  5. Conditional expressions - Terraform configuration language
  6. For expressions - Terraform configuration language
  7. Splat expressions - Terraform configuration language
  8. Dynamic blocks - Terraform configuration language
  9. Strings and templates - Terraform configuration language