Implement a function
In this tutorial, you will add a function to a provider that calculates the tax rate for a fictional coffee-shop application called Hashicups. To do this, you will:
- Implement the function.
Thecompute_tax
function takes a price, a tax rate, and returns the total cost including tax. - Reference the function.
You will update your configuration to use the function and verify that it works as expected. You will add tests for the function in the next tutorial.
Provider-defined functions allow provider developers to define functions that encapsulate offline, computational logic. Practitioners can call functions from their Terraform configuration. Unlike resources and data sources, functions do not manage infrastructure or retrieve data from APIs.
Note
Provider-defined functions require Terraform version 1.8+.Prerequisites
To follow this tutorial, you need:
- Go 1.21+ installed and configured.
- Terraform v1.8+ installed locally.
- Docker and Docker Compose to run an instance of HashiCups locally.
Navigate to your terraform-provider-hashicups
directory.
Your code should match the 08-import-order
directory
from the example repository.
Implement the function
Provider functions are types that implement the function.Function
interface from the plugin framework.
The interface requires the following:
- A Metadata method that sets the function name. Unlike resources and data sources, function names do not start with the provider name.
- A Definition method that defines the parameters, return value, and documentation for the function.
- A Run method that executes the function code.
Create a file named internal/provider/compute_tax_function.go
and paste in the contents below.
The Run
method includes the code that implements the function itself. First,
it retrieves the function arguments, then calculates the total price, and
finally sets the result, returning any errors. Provider-defined functions do
all of their work locally. You should never call remote APIs from within the
Run
method.
Providers that include functions must implement the
provider.ProviderWithFunctions
interface from the plugin framework.
Open the internal/provider/provider.go
file and replace the var
block with
the following.
The ProviderWithFunctions
interface requires a Functions
method that returns
a list of the functions supported by your provider. Add the following to the end
of provider.go
.
Add the required package by replacing the import
statement at the beginning of the file with the following.
Build and install the updated provider.
Verify the function
Create an examples/compute_tax
directory and navigate to it.
Create a main.tf
Terraform configuration file in this directory that calls the
compute_tax
function.
This configuration includes an output value set by calling the compute_tax
function. This represents the total price of an item that costs $5.00 after a
tax rate of 8.5%.
You call provider-defined functions with the syntax
provider::<PROVIDER_NAME>::<FUNCTION_NAME>(<ARGUMENTS>)
.
Apply this configuration to ensure that the compute_tax
function returns the
total price after tax is applied.
Navigate to the terraform-provider-hashicups
directory.
Next steps
Congratulations! You have enhanced the hashicups
provider by adding a
function.
If you were stuck during this tutorial, checkout the
09-functions
directory in the example repository to see the code implemented in this
tutorial.
- To learn more about the Terraform Plugin Framework, refer to the Terraform Plugin Framework documentation.
- For a full capability comparison between the SDKv2 and the Plugin Framework, refer to the Which SDK Should I Use? documentation.
- The example repository contains directories corresponding to each tutorial in this collection.
- Submit any Terraform Plugin Framework bug reports or feature requests to the development team in the Terraform Plugin Framework Github repository.
- Submit any Terraform Plugin Framework questions in the Terraform Plugin Framework Discuss forum.