Functions & Expressions
Writing dynamic Terraform configuration with built-in functions, conditionals and loops.
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,lowerandtrimspace. - Collection functions: Works with lists, sets and maps. Examples:
lengthcounts elements,concatjoins lists,mergecombines maps,lookupreads a map with a fallback,containschecks membership, andflattencollapses nested lists. - Numeric functions: Math. Examples:
min,max,ceil,floor - Type conversion functions: Turn on type into another.