Sling Academy
Home/DevOps/Terraform: How to split a string into a list

Terraform: How to split a string into a list

Last updated: February 03, 2024

In the world of Infrastructure as Code (IaC), Terraform stands out for its simplicity and elegance in managing and provisioning infrastructure through code. One of the frequent operations when working with Terraform is the manipulation of strings and lists to dynamically configure resources. In this tutorial, we’ll focus on how to split a string into a list, a common operation needed in various scenarios.

Understanding Terraform’s Split Function

Before we dive into examples, it’s critical to understand Terraform’s split function. This built-in function divides a string into a list of strings, split along occurrences of a specified separator.

The function’s syntax is: split(separator, string)

# Example
split(",", "one,two,three")
# Output: ["one", "two", "three"]

Basic Usage of Split

Let’s start with a basic example where we have a string of comma-separated values that we want to convert into a list.

variable "example_string" {
  type    = string
  default = "apple,banana,cherry"
}

output "result" {
  value = split(",", var.example_string)
}

This will output:

["apple", "banana", "cherry"]

Note how we simply specify the comma as the separator in the split function to break the string into a list of individual fruit names.

Splitting with Different Separators

The beauty of Terraform’s split function is its flexibility with different types of separators. Not only commas, but you can also use spaces, colons, or any character as your separator. Here’s how to split a string with space as a separator:

variable "space_separated_string" {
  type    = string
  default = "Hello World Terraform"
}

output "split_by_space" {
  value = split(" ", var.space_separated_string)
}

This will output:

["Hello", "World", "Terraform"]

Advanced Usage: Dealing with Special Characters

Often, the string you’re trying to split might contain special characters, such as newline characters or tabs. Let’s see how to handle a string that has newline characters.

variable "newline_separated_values" {
  default = "one\ntwo\nthree"
}

output "split_by_newline" {
  value = split("\n", var.newline_separated_values)
}

This approach can be particularly useful when reading input from a file that has values on separate lines.

Using Split with Terraform Data Sources and Resources

The split function can also be integral in working with Terraform data sources and resources. For example, when retrieving a comma-separated string from a data source and needing to manipulate it into a list.

data "aws_vpc" "example" {
  tags = {
    Name = "example-vpc"
  }
}

output "vpc_subnets" {
  value = split(",", data.aws_vpc.example.cidr_block)
}

This snippet demonstrates using the split function to convert the CIDR blocks of an AWS VPC into a list, which can then be referenced elsewhere in your Terraform configuration.

Handling Edge Cases

In real-world applications, you’ll often encounter edge cases that require attention. For instance, what happens if the separator is not found in the string, or if the string ends with a separator? Knowing how Terraform’s split function behaves in these scenarios is crucial for creating robust configurations.

  • If the separator is not found, the split function returns a list containing the original string.
  • If the string ends with a separator, the resulting list will include an empty string as the last element.

Conclusion

In this tutorial, we explored how to use Terraform’s split function to convert strings into lists. We covered from basic usage with common separators to more advanced scenarios including handling special characters and edge cases. This essential skill will add flexibility and dynamism to your Terraform configurations.

Next Article: Terraform: How to join list elements into a string

Previous Article: How to mix strings with variables in Terraform

Series: Terraform Tutorials

DevOps

You May Also Like

  • How to reset Ubuntu to factory settings (4 approaches)
  • Making GET requests with cURL: A practical guide (with examples)
  • Git: What is .DS_Store and should you ignore it?
  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide