Function Concepts

!rw-book-cover](https://developer.hashicorp.com/og-image/terraform.jpg)

This page describes Terraform concepts relating to provider-defined functions within framework-based provider code. Provider-defined functions are supported in Terraform 1.8 and later. The What is Terraform](/terraform/intro), Terraform language](/terraform/language), and Plugin Development](/terraform/plugin) documentation covers more general concepts behind Terraform's workflow, its configuration, and how it interacts with providers.

The purpose of provider-defined functions is to encapsulate offline, computational logic beyond Terraform's built-in functions to simplify practitioner configurations. Terraform expects that provider-defined functions are implemented without side-effects and as pure functions where given the same input data that they always return the same output. Refer to HashiCorp Provider Design Principles](/terraform/plugin/best-practices/hashicorp-provider-design-principles) for additional best practice details.

Example use cases include:

Differences from other provider-defined concepts include:

There are two main components of provider-defined functions:

Within a function definition the components are:

Similar to many programming languages, when the function is called, the terminology for the data is slightly different than the terminology for the definition.

For each provider listed as a required provider](/terraform/language/providers/requirements), Terraform will query the provider for its function definitions. If a configuration attempts to call a provider-defined function without listing the provider as required, Terraform will return an error.

Terraform will typically call functions before other provider concepts are evaluated. This includes before provider configuration being evaluated, which the framework enforces by not exposing provider configuration data to function implementations.

Terraform requires that function names must be valid identifiers](/terraform/language/syntax/configuration#identifiers).

Terraform will statically validate that the number and types of arguments in a configuration match the definitions of parameters, otherwise returning an error.

If a null value is given as an argument, without individual parameter definition opt-in, Terraform will return an error. If an unknown value is given as an argument, without individual parameter definition opt-in, Terraform will skip calling the provider logic entirely and set the function result to an unknown value matching the return type.

Terraform will statically validate that the return type is appropriately used in consuming configuration, otherwise returning an error.

Function logic must always set the result to the return type, otherwise Terraform will return an error.

Function logic can only set the result to an unknown value if there is a parameter that opted into unknown value handling and an unknown value argument was received for one of those parameters.