Sling Academy
Home/DevOps/Terraform: How to check if a string contains a substring

Terraform: How to check if a string contains a substring

Last updated: February 04, 2024

Introduction

Terraform has become an indispensable tool in the world of Infrastructure as Code (IaC), allowing developers and operations teams to manage their infrastructure more efficiently and predictably. While mostly used for defining and provisioning infrastructure, dexterity with data types and operations, such as string manipulations, can be just as crucial for scripting conditionals and dynamic configurations. In this tutorial, we’ll explore different strategies to check if a string contains a certain substring in Terraform, moving from basic examples to more advanced use cases.

How Strings Work in Terraform?

Before diving into string manipulation, it’s important to understand how strings work in Terraform. A string in Terraform is a sequence of characters used to represent text data. Terraform allows for variable interpolation and a set of built-in functions that can operate on strings, providing powerful capabilities for string manipulation.

Checking if a String Contains a Substring

The most straightforward way to determine if a string contains another string is to use the contains function. This function takes two arguments: the whole string and the substring to search for. It returns true if the substring is found within the string and false otherwise.

locals {
  example_string = "Hello, World!"
  search_string = "World"
}

output "contains_example" {
  value = contains(local.example_string, local.search_string)
}

In this example, the output will be true, as “World” is indeed a part of “Hello, World!”.

Case Sensitivity

It’s important to note that the contains function in Terraform is case-sensitive. This means that searching for “world” in our previous example would result in false. To perform a case-insensitive search, you can use the lower or upper functions to normalize both the string and the substring before performing the search.

locals {
  example_string = "Hello, World!"
  search_string = "world"
}

output "case_insensitive_search" {
  value = contains(lower(local.example_string), lower(local.search_string))
}

This modifies both the example string and the search string to lower case, ensuring that the contains function can find the substring regardless of its case.

For more complex substring search needs, Terraform provides the regex function, which matches a string against a regular expression. This is especially useful when you need to check for a substring matching a specific pattern rather than a fixed sequence of characters.

locals {
  example_string = "Your total is: $123.45"
  pattern = "\\\d+\\.\\\d+"
}

output "regex_example" {
  value = regex(local.pattern, local.example_string)
}

This example searches for a substring pattern that matches one or more digits followed by a dot and two digits, effectively finding the amount in the string. Note the escaping required in Terraform for backslashes in regular expressions.

Combining Methods for Complex Scenarios

In real-world Terraform scripts, you may encounter scenarios where a combination of the methods described above is necessary to achieve the desired outcome. Considering performance and readability should guide your approach to string manipulation in such cases.

Performance Considerations

While string manipulation functions in Terraform are quite efficient, excessive use in large-scale Terraform configurations can impact performance. It’s advisable to use string manipulation sparingly and consider alternative approaches, such as structuring your Terraform configurations to minimize dynamic content, where possible.

Conclusion

Checking if a string contains a certain substring in Terraform can be achieved through various methods, from utilizing the basic contains function to leveraging regular expressions for more complex searches. Understanding these techniques and their appropriate use cases enables Terraform users to craft more dynamic and versatile infrastructure configurations. Mastery of string manipulation in Terraform denotes a significant step forward in utilizing the full potential of Infrastructure as Code.

Next Article: Terraform: How to replace all occurrences of a substring in a string

Previous Article: Using Regular Expressions in Terraform: A Practical Guide

Series: Terraform Tutorials

DevOps

You May Also Like

  • How to reset Ubuntu to factory settings (4 approaches)
  • Making GET requests with cURL: A practical guide (with examples)
  • Git: What is .DS_Store and should you ignore it?
  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide