Terraform: How to concatenate lists

Updated: February 3, 2024 By: Guest Contributor Post a comment

Introduction

In this tutorial, we delve into the practical aspects of managing infrastructure as code (IaC) using Terraform, focusing on a specific operation: concatenating lists. Whether you’re a beginner or an experienced developer, understanding how to manipulate lists in Terraform can greatly enhance your automation scripts. We will explore multiple examples, starting with basic concepts and progressing to more advanced techniques. By the end, you’ll have a solid understanding of list concatenation in Terraform, empowering you to write more efficient and streamlined code.

Lists in Terraform

Terraform, a popular open-source tool created by HashiCorp, enables users to define and provision infrastructure using a high-level configuration language. Lists, or arrays, are fundamental data types in Terraform that store ordered sequences of values. Concatenating lists refers to the process of merging two or more lists into a single list. This operation is particularly useful when managing similar resources that need to be handled as a collective group.

Before we jump into concatenating lists, ensure you have Terraform installed on your machine and are familiar with its basic syntax and commands. Now, let’s begin with basic examples and gradually move to more complex scenarios.

Basic List Concatenation

To start, let’s try concatenating two simple lists. Create a Terraform file (main.tf) and input the following:

locals {
  list_one = ["A", "B", "C"]
  list_two = ["D", "E", "F"]
  combined_list = concat(locals.list_one, locals.list_two)
}

output "combined_list" {
  value = locals.combined_list
}

This code defines two local lists and then uses the Terraform concat function to merge them. The result, a single list containing all items in the order they were added, is outputted when you apply the configuration. To see the output, run:

terraform apply

This should return the combined list:

combined_list = ["A", "B", "C", "D", "E", "F"]

Advanced Concatenation Techniques

As we progress, consider situations where lists might not be statically defined in the configuration file. Often, lists are generated dynamically as the output of various resources or modules. Concatenating such lists requires a more nuanced approach.

For example, let’s concatenate lists that are outputs from two different modules:

module "module_one" {
  source = "./module_one"
  // additional configuration
}

module "module_two" {
  source = "./module_two"
  // additional configuration
}

output "combined_list_from_modules" {
  value = concat(module.module_one.output_list, module.module_two.output_list)
}

This snippet demonstrates how to reference lists generated from modules. Note that the exact implementation will depend on the output names defined in your modules.

Another complex scenario involves concatenating lists with different types of elements or nested lists. Terraform’s concat function is versatile but expects that all lists being concatenated are of the same type or structure. Mixing different types (e.g., string and number) in a single list can lead to unexpected behavior or errors. If you encounter such a scenario, it’s advisable to homogenize your lists before concatenation.

Concatenating Lists with Conditional Statements

A powerful feature of Terraform is its ability to use conditional statements within the list concatenation. This feature becomes indispensable when you need to merge lists based on certain conditions, such as environment configurations.

For instance, you might only want to include certain resources in a list if you’re deploying to a production environment. Using Terraform’s conditional syntax, this can be elegantly achieved:

locals {
  env = "prod"
  common_resources = ["resource1", "resource2"]
  prod_resources = ["resource3", "resource4"]

  combined_resources = concat(
    locals.common_resources,
    locals.env == "prod" ? locals.prod_resources : []
  )
}

output "combined_resources" {
  value = locals.combined_resources
}

This code snippet demonstrates how to conditionally concatenate the list of production-only resources with the common resources, based on the deployment environment.

Through the given examples, we’ve explored various ways to concatenate lists in Terraform, from simple to complex use cases. It’s crucial to understand these techniques as they can significantly optimize your Terraform configurations, making them more readable and efficient.

Conclusion

Mastering the art of concatenating lists in Terraform is a highly valuable skill that can streamline the management of infrastructure resources. By practicing the examples provided in this tutorial, you’re now better equipped to utilize this technique in your Terraform projects, enabling more dynamic and powerful configurations.