Terraform – FotD – split()

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

What is it?

Function name: split(delimiter, string)

Returns: Takes a string and a delimiter. The string is split by the supplied delimiter and returned as a list of values.

Example:

# Returns [a, b, c]
output "split_output" {
  value = "${split(":","a:b:c")}"
}

Example file:

##############################################
# Function: split
##############################################
##############################################
# Variables
##############################################
variable "source" {
  default = "Arthur scratched his head and discovered an Arthur doppelganger. \nFord screamed and threw a towel over his head."
}

variable "delimiter" {
  default = "\n"
}

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


##############################################
# Outputs
##############################################
output "1_split" {
  value = "${split(var.delimiter,var.source)}"
}

output "original" {
  value = "${var.source}"
}

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

#Default values
terraform apply

#Comma delimited
terraform apply -var "source=One,Two,Three,Four" -var "delimiter=,"

#Double delimited
terraform apply -var "source=One::Two::Three::Four" -var "delimiter=::"

#Whitespace delimited - regular expressions not supported
terraform apply -var "source=One Two Three Four" -var "delimiter=\s"

Why use it?

The most common reason to use this in Terraform is to split a string that has been produced as output by a module. In their current form, modules can only supply output in the form of a string. So if you wanted to have a list as output, you would use the join function to turn it into a string, and then use the split function in the configuration calling the module to turn the output back into a list. With Terraform version 0.12 coming, that restriction should be lifted. There will still be external data sources that supply strings you’ll want to split, so this function isn’t going to become useless. But it probably won’t be as commonly used as it once was.

Lessons Learned

The function does exactly what it advertised. It only returns a flat list. There’s no fancy nested lists or map types available. You could create a map by using the map function once you have your list. That’s how you can get a map out of a module. It appears that escape sequences are supported, so I was able to use a tab-delimited string and a new-line delimited string. Multi-character delimiters are also supported, so you could use :: as the delimiter if you’re worried that your source data will match on single characters that are not a delimiter. Regular expressions for the delimiter are not supported, so you can’t just split on any whitespace character, or something funky like that. It probably would be nice to add regular expression capabilities – like what is in the replace function – but that does seem like overkill for most use cases.

Coming up next is the substr() function.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.