Functions reference
Terraform policy includes built-in functions you can use when you write policies. You can also use many built-in Terraform functions, and write function plugins to implement reusable logic such as HTTP functions or arithmetic operations.
Terraform Policy functions
Terraform policy includes built-in functions to allow you to retrieve resources and data sources dynamically within your policies, make HTTP requests, and compare semantic version constraints:
core::getresources- Return a list of resources that match the specified type and filtercore::getdatasource- Return a data source that matches the specified type and filtercore::gethttprequest- Make an HTTP GET request and return the resultscore::semverconstraint- Validate that a given version matches a version constraint
Plugin functions
In addition to the built-in functions, you can write plugins to implement your own Terraform policy functions. Refer to Policy plugins for more information.
Terraform library functions
Terraform policy also supports a number of standard Terraform library functions, which are in the 'core' namespace, referenced in policy configuration with core::<function-name>(arguments).
Numeric functions
core::abs- Returns the absolute (positive) value of a given numbercore::ceil- Rounds a number up to the nearest whole numbercore::floor- Rounds a number down to the nearest whole numbercore::log- Returns the logarithm of a number to a given basecore::max- Returns the largest of a set of numberscore::min- Returns the smallest of a set of numberscore::parseint- Parses a string as a whole number in the given basecore::pow- Returns the result of a number raised to a given powercore::signum- Returns a number indicating the sign of the input
String functions
core::chomp- Removes any newline characters from the end of a stringcore::format- Produces a string by formatting other values according to a specification stringcore::endswith- Returns true if the string ends with the given characterscore::indent- Adds a specified number of spaces to the beginning of each linecore::join- Concatenates all elements of a list of strings into a single stringcore::lower- Converts all uppercase letters in a string to lowercasecore::regex- Returns the first substring that matches the regular patterncore::regexall- Returns a list of all substrings that match the regular patterncore::split- Splits a string into a list of substringscore::startswith- Returns true if the string starts with the given characterscore::strrev- Reverses the characters in a stringcore::substr- Returns a substring starting at offset with the specified lengthcore::title- Converts the first letter of each word to uppercasecore::trim- Removes leading or trailing characters specified in cutsetcore::trimprefix- Removes the specified prefix from the beginningcore::trimspace- Removes all leading and trailing whitespacecore::trimsuffix- Removes the specified suffix from the endcore::upper- Converts all lowercase letters to uppercase
Collection functions
core::alltrue- Returns true if all elements in a collection are truecore::anytrue- Returns true if any element in a collection is truecore::can- Evaluates an expression and returns true if it produces a result without errorscore::chunklist- Splits a single list into fixed-size chunkscore::coalescelist- Returns the first list that is not null or emptycore::compact- Returns a new list with any null or empty string elements removedcore::concat- Combines two or more lists into a single listcore::contains- Determines whether a list contains at least one element equal to the given valuecore::distinct- Returns a new list containing only the unique elementscore::element- Retrieves a single element from a list by its indexcore::flatten- Collapses a multi-dimensional list into a single-dimensional listcore::formatlist- Produces a list of strings by formatting valuescore::keys- Returns a list of all the keys in a given mapcore::length- Returns the number of elements in a list, map, or stringcore::merge- Combines multiple maps into a single mapcore::range- Generates a list of numberscore::reverse- Reverses the order of elements in a listcore::setintersection- Returns elements common to all given setscore::setproduct- Returns the Cartesian product of the given setscore::setsubtract- Returns elements from set1 not present in set2core::setunion- Returns all unique elements from all given setscore::slice- Returns a subset of elements from the original listcore::sort- Sorts the elements of a list in ascending ordercore::values- Returns a list of all the values in a given mapcore::zipmap- Creates a map from two lists of equal length
Encoding/decoding functions
core::csvdecode- Decodes CSV-formatted data into a list of mapscore::jsondecode- Decodes a JSON-formatted string into a Terraform valuecore::jsonencode- Encodes a Terraform value into a JSON-formatted stringcore::yamldecode- Decodes a YAML-formatted string into a Terraform valuecore::yamlencode- Encodes a Terraform value into a YAML-formatted string
Terraform-specific functions
core::timeadd- Adds a duration string to a timestampcore::timecmp- Compares two timestampscore::timestamp- Returns a string representation of the current date and timecore::try- Evaluates expressions in order and returns the first one that does not produce an error