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 join() function. The example file is on GitHub here.
Function name: join(delimiter, list)
Returns: Takes a list and joins all the elements of the list using the delimiter. Returns the resultant string.
Example:
variable "list" {
default = ["one","two","three"]
}
# Returns "one-two-three"
output "join_output" {
value = "${join("-",var.list)}"
}
##############################################
# Function: join
##############################################
##############################################
# Variables
##############################################
variable "string_list" {
default = ["So", "long", "and", "thanks"]
}
variable "int_list" {
default = [41, 42, 43]
}
variable "empty_string_list" {
default = ["", "", ""]
}
variable "empty_list" {
default = []
}
variable "bool_list" {
default = [false, true]
}
variable "nested_list" {
default = [[41], [42]]
}
##############################################
# Resources
##############################################
##############################################
# Outputs
##############################################
#Test a standard list
output "1_join_list" {
value = "${join(" ",var.string_list)}"
}
#Test length
output "1b_join_list" {
value = "${join(",",var.string_list)}"
}
#Test wrap
output "1c_join_list" {
value = "${join("+",var.string_list)}"
}
#Test a standard int list
output "2_int_list" {
value = "${join("\n",var.int_list)}"
}
#Test list of empty strings
output "3_empty_string_list" {
value = "${join(" ",var.empty_string_list)}"
}
#Test what will happen with a list boolean values
output "4_bool_list" {
value = "${join("\\",var.bool_list)}"
}
output "5_nested_list_flatten" {
value = "${join("-",flatten(var.nested_list))}"
}
Run the following from the join folder to get example output for a number of different cases:
#All examples are in variables
terraform apply
This function is useful if you need to pass a list to another module or an outside source that doesn’t natively support a list object. Once the constructed string gets to the other side, a mechanism can pull it apart. There is a split function as well that can re-create the list. The output of a module doesn’t currently support a list, so you might join the list for the output and split it apart on the other side.
The function explicitly doesn’t deal with nested lists, so you’ll need to use flatten first. An example is provided. I also checked to see what escape sequences I could use as a delimiter. The only ones that seemed to work are newline (’\n’) and backslash (’\’). A sequence like (’\t’) resulted in an error that the escape sequence is invalid. The delimiter can be multiple characters, so if you suspect that your list might have a common character, like a comma, already in it, then you could use a combination of characters instead.
Coming up next is the jsonencode() function.
Resourcely Guardrails and Blueprints
November 15, 2024
Deploying Azure Landing Zones with Terraform
November 12, 2024
October 18, 2024
What's New in the AzureRM Provider Version 4?
August 27, 2024