Terraform: How to uppercase/lowercase a string

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

Introduction

When working with Terraform, manipulating strings is a common task that could range from constructing identifiers dynamically to adjusting configurations based on certain inputs. In particular, converting string cases might seem trivial but can be crucial for adhering to naming conventions, compatibility with external systems, or just ensuring consistency across your infrastructure as code. Terraform provides several built-in functions that make string manipulation, including changing cases, into a straightforward process.

This tutorial aims to guide you through various ways to convert strings to uppercase or lowercase in Terraform. Through incremental complexity, we will explore basic to advanced scenarios, showcasing how Terraform’s function-based approach offers flexibility and power in seemingly simple operations. Whether you’re new to Terraform or looking to deepen your understanding, this guide will illuminate the possibilities and practical uses of string case conversion within your configurations.

Getting Started

Before diving into case conversion, ensure you have Terraform installed and are familiar with the basics of writing Terraform configurations. Familiarity with variables and outputs will be particularly useful as we proceed.

Basic Case Conversion Functions

To begin, let’s explore the two primary functions Terraform provides for changing the case of strings:

  • upper(string): Converts all characters in the given string to uppercase.
  • lower(string): Converts all characters in the given string to lowercase.

Example:
In a simple scenario, you might have a variable that stores an environment name, and you wish to convert this to uppercase to use as part of a resource identifier.

variable "env_name" {
  type = string
  default = "development"
}

output "uppercase_env_name" {
  value = upper(var.env_name)
}

Output:
uppercase_env_name = DEVELOPMENT

This same approach applies for converting to lowercase, which might be useful for creating DNS records, for example.

Advanced Scenarios

Building on the basics, Terraform’s functional programming approach allows for combining functions to achieve more complex manipulations. Consider scenarios where you need to enforce naming conventions, or integrate with systems that are case sensitive.

Combining Functions for Case Conversion

It’s possible to nest functions within Terraform to perform multiple manipulations in sequence. For instance, converting a string to uppercase and then trimming whitespace can be done by nesting the trim function around the upper function.

variable "complex_string" {
  type = string
  default = " SomeValue "
}

output "manipulated_string" {
  value = trimspace(upper(var.complex_string))
}

Output:
manipulated_string = SOMEVALUE

Such combinations are essential when preparing strings for scenarios where formatting and case are critical.

Conditional Case Conversion

Terraform also permits dynamic string manipulation based on conditions. Using the condition ? true_val : false_val syntax, you can decide to convert a string’s case based on external factors, such as whether an environment is classified as production.

variable "env" {
  type = string
  default = "Prod"
}

output "dynamic_case_env" {
  value = var.env == "Prod" ? upper(var.env) : lower(var.env)
}

Output:
dynamic_case_env = PROD for a production environment, and dynamic_case_env = stag for any other given environment named “stag”.

Using External Data for Case Conversion

Expanding the scope, Terraform can manipulate strings pulled from external sources, such as files or environment variables. This capability enhances your configurations’ adaptability to external inputs, while still allowing for sophisticated manipulation, including case conversion.

data "local_file" "example" {
  filename = "${path.module}/input.txt"
}

output "file_content_uppercase" {
  value = upper(data.local_file.example.content)
}

In this scenario, Terraform reads a local file and converts its contents to uppercase, demonstrating how external data can be seamlessly integrated and manipulated within your infrastructure configurations.

Conclusion

Whether for ensuring compatibility with external systems, adhering to naming conventions, or simply for aesthetic reasons, changing the case of strings within Terraform is both simple and powerful. The functions upper and lower, along with Terraform’s ability to nest functions and use conditions, provide a flexible toolset for manipulating strings to suit any requirement. As shown through the examples, starting from basic transformations to more complex manipulations involving external data, Terraform’s string functions are essential tools in the developer’s toolbox for crafting dynamic, resilient, and consistent infrastructure code.