🐶
Terraform

Terraform: How to Append Elements to a List

By Filip on 10/07/2024

Learn different techniques to effectively append elements to existing lists within your Terraform configurations for more dynamic and scalable infrastructure deployments.

Terraform: How to Append Elements to a List

Table of Contents

Introduction

In Terraform, modifying lists directly can be tricky. If you need to add elements to a list without changing the original, the concat function is your solution. Let's explore how to use concat to append elements to lists effectively.

Step-by-Step Guide

To append elements to a list in Terraform, you can use the concat function. This function takes multiple lists as arguments and combines them into a single list.

For instance, if you have a list called foo and you want to add an element to it without modifying the original list, you can create a new list with the additional element and then use concat to combine them.

variable "foo" {
 default = ["a", "b"]
}

locals {
 bar = concat(var.foo, ["c"])
}

In this example, var.foo represents the original list and ["c"] is the new element being added. The concat function combines these two, resulting in a new list stored in the local.bar variable.

This approach allows you to append elements to a list without altering the original, ensuring that your modifications are explicit and controlled.

Code Example

This Terraform code defines a variable "foo" with a list of elements "a" and "b". It then uses the "concat" function to create a new list "bar" by appending the element "c" to the original list. Finally, it outputs both the original and modified lists, demonstrating how "concat" allows adding elements to a list without altering the original.

# Define a variable with a list
variable "foo" {
  default = ["a", "b"]
}

# Append an element to the list using concat
locals {
  bar = concat(var.foo, ["c"])
}

# Output the original and modified lists
output "original_list" {
  value = var.foo
}

output "modified_list" {
  value = local.bar
}

Explanation:

  1. Variable Definition: We define a variable var.foo with an initial list containing elements "a" and "b".
  2. Appending with concat: Inside the locals block, we use the concat function to create a new list local.bar. This function takes two arguments:
    • var.foo: The original list we want to append to.
    • ["c"]: A list containing the single element "c" that we want to add.
  3. Outputting the Lists: We use output blocks to display the contents of both the original list (var.foo) and the modified list (local.bar).

Output:

original_list = [
  "a",
  "b",
]

modified_list = [
  "a",
  "b",
  "c",
]

As you can see, the modified_list now includes the appended element "c" while the original_list remains unchanged. This demonstrates how concat allows you to add elements to a list in Terraform without modifying the original.

Additional Notes

  • Remember that concat doesn't modify lists in place. It creates a new list with the combined elements.
  • You can use concat to combine multiple lists, not just append a single element. For example: concat(["a", "b"], ["c", "d"], ["e"]) would result in ["a", "b", "c", "d", "e"].
  • This technique is helpful when you're working with modules and need to add elements to a list passed in as input.
  • For more complex list manipulations, consider using other Terraform functions like slice, element, or lookup.
  • Always test your code thoroughly to ensure that the list manipulation produces the desired outcome.
  • Understanding how to use concat effectively is crucial for writing clean and maintainable Terraform code when dealing with lists.

Summary

Feature Description Example
Appending Elements to a List Use the concat function to combine an existing list with a new list containing the elements to be added. concat(var.foo, ["c"])
Preserving the Original List concat creates a new list, leaving the original list (var.foo) unmodified. local.bar stores the new list.
Benefits Explicit and controlled modifications to lists.

Conclusion

By understanding and utilizing the concat function, you can effectively manage and modify lists within your Terraform projects, ensuring that your code remains clear, predictable, and easy to maintain. Remember to leverage the provided resources and examples to deepen your understanding and explore more advanced list manipulation techniques in Terraform.

References

Were You Able to Follow the Instructions?

😍Love it!
😊Yes
😐Meh-gical
😞No
🤮Clickbait