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) => boolIt 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) => boolUse 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 trueThis 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 trycontains({ 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, orfor_eachfor clean, safe logic in your modules. - Donât guess. Test.
SIGNAL & INTEL
- The Private Order: Stop being a grunt. Become an Architect. Join The Private Order.