Terraform: Removing leading and trailing whitespace from a string

Updated: February 3, 2024 By: Guest Contributor Post a comment

Introduction

When working with Terraform, managing string values efficiently becomes a core necessity, especially when handling user inputs, configuration data, or output from other resources. Often, these strings contain unwanted leading or trailing spaces that could lead to errors or misconfigurations. This tutorial aims to demonstrate various methods for removing leading and trailing whitespace from strings in Terraform, guiding you from basic to more advanced techniques and scenarios.

Basic String Trimming in Terraform

Let’s begin with the basics. Terraform provides a built-in function, trimspace, specifically designed to remove both leading and trailing whitespace from a string.

variable "sample_string" {
  description = "A string with leading and trailing spaces"
  default     = "    Terraform is awesome!    "
}

output "trimmed_string" {
  value = trimspace(var.sample_string)
}

When you apply this configuration, the output will be:

Terraform is awesome!

This example illustrates the simplicity and effectiveness of the trimspace function in removing unwanted spaces.

Utilizing Regular Expressions

For scenarios where you need more control or have to deal with strings containing whitespace in places other than just the leading or trailing ends, Terraform’s regex and regex_replace functions come in handy. Here’s a more advanced example:

variable "complex_string" {
  default = "    Hello,    Terraform!
    "
}

output "advanced_trim" {
  value = regex_replace(var.complex_string, "^\\s+|\\s+$", "")
}

The regex pattern ^\s+|\s+$ used in regex_replace targets both leading (^\s+) and trailing (\s+$) whitespace characters. As a result, whitespace from both ends of the string is removed, while the space within the string is preserved. Your output should appear as follows:

Hello,    Terraform!

Combining Strings and Trimming Whitespace

Sometimes you might need to concatenate multiple strings and then trim any unwanted whitespace from the resulting string. Let’s look into how Terraform allows us to do this efficiently.

variable "first_part" {
  default = "  Terraform "
}

variable "second_part" {
  default = "    Rocks!  "
}

output "combined_trimmed" {
  value = trimspace("${var.first_part}${var.second_part}")
}

In this example, we’ve concatenated two strings with leading and trailing spaces and applied trimspace to the result. This effectively trims the unwanted space from the start and end of the combined string, with the output:

Terraform     Rocks!

Trimming Spaces from Lists of Strings

In advanced Terraform configurations, you might also deal with lists of strings, each potentially having leading or trailing spaces. Imagine you’re creating a dynamic configuration where each element needs trimming. Here’s how you could approach it:

variable "list_of_strings" {
  default = [
    "   Hello,   ",
    "  Terraform!  ",
    "    Rocks!    "
  ]
}

output "list_trim" {
  value = [for s in var.list_of_strings: trimspace(s)]
}

This code iterates over each element in the list, applying trimspace to each, resulting in a new list with all spaces trimmed. The output is a clear set of strings:

[
  "Hello,",
  "Terraform!",
  "Rocks!"
]

Conclusion

Throughout this tutorial, we explored how Terraform provides powerful and straightforward methods to handle leading and trailing whitespace in strings. Starting from the basic use of the trimspace function, advancing through to regex solutions, and dealing with lists, Terraform offers the tools you need to manage string data cleanly and effectively. These techniques not only simplify your code but also help in preventing potential configuration issues caused by unintended whitespace, making your infrastructure deployments more resilient and predictable.