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 (or OpenTofu) 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 following example 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 CDKTN application uses both Terraform Outputs and the Terraformelement function.
The element function gets the first element from the list of Availability Zone names.
Special functions
Property Access Helpers
To access nested properties from untyped objects or other datasources that return a dynamic datatype, use the Terraform functionlookup or, for nested access, the function “Fn.lookupNested()” which is a function offered by CDKTN that allows to avoid nesting Fn.lookup calls.
Raw string helper
Another helper function offered by CDKTN isFn.rawString which can be used to escape raw strings that contain characters that CDKTN or Terraform would try to interpret otherwise.
Operators
Use theOp object to include operators like !, +, and -.
Using Terraform built-in functions directly within strings
It is also possible to use all built-in Terraform functions without using CDKTN’sFn.* functions described above. To write Terraform built-in functions the same as you would in HCL, simply wrap the desired string within the HCL ${ and } syntax. Note: CDKTN doesn’t do any further processing within the escaped syntax (${ and }), and thus is unable to handle nested escape syntaxes yet.
Function Availability Across Terraform and OpenTofu
CDK Terrain works with both Terraform and OpenTofu, but the two products do not support the exact same set of built-in functions. TheFn class covers the
union of both: most functions are available everywhere, while a few exist in
only one product or were introduced in a later release.
The bindings are generated from the function metadata of every stable
Terraform release since 1.5.7 and every stable OpenTofu release since 1.6.0.
Functions that are not universally available are listed below.
All other functions exposed on
Fn are available in every supported release
of both products.
Validating Function Usage at Synth Time
With thevalidateFunctionVersions feature flag
enabled, CDK Terrain checks every function you use through Fn against the
project’s declared targetVersions
— the Terraform/OpenTofu version ranges your project intends to support.
Projects created with cdktn init have the flag enabled by default; existing
projects can opt in via cdktf.json:
terraform plan fail later:
"terraform": ">=1.9.0") — or drop the
product you don’t support — to use the function.
The validation is purely declarative: it never executes a Terraform or
OpenTofu binary, so synth behaves identically in CI, package builds, and
environments without either installed. To explicitly verify the binary
installed on a machine against the declared targets, opt in with
"validateInstalledBinary": true, which applies to commands that execute
the CLI (diff, deploy, destroy).skipValidation: true to your
App configuration.
Limitations
- Functions written inside raw expression strings (escape hatches such as
overrides or hand-built
${...}interpolations) are not tracked and therefore not validated. - Provider-defined functions
(
provider::<name>::<function>) are out of scope; availability depends on the provider, not the CLI version.