How to filter a map in Terraform

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

Overview

Terraform is an incredibly powerful tool for creating, changing, and versioning infrastructure safely and efficiently. One of its many strengths lies in its ability to manipulate complex data structures, including maps (objects in Terraform 0.12 and above). This guide will walk you through the process of filtering maps in Terraform, starting with the basics and progressing to more advanced techniques, complete with code examples and their expected outputs. Understanding how to orchestrate this can significantly enhance your Terraform scripts’ efficiency and readability.

Understanding Map Data Structure in Terraform

Before diving into filtering, let’s clarify what maps are in Terraform’s context. A map is a collection of key-value pairs, where each key is unique. In Terraform, maps are used to organize and store data that logically belongs together. They’re incredibly useful for grouping related values and accessing them via their keys. Now, let’s understand how to filter maps based on different criteria.

Basic Filtering of Maps

The most straightforward way to filter a map in Terraform is by using the for construct to iterate over the map and include only items that match certain criteria. Supposing we have the following input map:

variable "input_map" {
  default = {
    "a" = "apple",
    "b" = "banana",
    "c" = "cherry"
  }
}

To filter this map to include only keys that start with the letter ‘a’, we could write:

{for k, v in var.input_map : k => v if substr(k, 0, 1) == "a"}

This Terraform expression filters the input map to include only the items that have keys starting with ‘a’, producing:

{
  "a" = "apple"
}

Filtering Based on Value

Similarly, you also can filter maps based on their values. Assuming we have an input map like before, but you want to include values that contain ‘apple’ or ‘banana’. The filtering expression would look something like this:

{for k, v in var.input_map : k => v if contains(["apple", "banana"], v)}

This produces:

{
  "a" = "apple",
  "b" = "banana"
}

Advanced Filtering with Multiple Conditions

Moving onto more advanced filtering, suppose we need to filter based on both keys and values meeting certain conditions. If you want to keep elements where the key starts with ‘b’ and the value includes ‘an’, the expression expands as follows:

{for k, v in var.input_map : k => v if substr(k, 0, 1) == "b" && contains(v, "an")}

The result of this filtering is:

{
  "b" = "banana"
}

Filtering and Transforming Map Values

A step further involves not just filtering maps but also transforming the values in the process. For example, you might want to uppercase all values that start with ‘b’:

{for k, v in var.input_map : k => upper(v) if substr(k, 0, 1) == "b"}

This operation would return:

{
  "b" = "BANANA"
}

Combining Maps After Filtering

In a more complex scenario, you might want to combine results from filtering multiple maps. Assuming two maps:

variable "first_map" {
  default = {
    "a" = "apple",
    "b" = "banana"
  }
}

variable "second_map" {
  default = {
    "c" = "cherry",
    "d" = "date"
  }
}

To combine these maps into one while filtering, you could:

{
  ...{for k, v in var.first_map : k => v if substr(k, 0, 1) == "a"},
  ...{for k, v in var.second_map : k => v if substr(k, 0, 1) == "d"}
}

This produces a new map with elements filtered from both original maps based on the specified conditions:

{
  "a" = "apple",
  "d" = "date"
}

Performance Considerations When Filtering Maps

When filtering maps in Terraform, it’s essential to consider the implications on performance, especially with large datasets. Every filtering operation adds complexity and computation time. To optimize performance, minimize the number of iterations and filter operations by consolidating criteria and conditions as much as possible.

Conclusion

Filtering maps in Terraform allows for greater flexibility and control over your infrastructure configuration, enabling more precise and efficient structures. Whether you’re dealing with simple or complex maps, the examples provided should give you a solid foundation for manipulating maps in your Terraform projects. With practice, these techniques will become second nature, greatly enhancing your Terraform proficiency.