» Functions

Terraform provides a set of built-in functions that transform and combine values within Terraform configurations. The Terraform function documentation contains a complete list. You can also use your editor autocompletion on the Fn object to find available options.

Functions can handle normal and token values and will return either tokenized values or IResolvable values.

» When to Use Terraform Functions

Use Terraform functions when you need to calculate new values based on runtime values that are unknown before Terraform applies a configuration. For example, instance IDs that cloud providers assign on creation.

When inputs are available before synthesizing your code (e.g. local files), we recommend transforming the values with your preferred programming language.

» Usage Example

The TypeScript example below uses a Data Source from the AWS Provider to fetch the Availability Zones of the given region. As this data is unknown until Terraform applies the configuration, this CDKTF application uses both Terraform Outputs and the Terraform element function.

The element function gets the first element from the list of Availability Zone names.

import { Fn, TerraformOutput } from "cdktf";
import { DataAwsAvailabilityZones } from "@cdktf/provider-aws";

// ...

const zones = new DataAwsAvailabilityZones(this, "zones", {
  state: "available",
});

new TerraformOutput(this, "first-zone", {
  value: Fn.element(zones.names, 0),
});

// ...