Solving the DevOps Infrastructure Dilemma: Enabling developer velocity with control ūüí°

Register for the webinar here ‚Üí

Terraform

Terraform Contains Function [List & String Examples]

terraform contains

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!

  1. What is the Terraform contains function?
  2. How to use Terraform contains – examples
  3. What is Terraform stcontains function?
  4. How to use Terraform stcontains – examples

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¬†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.

How to use Terraform contains - examples

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)
}

What is Terraform stcontains function?

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.

How to use Terraform stcontains - examples

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

Key points

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 will be 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 will expand 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.

Start free trial

Struggling to balance developer velocity with control?

Attend the June 25 webinar:

Solving the DevOps Infrastructure Dilemma

Register for the webinar