Terraform: How to find the Min/Max value in a list

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

Oveview

Terraform by HashiCorp is an open-source infrastructure as code software tool that allows you to define and provision a datacenter infrastructure using a high-level configuration language. It is often used to manage cloud services and resources with declarative code. Terraform supports operations such as the creation, modification, and deletion of resources, but also offers powerful functions to work with data inside your configurations. Among these functions, finding the minimum or maximum value in a list is a common requirement for conditional logic or resource configuration. In this tutorial, we will delve into how to find the minimum and maximum values in a list using Terraform.

Understanding Lists in Terraform

Before we begin, it’s crucial to understand what lists are in the context of Terraform. A list is a collection of values that are of the same type. Lists are ordered, meaning each value in a list has a specific position that can be referenced. Terraform provides several built-in functions to manipulate lists, including those to find minimum and maximum values.

Basic Example: Finding Minimum and Maximum Values

locals {
  numbers = [1, 2, 3, 4, 5]
}

output "min_value" {
  value = min(local.numbers)
}

output "max_value" {
  value = max(local.numbers)
}

This example demonstrates the basic use of min and max functions in Terraform to find the minimum and maximum values in a list of numbers. The min function returns the smallest number, while the max function returns the largest number in the list.

Advanced Example: Working with Mixed Data Types

It’s essential to note that Terraform does not directly support finding the min/max values in a list of mixed data types (e.g., strings and numbers together). However, you can work around this limitation by using several Terraform functions in combination. The following example shows how to deal with a list of strings that represent numbers.

locals {
  string_numbers = ["1", "2", "3", "4", "5"]
}

output "min_string_number" {
  value = min([for s in local.string_numbers : tonumber(s)])
}

output "max_string_number" {
  value = max([for s in local.string_numbers : tonumber(s)])
}

In this advanced example, we use a for expression to iterate over each item in the list, converting each string to a number using the tonumber function before applying the min and max functions. This method allows for the comparison of numeric representations of strings to find the minimum and maximum values accurately.

Applying Min/Max Values in Resource Configuration

Finding the minimum or maximum values can also be directly applied to resource configurations. For example, you might want to set the size of a resource based on the smallest or largest value in a list of inputs. Here’s how you could dynamically adjust resource sizes:

resource "aws_instance" "example" {
  count = length(local.string_numbers)
  instance_type = "t2.micro"

  tags = {
    Name = "Instance ${count.index + 1}"
    Size = "${max([for s in local.string_numbers : tonumber(s)])}"
  }
}

This example illustrates how to use the max function within a resource’s configuration. Each instance will have a tag indicating its size, which is determined by the maximum number out of a list of string numbers after converting them to numeric values.

Conclusion

By understanding how to work with lists and utilize the min/max functions in Terraform, developers can introduce greater flexibility and conditional logic into their configurations. Whether you’re managing resources based on numeric criteria or need to perform data manipulation before making decisions, Terraform provides the tools to streamline these processes efficiently.