Built-in functions

Terraform ships with a library of built-in functions for transforming and combining values inside expressions.

You can call one by its name followed by an argument or more in parentheses. An example could be: upper("web"), which return "WEB". Functions can be nested and you can use them anywhere an expression is allowed:

  • Resource arguments
  • Locals
  • Outputs
  • Variable Validation

You cannot define your own functions in HCL. You are limited to built-in functions, but there are some functions that providers ship, which are called as provider::<name>::<function>.

The functions fall into a few groups, and knowing what each group is for matters more than memorising:

  • String functions: Build and clean up test. For example: format, join, split, replace, upper, lower and trimspace.
  • Collection functions: Works with lists, sets and maps. Examples: length counts elements, concat joins lists, merge combines maps, lookup reads a map with a fallback, contains checks membership, and flatten collapses nested lists.
  • Numeric functions: Math. Examples: min, max, ceil, floor
  • Type conversion functions: Turn on type into another.

Conditional expressions

for expressions

dynamic blocks