Sling Academy
Home/DevOps/Terraform: How to find the Min/Max value in a list

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

Last updated: February 03, 2024

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.

Next Article: Terraform: 3 ways to round a number

Previous Article: Terraform: How to find the index of an element in a list

Series: Terraform Tutorials

DevOps

You May Also Like

  • How to reset Ubuntu to factory settings (4 approaches)
  • Making GET requests with cURL: A practical guide (with examples)
  • Git: What is .DS_Store and should you ignore it?
  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide