In this short article, we will look at the contains
function in Terraform, explain 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 share some practical examples. Hope it helps!
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Ā contains
Ā function is especially useful when you need to make decisions based on the contents of a list or set.
The syntax of Terraform contains
function 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.
Read more about Terraform functions, expressions, and loops.
In this example, Terraform contains
function is given a list and, after the comma, the value to search for. If the value exists, the function 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
.
contains(["darth", "luke", "yoda"], "jarjar")
false
One good preactical use of the contains
function is to check if a particular virtual machine size is supported in a specific Azure region before deploying it.
You can use theĀ contains
function to check if the desired VM size is present in the list of available sizes for that region.
provider "azurerm" {
features {}
}
variable "region" {
description = "Azure region"
type = string
default = "uksouth"
}
variable "vm_size" {
description = "VM size"
type = string
default = "Standard_DS2_v2"
}
data "azurerm_virtual_machine_sizes" "example" {
location = var.region
}
output "vm_size_supported" {
value = contains(data.azurerm_virtual_machine_sizes.example.sizes, var.vm_size)
}
Similar to the contains function, the Terraform strcontains
function checks whether a substring is within another string. This function is categorized as a āStringā function. The strcontains
function is useful for string manipulations and conditional logic based on string data.
Syntax of the Terraform strcontains
Ā function:
strcontains(string, substr)
string
ā The string to search within for the substring.substr
ā The value to search for within the main string.
Learn more: Terraform Strings: Interpolation, Multiline & Built-in Functions
In this example, given the first string specified as ādarthā we check for the substring within it ādarā. Because it exists, the Terraform stcontains
function returns true.
strcontains("darth", "dar")
true
In this second example, we check using the stcontains
function for the substring āyodaā within the word ādarthā. Because it is not contained within the string, it returns false.
> strcontains("darth", "yoda")
false
The Terraform contains
function is used to check if a specified value exists within a given list or set. And Terraform strcontains
Ā function checks whether a substring is within another string.
We encourage you also to exploreĀ how Spacelift makes it easy to work with Terraform. If you need any help managing your Terraform infrastructure, building more complex workflows based on Terraform, and managing AWS credentials per run, instead of using a static pair on your local machine, Spacelift is a fantastic tool for this.Ā It supports Git workflows, policy as code, programmatic configuration, context sharing, drift detection, and many moreĀ greatĀ features right out of the box. You can check it for free byĀ creating a trial account.
Note: New versions of Terraform are placed under the BUSL license, but everything created before version 1.5.x stays open-source. OpenTofu is an open-source version of Terraform that expands on Terraformās existing concepts and offerings. It is a viable alternative to HashiCorpās Terraform, being forked from Terraform version 1.5.6.
Manage Terraform Better with Spacelift
Build more complex workflows based on Terraform using policy as code, programmatic configuration, context sharing, drift detection, resource visualization, and many more.