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 Terraform Join Function?

The Terraform join function is a built-in function that allows you to concatenate a list of strings together. The join function is useful when you need to create a string from a list of elements and want to control the formatting of the resulting string.

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 Terraform workspace from the backend before evaluating any expressions.

What does the Terraform Join Function do?

According to the Terraform docs:

join produces a string by concatenating together all elements of a given list of strings with the given delimiter.

The syntax for the join function is as follows:

join(delimiter, list)
  • delimiter: The string to be used between each pair of adjacent elements in the resulting string.
  • list: The list of elements to be concatenated.

Terraform Join Example

Let’s take a look at the example:

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

Results in the following output:

terraform join example

In the following example, we have 3 Azure storage accounts defined as lists of strings in the variable storage_account_names. We then use the join function in the outputs to show the outputted resource IDs in one comma-separated string.

provider "azurerm" {
 features = {}

# Define variables
variable "storage_account_names" {
 type    = list(string)
 default = ["storageaccount1", "storageaccount2", "storageaccount3"]

# Create Azure Storage Accounts
resource "azurerm_storage_account" "example" {
 count                    = length(var.storage_account_names)
 name                     = var.storage_account_names[count.index]
 resource_group_name      = "example-resource-group"
 location                 = "East US"
 account_tier             = "Standard"
 account_replication_type = "LRS"

# Join the resource IDs of the created storage accounts into a comma-separated string
output "joined_storage_account_ids" {
 value = join(", ", [for acc in azurerm_storage_account.example : acc.id])

The output would look like this:

joined_storage_account_ids = "/subscriptions/<subscription_id>/resourceGroups/example-re

What does the Terraform Split Function do?

Terraform split performs the opposite operation: producing a list by separating a single string using a given delimiter.

Terraform Split Example

For example, the command below will split the three strings into a list:

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.

Note: New versions of Terraform will be placed under the BUSL license, but everything created before version 1.5.x stays open-source. OpenTofu is an open-source version of Terraform that will expand on Terraform’s existing concepts and offerings. It is a viable alternative to HashiCorp’s Terraform, being forked from Terraform version 1.5.6. OpenTofu retained all the features and functionalities that had made Terraform popular among developers while also introducing improvements and enhancements. OpenTofu works with your existing Terraform state file, so you won’t have any issues when you are migrating to it.

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