Sling Academy
Home/DevOps/Terraform: Removing leading and trailing whitespace from a string

Terraform: Removing leading and trailing whitespace from a string

Last updated: February 03, 2024

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.

Next Article: Sorting Lists in Terraform: A Practical Guide

Previous Article: Terraform: How to uppercase/lowercase a string

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