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.
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)}"
}
##############################################
# 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,./;[]\<>?:"{}|~!@#$%^&*()_+-=`'
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.
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.
October 18, 2024
What's New in the AzureRM Provider Version 4?
August 27, 2024
Debugging the AzureRM Provider with VSCode
August 20, 2024
State Encryption with OpenTofu
August 1, 2024