Terraform Contains Function
List & String Examples
In this short article, we will take a look at the contains
function in Terraform, explaining what it is, and what it does, and share some practical examples using lists and strings. We will then compare it to the strcontains
function and also share some practical examples. Hope it helps!
What is the Terraform contains function?
In Terraform, the contains
function is used to check if a specified value exists within a given list or set. This function returns true
if the value is found, and false
otherwise. The function is categorized as a ‘collection’ function.
The syntax is as follows:
contains(list, value)
list
: The list, map, or set to search within.value
: The value to search for within the list or set.
How to use Terraform contains — examples
In this example, given a list, the value after the comma is searched for, if it exists, it returns true. Because darth
is in the first list it returns true.
contains(["darth", "luke", "yoda"], "darth")
true
In the second example because jarjar
is not in the list, the function returns false.