Terraform: How to get the length of a list

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

Overview

Understanding how to manipulate and query data structures in Terraform is a pivotal skill when designing and implementing infrastructure as code. Among these operations, knowing how to determine the length of a list is crucial for dynamic configurations and looping constructs. This guide will walk you through various scenarios involving lists and demonstrate how to use Terraform’s length() function to obtain the size of a list. We’ll start with the basics and gradually dive into more advanced use cases, providing code examples and expected outputs along the way.

Let’s Define a Terraform List

Before we delve into measuring the length of a list, let’s briefly recap what lists are in Terraform and how to define them. A list in Terraform is an ordered collection of values that are all of the same type. You define a list using square brackets, and separate each item with a comma:

variable "example_list" {
  type = list(string)
  default = ["item1", "item2", "item3"]
}

Using the Length Function

The length() function in Terraform returns the number of elements in a given list, map, or string. For lists, this allows you to ascertain how many items are contained within. An example usage is as follows:

output "example_list_length" {
  value = length(var.example_list)
}

Output:

example_list_length = 3

Dynamic Configuration with Length

Using the length of a list can be incredibly useful for dynamic Terraform configurations. For instance, when creating a set of similar resources that differs only by an index, you can loop over a list using a count property, like so:

resource "aws_instance" "example" {
  count = length(var.example_list)
  // Other configuration fields
}

This configuration will create an instance for each item in var.example_list, dynamically adjusting based on the list’s length.

Advanced List Operations

Moving onto more advanced scenarios, let’s consider list slice operations combined with the length() function. Slicing a list returns a portion of it, and using length() on the result can give you the size of that subset:

locals {
  sliced_list = slice(var.example_list, 0, length(var.example_list) - 1)
}

output "sliced_list_length" {
  value = length(locals.sliced_list)
}

Output:

sliced_list_length = 2

This effectively removes the last item from the list, demonstrating how length() interacts with other list operations.

Conditional Length Calculation

In more complex scenarios, you might want to conditionally execute operations based on the length of a list. For instance, using a condition to output different values if a list is empty or not:

output "list_or_default" {
  value = length(var.example_list) > 0 ? var.example_list : ["default"]
}

This pattern is especially useful for providing default values or alternative paths in your Terraform configurations.

Working with Modules

When working with modules in Terraform, passing lists between modules and calculating their lengths can enable highly dynamic and reusable infrastructures. Consider a module that takes a list of subnet IDs as an input:

module "subnets" {
  source = "./modules/subnets"
  subnet_ids = var.subnet_ids
}

output "num_of_subnets" {
  value = length(module.subnets.subnet_ids)
}

This allows for the dynamic calculation and utilization of resources based on the length of the list passed to the module.

Conclusion

This article has explored various scenarios in which knowing how to ascertain the length of a list in Terraform is beneficial. From dynamic resource creation to advanced list operations and module interactions, understanding and utilizing the length() function can significantly enhance the flexibility and efficiency of your Terraform configurations. As you incorporate these concepts into your infrastructure projects, remember that the power of Terraform lies in its ability to abstract and automate, with operations like determining list length playing a small yet pivotal role in that capability.