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.
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.
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.
According to the Terraform docs:
join
produces a string by concatenating together all elements of a given list of strings with the given delimiter.
In the previous command, we specified a comma as the delimiter and combined the 3 strings specified in the list to produce the output.
split
performs the opposite operation: producing a list by separating a single string using a given delimiter.
For example, the command below will split the 3 strings into a list:
split(", ", "this, and, that")
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:
split("/",var.VAR_NAME)
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.
There are many built-in functions available for use with Terraform. join
and split
can be used to transform a list of Strings as required.
For more information, check out the official documentation.
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.
Terraform Functions Cheatsheet
Grab our ultimate cheat sheet PDF for all the Terraform functions you need on hand.