Compare lists of string in terraform
Categories:
Comparing Lists of Strings in Terraform

Learn how to effectively compare two lists of strings in Terraform, covering common scenarios like checking for equality, differences, and subset relationships.
Terraform's configuration language, HCL, provides powerful capabilities for managing infrastructure. However, comparing complex data structures like lists of strings can sometimes be tricky. This article will guide you through various methods to compare two lists of strings, whether you need to check for exact equality, identify differences, or determine if one list is a subset of another. Understanding these techniques is crucial for writing robust and predictable Terraform configurations, especially when dealing with dynamic resource attributes or conditional logic.
Basic List Equality Check
The simplest form of comparison is checking if two lists are exactly equal, meaning they contain the same elements in the same order. Terraform's equality operator (==
) can be used for this purpose. However, it's important to note that this operator performs an exact, order-sensitive comparison. If the order of elements differs, even if the elements themselves are the same, the lists will be considered unequal.
locals {
list_a = ["apple", "banana", "cherry"]
list_b = ["apple", "banana", "cherry"]
list_c = ["cherry", "banana", "apple"]
list_d = ["apple", "banana"]
# Exact equality
are_a_b_equal = local.list_a == local.list_b # true
are_a_c_equal = local.list_a == local.list_c # false (order differs)
are_a_d_equal = local.list_a == local.list_d # false (different length)
}
Using the ==
operator for exact list equality.
==
operator for lists in Terraform performs an order-sensitive comparison. If you need to compare lists regardless of element order, you'll need a different approach.Order-Insensitive Equality and Difference
Often, the order of elements in a list doesn't matter; you just want to know if two lists contain the same set of unique elements. For this, converting lists to sets is the most effective method. Terraform's toset()
function converts a list into a set, which inherently removes duplicates and disregards order. Once converted to sets, you can use set operations to find equality, differences, or intersections.
flowchart TD A[List 1] --> B{Convert to Set 1} C[List 2] --> D{Convert to Set 2} B --> E{Compare Set 1 and Set 2} D --> E E --> F{Equality?} E --> G{Differences?} E --> H{Intersection?} F --> I[Boolean Result] G --> J[List of Differences] H --> K[List of Common Elements]
Flowchart for order-insensitive list comparison using sets.
locals {
list_x = ["alpha", "beta", "gamma"]
list_y = ["gamma", "alpha", "beta"]
list_z = ["alpha", "beta", "delta"]
set_x = toset(local.list_x)
set_y = toset(local.list_y)
set_z = toset(local.list_z)
# Order-insensitive equality
are_x_y_equal_set = local.set_x == local.set_y # true
are_x_z_equal_set = local.set_x == local.set_z # false
# Elements unique to list_x (not in list_z)
x_minus_z = tolist(setsubtract(local.set_x, local.set_z)) # ["gamma"]
# Elements unique to list_z (not in list_x)
z_minus_x = tolist(setsubtract(local.set_z, local.set_x)) # ["delta"]
# All elements present in either list_x or list_z (union)
x_union_z = tolist(setunion(local.set_x, local.set_z)) # ["alpha", "beta", "gamma", "delta"]
# Elements common to both list_x and list_z (intersection)
x_intersection_z = tolist(setintersection(local.set_x, local.set_z)) # ["alpha", "beta"]
}
Using toset()
, setsubtract()
, setunion()
, and setintersection()
for advanced list comparisons.
toset()
. This handles both ordering and duplicate elements automatically.Checking for Subsets
A common requirement is to check if all elements of one list are present in another list (i.e., if one list is a subset of another). This can also be achieved efficiently using set operations. If the intersection of two sets is equal to the smaller set, then the smaller set is a subset of the larger one.
locals {
main_list = ["red", "green", "blue", "yellow"]
subset_a = ["red", "blue"]
subset_b = ["red", "purple"]
set_main = toset(local.main_list)
set_subset_a = toset(local.subset_a)
set_subset_b = toset(local.subset_b)
# Check if subset_a is a subset of main_list
is_subset_a = setunion(local.set_main, local.set_subset_a) == local.set_main # true
# Alternatively, using setintersection:
# is_subset_a_alt = setintersection(local.set_main, local.set_subset_a) == local.set_subset_a # true
# Check if subset_b is a subset of main_list
is_subset_b = setunion(local.set_main, local.set_subset_b) == local.set_main # false
}
Determining if one list is a subset of another using set operations.
toset()
function requires all elements to be of the same type. If your list contains mixed types, you might encounter errors or unexpected behavior.