Built-in Functions
Terraform includes many built-in functions that you can call from expressions to transform and combine values. This page provides a complete reference of all available functions.Function Syntax
Functions are called with the syntaxfunction_name(arg1, arg2, ...):
Function Categories
Numeric
Mathematical operations and number manipulation
String
String manipulation and formatting
Collection
List, map, and set operations
Encoding
Encode and decode data formats
Filesystem
Read files and check existence
Date/Time
Timestamp manipulation
Hash/Crypto
Hashing and cryptographic operations
IP Network
CIDR and network calculations
Type Conversion
Convert between types
Numeric Functions
abs
Returns the absolute value of a number.ceil
Returns the closest whole number greater than or equal to the given value.floor
Returns the closest whole number less than or equal to the given value.log
Returns the logarithm of a number in a given base.max
Returns the maximum value from the given numbers.min
Returns the minimum value from the given numbers.parseint
Parses a string as an integer in a given base.pow
Calculates an exponent.signum
Returns the sign of a number (-1, 0, or 1).sum
Returns the sum of all numbers in a list.String Functions
chomp
Removes newline characters at the end of a string.endswith
Checks if a string ends with a specific suffix.format
Formats a string according to a specification.formatlist
Formats multiple strings according to a specification.indent
Adds spaces to the beginning of all but the first line.join
Concatenates list elements with a delimiter.lower
Converts all letters to lowercase.upper
Converts all letters to uppercase.replace
Replaces occurrences of a substring or regex pattern.split
Splits a string into a list using a delimiter.startswith
Checks if a string starts with a specific prefix.strcontains
Checks if a string contains a substring.strrev
Reverses a string.substr
Extracts a substring.title
Converts the first letter of each word to uppercase.trim
Removes specified characters from both ends of a string.trimprefix
Removes a prefix from a string.trimspace
Removes whitespace from both ends.trimsuffix
Removes a suffix from a string.Collection Functions
alltrue
Returns true if all elements in a list are true.anytrue
Returns true if any element in a list is true.chunklist
Splits a list into fixed-size chunks.coalesce
Returns the first non-null, non-empty value.coalescelist
Returns the first non-empty list.compact
Removes empty strings from a list.concat
Combines multiple lists into one.contains
Checks if a list contains a value.distinct
Removes duplicate elements from a list.element
Retrieves an element from a list by index.flatten
Flattens nested lists.index
Finds the index of a value in a list.keys
Returns a list of map keys.length
Returns the length of a string, list, or map.lookup
Retrieves a value from a map with a default fallback.matchkeys
Filters one list based on values in another.merge
Merges multiple maps into one.one
Returns the single element from a list, or null if empty.range
Generates a list of numbers.reverse
Reverses a list.setintersection
Returns the intersection of sets.setproduct
Computes the Cartesian product of sets.setsubtract
Subtracts one set from another.setunion
Returns the union of sets.slice
Extracts a portion of a list.sort
Sorts a list of strings.transpose
Transposes a map of lists.values
Returns a list of map values.zipmap
Constructs a map from lists of keys and values.Encoding Functions
base64decode
Decodes a Base64-encoded string.base64encode
Encodes a string to Base64.base64gzip
Compresses and Base64-encodes a string.csvdecode
Decodes CSV data into a list of maps.jsondecode
Parses a JSON string.jsonencode
Encodes a value as JSON.textdecodebase64
Decodes Base64-encoded text with charset detection.textencodebase64
Encodes text to Base64 with specified charset.urlencode
URL-encodes a string.yamldecode
Parses a YAML string.yamlencode
Encodes a value as YAML.Filesystem Functions
abspath
Converts a relative path to absolute.basename
Returns the last element of a path.dirname
Returns the directory portion of a path.file
Reads a file’s contents as a string.filebase64
Reads a file and returns Base64-encoded contents.fileexists
Checks if a file exists.fileset
Lists files matching a pattern.pathexpand
Expands ~ to the home directory.templatefile
Renders a template file.Hash and Crypto Functions
base64sha256
Computes SHA256 hash and encodes as Base64.base64sha512
Computes SHA512 hash and encodes as Base64.bcrypt
Hashes a string using bcrypt.filebase64sha256
Computes Base64-encoded SHA256 of a file.filebase64sha512
Computes Base64-encoded SHA512 of a file.filemd5
Computes MD5 hash of a file.filesha1
Computes SHA1 hash of a file.filesha256
Computes SHA256 hash of a file.filesha512
Computes SHA512 hash of a file.md5
Computes MD5 hash of a string.rsadecrypt
Decrypts a string encrypted with RSA.sha1
Computes SHA1 hash of a string.sha256
Computes SHA256 hash of a string.sha512
Computes SHA512 hash of a string.uuid
Generates a random UUID.UUID is regenerated on every run. Use it carefully to avoid unnecessary changes.
uuidv5
Generates a name-based UUID.Date and Time Functions
formatdate
Formats a timestamp.plantimestamp
Returns the timestamp when the plan was created.timeadd
Adds a duration to a timestamp.timecmp
Compares two timestamps.timestamp
Returns the current timestamp.IP Network Functions
cidrhost
Calculates a host IP within a CIDR block.cidrnetmask
Converts a CIDR to a netmask.cidrsubnet
Calculates a subnet within a CIDR block.cidrsubnets
Calculates multiple subnets.Type Conversion Functions
can
Evaluates an expression and returns true if it succeeds.convert
Converts a value to a specific type.tobool
Converts to boolean.tolist
Converts to list.tomap
Converts to map.tonumber
Converts to number.toset
Converts to set (removes duplicates).tostring
Converts to string.try
Tries multiple expressions and returns the first that succeeds.Special Functions
sensitive
Marks a value as sensitive.nonsensitive
Removes sensitive marking.issensitive
Checks if a value is marked as sensitive.type (Console Only)
Returns the type of a value.The
type function is only available in terraform console, not in configurations.Best Practices
Use Descriptive Names
Assign function results to descriptive local values for readability.
Handle Errors
Use
try and can to handle potential errors gracefully.Cache Results
Store expensive function results in locals to avoid recomputation.
Test in Console
Use
terraform console to test function behavior interactively.Testing Functions
Useterraform console to experiment:
Next Steps
Expressions
Use functions in expressions
Variables
Combine functions with variables