HashiCorp Terraform, a versatile infrastructure-as-code tool, empowers users to define and provision infrastructure resources with a declarative configuration language. In Terraform, managing strings effectively is pivotal to a seamless experience. This article delves into the techniques of joining and splitting strings, offering valuable insights into handling string operations within your Terraform projects.

Using the join Function

Joining strings in Terraform is a fundamental skill, especially when you need to create configuration options or resource attributes that require concatenated values. Let’s explore how you can expertly join strings in your Terraform configurations.

The join function in Terraform allows you to concatenate a list of strings into a single string, using a specified delimiter.

Here’s how you can use it:

my_list = ["foo", "bar", "xid", "b59"]
my_str = join(", ", my_list)

In this example, the join function takes the my_list variable, concatenates its elements with a comma and space delimiter, and stores the result in the my_str variable.

Joining Dynamic String Values

The ability to join strings dynamically can be immensely useful. Consider a scenario where you want to generate a configuration option based on user inputs or other factors. You can effortlessly achieve this by joining strings in Terraform.

Here’s an explanation of the syntax for the join function:

join(delimiter, list)
  • delimiter: This parameter specifies the character or string that you want to use as the separator or delimiter between the elements in the list when they are concatenated into a single string. It is enclosed in double quotes, and it can be any valid string, such as a comma, space, or custom separator.
  • list: This parameter represents the list of strings that you want to concatenate into a single string. The list parameter should be an expression that evaluates to a list, typically a list of strings, but it can also contain other types that Terraform can implicitly convert to strings.

Let’s say you have a Terraform project where you want to create a configuration option that depends on a user-defined list of values. You want to join these values into a single string with a specific delimiter.

# Define a variable to accept user input as a list of string values
variable "user_values" {
  description = "A list of user-defined values"
  type        = list(string)

# Use the join function to concatenate user-defined values to single string
resource "example_resource" "dynamic_config" {
  config_option = join(", ", var.user_values)

In this example:

  1. We define a Terraform variable named user_values of type list(string), allowing the user to provide a list of values.
  2. Inside the resource block, we use the join function to concatenate the user-defined values from the user_values variable into a single string, separated by a comma and a space.
  3. The resulting string is set as the config_option attribute of the example_resource resource.

The join function is a versatile tool in Terraform for creating formatted strings, which can be particularly useful for configuring resource attributes, dynamic configuration options, and more. The choice of delimiter and the contents of the list can be tailored to your specific use case.

Using the split Function

On the flip side, you may encounter situations where you need to split a string into a list of individual elements. This is particularly valuable when working with external data sources or dealing with comma-separated strings. Let’s dive into the methods for splitting strings in Terraform.

In Terraform, the split function is a powerful tool for breaking a string into a list of substrings based on a specified delimiter. Whether you’re dealing with comma-separated values, space-separated data, or any other format, the split function allows you to dissect a string into manageable components. This functionality is particularly valuable when you need to work with external data sources, user inputs, or configurations that come in string format.

The split function in Terraform has the following syntax:

split(delimiter, input_string)
  • delimiter: This is the character or string that you want to use as the separator for splitting the input string.
  • input_string: The string that you want to split into substrings.

Let’s take a practical example to illustrate how the split function can be used. Consider a scenario where you have a string that contains a list of values separated by commas and spaces. You want to convert this string into a list of individual elements.

Here’s how you can achieve this using the split function:

my_var = "foo, bar, xid, b59"
my_list = split(", ", my_var)

In this example:

  • my_var represents the input string, which contains the values separated by “, “.
  • split(", ", my_var) is the split function call, which takes the my_var string and splits it at each occurrence of “, “. This results in a list of substrings.

After executing this code, the my_list variable will contain ["foo", "bar", "xid", "b59"]. Each value that was originally separated by commas and spaces is now an individual element within the list.

Real-World Applications

Joining and splitting strings in HashiCorp Terraform has practical applications in real-world scenarios:

  • Dynamic Configurations – When configuring options based on user inputs or external data sources, string concatenation allows you to adapt your configurations to varying needs effortlessly.
  • Resource Provisioning – In resource provisioning, it’s often necessary to pass a list of values to resource attributes. By splitting and joining strings, you can dynamically generate these lists according to your requirements.
  • Dealing with External Data – When you receive input data from external sources, such as user inputs or data from other systems, it may be provided in the form of a delimited string. The split function allows you to convert this data into a list, making it easier to work with in your Terraform configurations.


Mastering the art of joining and splitting strings in Terraform opens up a world of possibilities. Whether you need to create dynamic configurations, handle input data from external sources, or provision resources efficiently, these string operations are invaluable. With Terraform’s join and split functions, you have the tools at your disposal to work with strings flexibly and enhance the functionality of your Terraform projects.

Microsoft MVP

Chris Pietschmann is a Microsoft MVP, HashiCorp Ambassador, and Microsoft Certified Trainer (MCT) with 20+ years of experience designing and building Cloud & Enterprise systems. He has worked with companies of all sizes from startups to large enterprises. He has a passion for technology and sharing what he learns with others to help enable them to learn faster and be more productive.
HashiCorp Ambassador Microsoft Certified Trainer (MCT) Microsoft Certified: Azure Solutions Architect