slide

Terraform fot d title()

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

What is it?

Function name: title(string)

Returns: Takes a string and returns that string with the first letter of every word capitalized

Example:

# Vogon Jeltz Is My Hero
output "title_output" {
  value = "${title("Vogon Jeltz is my hero")}"
}

Example file:

##############################################
# Function: title
##############################################
##############################################
# Variables
##############################################
variable "source" {
  default = "don't panic"
}

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


##############################################
# Outputs
##############################################
output "1_title_basic" {
  value = "${title(var.source)}"
}

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

#Default
terraform apply

#Inverse Caps, capitalizes everything
terraform apply -var "source=oNCE uPON a tIME"

#all lower case
terraform apply -var "source=the restaurant at the end of the universe"

#empty string
terraform apply -var "source="

Why use it?

I guess you would use this to make things look nice for cases where putting something in Titlecase matters. Honestly, there are some major issues with this function.

Lessons Learned

The function does exactly what you would expect, mostly. It counts an apostrophe as the beginning of a new word. If you run it against don’t panic, you get back Don’T Panic. I checked a few others and a dash ‘-’ is also consider a word separator, so state-of-the-art becomes State-Of-The-Art. Those are probably both wrong. A larger issue is that Titlecase is a real thing that has rules. And this function does not follow those rules. If a developer is going to add a function that does Titlecase, then it should do it reasonably well. Or don’t add it. In all fairness, this function uses the Golang title function in the strings package to do the work. And that function has a bug:

BUG(rsc): The rule Title uses for word boundaries does not handle Unicode punctuation properly.

Still, I think if you have a function that is trying to provide Titlecase, it should do that. Or at least know that an apostrophe is not a word separator.

Coming up next is the transpose() function.