How to Check if String Contains a Substring in Terraform Interpolation?
TLDR
To check if a string contains a substring in Terraform, use the can
function with regex
or the contains
function for lists. These methods allow you to perform substring checks dynamically.
Terraform provides several ways to check if a string contains a substring, depending on your use case. This guide explains how to achieve this using practical examples.
Step 1: Use the regex Function
The regex
function allows you to match substrings using regular expressions.
Example
variable "example_string" {
default = "hello world"
}
locals {
contains_hello = can(regex("hello", var.example_string))
}
output "result" {
value = local.contains_hello
}
Explanation
regex("hello", var.example_string)
: Checks if the string contains the substringhello
.can
: Returnstrue
if the regex matches, otherwisefalse
.local.contains_hello
: Stores the result of the check.
Step 2: Use the contains Function for Lists
If you are working with a list of strings, use the contains
function.
Example
variable "example_list" {
default = ["hello", "world"]
}
locals {
contains_hello = contains(var.example_list, "hello")
}
output "result" {
value = local.contains_hello
}
Explanation
contains(var.example_list, "hello")
: Checks if the list contains the stringhello
.local.contains_hello
: Stores the result of the check.
Step 3: Combine with Conditional Logic
Use the result of the substring check in conditional logic.
Example
variable "example_string" {
default = "hello world"
}
locals {
message = can(regex("hello", var.example_string)) ? "Substring found" : "Substring not found"
}
output "result" {
value = local.message
}
Explanation
can(regex("hello", var.example_string))
: Checks for the substring.? "Substring found" : "Substring not found"
: Returns a message based on the result.
Best Practices
- Use Descriptive Variable Names: Clearly indicate the purpose of each variable.
- Validate Inputs: Ensure strings are in the expected format before performing checks.
- Document Logic: Provide clear comments for complex expressions.
By following these steps, you can effectively check if a string contains a substring in Terraform, enabling dynamic and flexible configurations.
Published: 2025-02-23|Last updated: 2025-02-23T09:00:00Z
Found an issue?