Working with dynamic input in Terraform means dealing with plain strings from external systems, CI/CD pipelines, or parameter stores. These inputs are typically unstructured, making it important to transform them into usable formats.
In this article, we’ll cover the Terraform split function and explore some use case examples.
The split
function in Terraform takes a delimiter and a string, then returns a list by splitting the string at each occurrence of the delimiter. It’s often used to parse structured strings like CSVs or resource names.
It follows the format:
split(delimiter, string)
For example, split(",", "a,b,c")
returns ["a", "b", "c"]
.
Unlike some languages, Terraform’s split
does not trim whitespace or remove empty elements automatically, so input formatting matters. Also, it does not treat repeated delimiters as one – split(",", "a,,b")
yields ["a", "", "b"]
.
Remember that split()
always returns a list. If the delimiter is not found in the string, the whole string is returned as a one-element list. This behavior is important when building conditional logic, to avoid indexing errors (e.g., accessing a non-existent [1]
index).
Imagine you have a comma-separated string of environment names like this:
"dev,staging,production"
You want to turn this into a list so you can refer to each environment individually. This is a perfect use case for the split
function.
variable "env_string" {
default = "dev,staging,production"
}
output "env_list" {
value = split(",", var.env_string)
}
output "first_environment" {
value = split(",", var.env_string)[0]
}
When Terraform evaluates this configuration, it will first split the string "dev,staging,production"
by commas. The result is a list:
["dev", "staging", "production"]
The first output, env_list
, shows the entire list. The second output, first_environment
, accesses just the first item in the list, which is "dev"
.
This kind of operation is especially useful when you’re getting input as a string (from a variable or a data source) but need to work with it as a list. If inputs may include spaces you can apply trimspace
to each element after split
.
Say you have a string that contains an IP address with a subnet, something like "10.0.1.15/24"
. You want to extract just the IP part (10.0.1.15
) so you can use it as an input to some other resource that only needs the raw IP address, not the subnet mask.
Here’s how you’d do it in Terraform:
variable "ip_with_cidr" {
default = "10.0.1.15/24"
}
output "ip_only" {
value = split("/", var.ip_with_cidr)[0]
}
The split
function breaks the string "10.0.1.15/24"
into two parts based on the slash. That gives you a list: ["10.0.1.15", "24"]
. Then you grab the first element of that list using [0]
, which is just the IP address.
This kind of thing comes up a lot in infrastructure work, especially when working with subnets, routes, or firewall rules where you need to separate the network address from the mask.
Using split
like this keeps things clean and avoids unnecessary manual edits, especially when you’re dealing with dynamic data or input variables. If you later need network math like masks or host counts use dedicated CIDR functions rather than more string splits.
Imagine a situation where you’re working with AWS and you’re provided with an Amazon Resource Name (ARN) for an IAM role. A full ARN looks like this:
arn:aws:iam::123456789012:role/EC2AccessRole
This string contains several pieces of information: the partition (aws
), the service (iam
), the account ID (123456789012
), and the resource (role/EC2AccessRole
). Suppose you want to extract just the role name (EC2AccessRole
) from this ARN so you can use it elsewhere in your Terraform configuration.
Here’s how you could do it using the split
function in Terraform:
variable "iam_role_arn" {
default = "arn:aws:iam::123456789012:role/EC2AccessRole"
}
output "role_name" {
value = split("/", split(":", var.iam_role_arn)[5])[1]
}
Let’s break this down. First, split(":", var.iam_role_arn)
separates the ARN by colons. This results in a list like:
["arn", "aws", "iam", "", "123456789012", "role/EC2AccessRole"]
We then extract the sixth element (index 5) of this list, which is "role/EC2AccessRole"
. Next, we apply another split
, this time using the /
delimiter, to divide the string into ["role", "EC2AccessRole"]
. Finally, we extract the second element (index 1) to get "EC2AccessRole"
.
This technique enables dynamic extraction of structured values from strings that are common in cloud infrastructure configurations. It reduces hard-coding and makes your Terraform modules more reusable and maintainable.
By combining split
with indexing, you can efficiently dissect complex values and use their components flexibly across your infrastructure as code.
Note: If the role has a path such as role/service-role/EC2AccessRole
a safer split-only approach is to take the last path segment with element(reverse(split("/", split(":", var.iam_role_arn)[5])), 0)
.
The Terraform split function enhances data transformation within Terraform configurations and allows you to work with tools that don’t natively support structured data formats. Use split when you have a clear delimiter, and pair it with helpers like trimspace
, length
, or try
to keep configs robust without overcomplicating them
Terraform is really powerful, but to achieve an end-to-end secure GitOps approach, you need to use a product that can run your Terraform workflows. Spacelift takes managing Terraform to the next level by giving you access to a powerful CI/CD workflow and unlocking features such as:
- Policies (based on Open Policy Agent)
- Multi-IaC workflows
- Self-service infrastructure
- Integrations with any third-party tools
If you want to learn more about Spacelift, create a free account today or book a demo with one of our engineers.
Note: New versions of Terraform are 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 expands on Terraform’s existing concepts and offerings. It is a viable alternative to HashiCorp’s Terraform, being forked from Terraform version 1.5.6.
Manage Terraform better with Spacelift
Build more complex workflows based on Terraform using policy as code, programmatic configuration, context sharing, drift detection, resource visualization and many more.