Terraform: How to add/remove elements from a list

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

Overview

Terraform, an open-source infrastructure as code tool developed by HashiCorp, allows you to define both cloud and on-prem resources in human-readable configuration files that can be versioned and reused. Lists (or arrays) are an essential part of Terraform configurations, providing a way to define multiple values or resources of the same type. In this tutorial, we’ll delve into how to manipulate lists in Terraform, including adding and removing elements.

Understanding Lists in Terraform

Briefly, a list is an ordered collection of values where each value is of the same type. before diving into manipulating lists, ensure that you have Terraform installed and are familiar with basic concepts like variables and resources.

Adding Elements to a List

Adding elements to a list in Terraform can be done in several ways:

  • Static addition
  • Dynamic addition using functions

Static List Addition Example

variable "example_list" {
  type    = list(string)
  default = ["element1", "element2"]
}

resource "some_resource" "example" {
  some_property = [...var.example_list, "element3"]
}

In this static addition example, a new element (“element3”) is explicitly added to the end of the existing list defined by the variable `example_list`.

Dynamic Addition using Concat Function

variable "example_list" {
  type    = list(string)
  default = ["element1", "element2"]
}

locals {
  additional_elements = ["element3", "element4"]
}

resource "some_resource" "example" {
  some_property = concat(var.example_list, local.additional_elements)
}

Here, the `concat` function is used to dynamically add multiple elements (`element3`, `element4`) to `example_list`. This is useful for when the elements to be added are not known ahead of time or are the result of another operation.

Removing Elements from a List

Removing elements from a list is more complex and usually involves the use of filtering functions. Terraform doesn’t have a built-in `remove` function, but you can achieve similar results with the `filter` function.

Filtering Example

variable "example_list" {
  type    = list(string)
  default = ["element1", "element2", "element3"]
}

locals {
  element_to_remove = "element2"
}

output "modified_list" {
  value = [for e in var.example_list : e if e != local.element_to_remove]
}

This code snippet demonstrates how to use a for loop with an if condition to exclude `element2` from the list. This method is powerful and flexible, allowing for the removal of multiple elements based on complex conditions.

Advanced List Manipulation

Beyond simple addition and removal of elements, Terraform’s rich function set allows for sophisticated manipulation of lists, including slicing, dicing, and merging.

Slicing Lists

variable "example_list" {
  type    = list(string)
  default = ["a", "b", "c", "d"]
}

output "sliced_list" {
  value = slice(var.example_list, 1, 3)
}

With the `slice` function, you can extract a portion of a list, specified by start and end indexes. In the example above, the resulting sliced list will only contain `[“b”, “c”]`, slicing from index 1 to 3 (exclusive of the end index).

Merging Lists

variable "list_one" {
  type    = list(string)
  default = ["a", "b"]
}

variable "list_two" {
  type    = list(string)
  default = ["c", "d"]
}

output "merged_list" {
  value = concat(var.list_one, var.list_two)
}

The `concat` function can also be used to merge two lists into one. This can be incredibly useful for composing complex configurations dynamically.

Conclusion

Mastering list manipulations in Terraform can greatly enhance your flexibility and efficiency in defining and managing infrastructure as code. Through static additions, dynamic concatenations, and creative filtering, you can achieve a high degree of control over list elements. As you advance in your Terraform journey, harness the power of mixing and matching functions to tailor lists exactly to your needs.