slide

Terraform fot d lower()

Ned Bellavance
2 min read

Cover

This is part of an ongoing series of posts documenting the built-in interpolation functions in Terraform. For more information, check out the beginning post. In this post I am going to cover the lower() function. The example file is on GitHub here.

What is it?

Function name: lower(string)

Returns: Takes a string and returns the lowercase version of all characters in the string as a string.

Example:

variable "string" {
  default = "ALL CAPS"
}

# Returns "all caps"
output "lower_output" {
  value = "${lower(var.string)}"
}

Example file:

##############################################
# Function: lower
##############################################
##############################################
# Variables
##############################################
variable "lower" {
  default = "This Line Is Capitalized."
}

variable "sourcefile" {
  default = "textFile.txt"
}

##############################################
# Resources
##############################################

data "local_file" "source" {
  filename = "${var.sourcefile}"
}

##############################################
# Outputs
##############################################
output "lower_output" {
  value = "${lower(var.lower)}"
}

output "file_output" {
  value = "${lower(data.local_file.source.content)}"
}

Run the following from the lower folder to get example output for a number of different cases:

#We have to get the local resource first
terraform init

#Default values
terraform apply

#All caps
terraform apply -var "lower=THIS IS IN ALL CAPITALS"

#Already lowercase
terraform apply -var "lower=this is in all lowercase"

#Empty string
terraform apply -var "lower="

#All standard US-EN characters
terraform apply -var 'lower=QWERTYUIOPASDFGHJKLZXCVBNM1234567890,./;[]\<>?:"{}|~!@#$%^&*()_+-=`'

Why use it?

There are a number of resources that will not accept uppercase characters for some of their values. For instance, storage accounts in Microsoft Azure need to be all lowercase for… well, for reasons obviously. Yeah, not sure why Microsoft decided to start caring about case for this one service when they have been case insensitive forever. But my point stands regardless, if you have a resource that won’t accept uppercase characters, then don’t trust the end user to know that, just make it lowercase for them.

Lessons Learned

The function does precisely what you would expect. I only tested it with standard EN-US characters, so your mileage could vary. It should be able to do lowercase translation for any character in the Unicode set. There are 1,781 uppercase characters in Unicode version 11. If you’d like to test all of them, have fun! Personally I will trust the Go function that is being called here to do the right thing.

Coming up next is the map() function.