Skip to main content

Functions

Built-in Functions

The OpenTofu language includes a number of built-in functions that you can call from within expressions to transform and combine values. The general syntax for function calls is a function name followed by comma-separated arguments in parentheses:

Code Block
max(5, 12, 9)

For more details on syntax, see Function Calls in the Expressions section.

You can experiment with the behavior of OpenTofu's built-in functions from the OpenTofu expression console, by running the tofu console command:

Code Block
> max(5, 12, 9)
12

The examples in the documentation for each function use console output to illustrate the result of calling the function with different parameters.

Provider-defined Functions

As of OpenTofu 1.7.0, providers may define their own functions to be available during execution.

OpenTofu iterates through the required_providers block and queries the specified providers for any functions they wish to register. Functions are added to the current module's context under provider::<provider_name>::<function_name>. Provider aliases are also supported under provider::<provider_name>::<provider_alias>::<function_name>. Functions are scoped to the module that requires the provider and are not inherited by child modules.

Example:

Code Block
terraform {
required_providers {
myhelper = {
# yantrio/helpers registers a function named "echo"
source = "yantrio/helpers"
}
}
}

locals {
myval = provider::myhelper::echo("Hello Functions!")
}

Notes for Provider Authors:

  • Support for functions was added in protocol version 5.5 and 6.5.
  • OpenTofu's provider protocol is compatible with Terraform's provider protocol.
  • GetProviderSchema() is used to initially query the functions available in a given provider.
  • Providers which supply functions may be configured and may supply additional functions via GetFunctions(). See the experimental Lua and Go providers for implementation examples.