Files
opentf/website/docs/language/functions/index.mdx
2024-12-25 13:21:59 +04:00

72 lines
2.5 KiB
Plaintext

---
description: >-
An introduction to the functions which can be used to transform and
combine values in expressions.
---
# 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:
```hcl
max(5, 12, 9)
```
For more details on syntax, see
[_Function Calls_](../../language/expressions/function-calls.mdx)
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](../../cli/commands/console.mdx):
```
> 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](../../language/providers/requirements.mdx) 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:
```hcl
terraform {
required_providers {
myhelper = {
# yantrio/helpers registers a function named "echo"
source = "yantrio/helpers"
}
}
}
locals {
myval = provider::myhelper::echo("Hello Functions!")
}
```
### Built-in Provider Functions:
OpenTofu has a built-in provider `terraform.io/builtin/terraform` which provides [additional functions](../providers/builtin.mdx#functions) that can be used in OpenTofu configurations.
### 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](https://github.com/opentofu/terraform-provider-lua) and [Go](https://github.com/opentofu/terraform-provider-go) providers for implementation examples.