OpenTofu is now part of the Linux Foundation 🎉

Read more here →


How to Use Terraform Join and Split Functions with Strings

How to use the Terraform join and split functions

The Hashicorp Configuration Language (HCL) that Terraform employs utilizes a number of beneficial built-in functions. These functions can be used to manipulate and transform values within a given expression. In this post, we will take a look at the operation of the join function and its counterpart, the split function for use with Strings.

What Is A Built-in Function?

HCL does not support user-defined functions, like some other classic languages, for example, C or SQL. Only built-in functions are available for use with Terraform. The current list of available built-in functions can be found in the Terraform documentation.

Using The Terraform Console To Experiment

A particularly useful way to experiment with the operation of functions is to use the terraform console command. This provides an interactive console to the user. To try it out, simply type terraform console on the command line, and then a simple join command:

join(", ", ["this", "and", "that"])

Note that terraform console does not require a state file to exist or be created first. If there is a remote state file in the current working directory, Terraform will read the state for the current workspace from the backend before evaluating any expressions.

What Do the Join and Split Functions in Terraform Do?

split(", ", "this, and, that")
terraform console - split

The split function can be useful to use in output values when a list of values exists. For example, for the creation of an Azure VNET gateway, the following configuration would split out the names of the Gateways with the “/” delimiter.

output “gateway_network_name” {
value =    split(“/”,azurerm_virtual_network_gateway.azure_vng.ip_configuration[0].subnet_id)[8]

Note that split can be used with variables, for example:


Lastly join and split can be useful when creating or manipulating configuration files for devices, appliances, or environment variables that are created or pulled into Terraform configuration. For example, to pull IP addresses from a list to pass into a file needed to configure a firewall. They are also useful to extract from a list of URLs or chop up parts of a URL.

Key Points

And if you want more help managing your Terraform state file, building more complex workflows based on Terraform, and create self-service infrastructure, Spacelift is a great tool for that.

Manage Terraform Better and Faster

If you are struggling with Terraform automation and management, check out Spacelift. It helps you manage Terraform state, build more complex workflows, and adds several must-have capabilities for end-to-end infrastructure management.

Start free trial
Terraform Functions Cheatsheet

Grab our ultimate cheat sheet PDF for all the Terraform functions you need on hand.

Share your data and download the cheatsheet