slide

Terraform fot d substr()

Ned Bellavance
3 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 substr() function. The example file is on GitHub here.

What is it?

Function name: substr(string, offset, length)

Returns: Takes a string, offset, and length. Returns a string as long as the length value and starting at the offset value.

Example:

# Returns abc
output "substr_output" {
  value = "${substr("abcdefg",0,3)}"
}

Example file:

##############################################
# Function: substr
##############################################
##############################################
# Variables
##############################################
variable "source" {
  default = "Don't Panic"
}

variable "offset" {}

variable "length" {}

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


##############################################
# Outputs
##############################################
output "1_substr_basic" {
  value = "${substr(var.source,var.offset,var.length)}"
}

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

#Standard substr
terraform apply -var "offset=0" -var "length=5"

#Both 0s
terraform apply -var "offset=0" -var "length=0"

#Negative offset
terraform apply -var "offset=-5" -var "length=5"

#Negative length
terraform apply -var "offset=5" -var "length=-1"

#Both Negative
terraform apply -var "offset=-5" -var "length=-1"

Why use it?

There are plenty of reasons why you might want a portion of a string. Take a hostname like prod-web-001. Maybe you need the server type, or the environment, or the number of the server. Assuming your name formatting is consistent, you can always use substr to extract the information you are looking for. There’s probably a ton of other examples. This is just the first that came to mind.

Lessons Learned

As the documentation explains, a negative offset starts at the last character and moves to the left. If a string is 10 characters long, then a -2 would start at the 9th character. This is great if you don’t know the length ahead of time. There is no length function for a string in Terraform, so the only way to know is either turn the string into a list and use the actual length function, or use a negative offset. A length of -1 corresponds to the end of the string, regardless of the length of the actual string. Don’t try and use a -2 for length, as it will not work. As in the previous example of a hostname, if you know the last three characters are the server number, then you can use an offset of -3 and a length of -1 to get the numbers consistently regardless of the length of the string. I have to say I really like the way this function works. You can tell someone had been frustrated by other substr functions and looked for a way to make things easier for the user.

Coming up next is the timestamp() function. It takes no arguments at all, because it is timeless.