slide

Terraform fot d upper()

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 upper() function. The example file is on GitHub here.

What is it?

Function name: upper(string)

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

Example:

variable "string" {
  default = "all lower"
}

# Returns "ALL LOWER"
output "upper_output" {
  value = "${upper(var.string)}"
}

Example file:

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

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

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

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

##############################################
# Outputs
##############################################
output "upper_output" {
  value = "${upper(var.upper)}"
}

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

Run the following from the upper 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 "upper=THIS IS IN ALL CAPITALS"

#All lowercase
terraform apply -var "upper=this is in all lowercase"

#Empty string
terraform apply -var "upper="

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

Why use it?

I had some really good ideas on why to use the lower function. There are many cases of resources that will not accept uppercase characters as valid input, so lower makes sense. I can’t think of a single service that uses all uppercase. Maybe you use this for metadata tagging to really bring attention to something. Or for your SQL statement, which seem to always want to be in uppercase. Or this was just included for completeness.

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 uppercase 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 urlencode() function.