770 words
4 minutes

Mastering Terraform Contains and Strcontains Functions

Cover image for Mastering Terraform Contains and Strcontains Functions

Terraform’s a declarative language — which is just a fancy way of saying you don’t get if-statements like a normal programmer. So when you need to validate inputs, control behavior, or gate deployments, you reach for the logic tools Terraform does give you.

Two of the most deceptively simple — and ridiculously useful — are contains() and strcontains().

If you’ve ever been bitten by a bad variable, a missing VM size, or a misnamed AZ, this post is for you.

Let’s break these two down. Sharp, real, no fluff.


contains(): Check If It’s In There — Or Burn a Saturday Debugging#

The contains() function checks whether a specific value exists inside a list or a set.

contains(list, value) => bool

It returns true if the list contains the value. false if it doesn’t. That’s it.

Sounds basic? Sure. But when your infra logic starts depending on user input, region capabilities, or feature flags, this little guy becomes the bouncer at the front of your Terraform nightclub.

Real-World Example: Azure VM Sizes#

Let’s say you’re deploying to Azure, and someone on your team decides to request a beefy VM in a tiny region that doesn’t support it. You want to stop that mistake before the apply fails.

variable "region" {
default = "uksouth"
}
variable "vm_size" {
default = "Standard_DS2_v2"
}
data "azurerm_virtual_machine_sizes" "example" {
location = var.region
}
output "is_supported" {
value = contains(data.azurerm_virtual_machine_sizes.example.sizes, var.vm_size)
}

If vm_size isn’t available in that region, it returns false. You can use this in a count, a for_each, or as part of a validation block. Either way, it’s miles better than letting Terraform barf mid-deploy.


strcontains(): When You’re Parsing Strings Like It’s Bash Again#

Now let’s talk about strcontains() — Terraform’s way of answering the question: “Does this string have that other string inside it?”

strcontains(string, substr) => bool

Use it when you don’t have a list, just a single string — like an AZ name, tag, or label — and want to match patterns.

Example: Is This AZ Optimized?#

strcontains("us-east-1b-optimal", "optimal") // returns true

This is especially handy when providers or modules return strings with embedded metadata — and you want to route logic accordingly.

Say you only want to run a deployment if the target zone is labeled “optimal”:

locals {
is_optimal_zone = strcontains(var.availability_zone, "optimal")
}

Now local.is_optimal_zone becomes your condition switch — whether for creating resources, setting tags, or adding taints.


Gotchas (That Bit Me, So You Don’t Have To)#

  • contains() cares about exact values. Case-sensitive. No fuzzy matches.
  • strcontains() won’t match regex or wildcards. It’s pure substring.
  • Maps don’t work with contains() the way you want. Only lists and sets. If you try contains({ key = "val" }, "key") — expect disappointment.
  • Nulls can mess with results. Validate your variables, or wrap with coalesce() if needed.

Bonus: Validate with Style#

Want to enforce logic on inputs? Use validation blocks with these functions.

variable "az" {
type = string
default = "us-east-1b-optimal"
validation {
condition = strcontains(var.az, "optimal")
error_message = "Only 'optimal' AZs are allowed for this deployment."
}
}

Now Terraform fails early — with a message that makes sense — instead of crashing halfway through your infra plan.


TL;DR#

  • Use contains() when working with lists or sets.
  • Use strcontains() for substring checks in single strings.
  • Combine them with validation, count, or for_each for clean, safe logic in your modules.
  • Don’t guess. Test.

Social Channels#


Community of IT Experts#


Is this content AI-generated?

No. Every article on this blog is written by me personally, drawing on decades of hands-on IT experience and a genuine passion for technology.

I use AI tools exclusively to help polish grammar and ensure my technical guidance is as clear as possible. However, the core ideas, strategic insights, and step-by-step solutions are entirely my own, born from real-world work.

Because of this human-and-AI partnership, some detection tools might flag this content. You can be confident, though, that the expertise is authentic. My goal is to share road-tested knowledge you can trust.

Mastering Terraform Contains and Strcontains Functions
https://www.heyvaldemar.com/mastering-terraform-contains-and-strcontains-functions/
Author
Vladimir Mikhalev
Published at
2024-05-25
License
CC BY-NC-SA 4.0