slide

Terraform fot d coalesce()

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

What is it?

Function name: coalesce(string,string,…)

Returns: Takes two or more strings and returns the first non-empty string in the list. Must include at least two arguments to evaluate. Does not accept a list of strings.

Example:

variable "string" {
  default = "some_string"
}

variable "empty_string" {
  default = ""
}

# Returns some_string
output "coalesce" {
  value = "${coalesce(var.empty_string,var.string)}"
}

Example file:

##############################################
# Function: coalesce
##############################################
##############################################
# Variables
##############################################
variable "1_string" {
  default = false
}

variable "2_string" {
  default = 0
}

variable "3_string" {
  default = "three"
}

variable "4_string" {
  default = "four"
}

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

##############################################
# Outputs
##############################################

output "coalesce_output" {
  value = "${coalesce(var.1_string,var.2_string,var.3_string,var.4_string)}"
}

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

#Evaluate to one
terraform apply -var "1_string=one" -var "2_string=two" -var "3_string=three" -var "4_string=four"

#First string is empty, evaluate to second string
terraform apply -var "1_string=" -var "2_string=two" -var "3_string=three" -var "4_string=four"

#What will a default value of bool false do? Evaluate as string 0
terraform apply -var "2_string=two" -var "3_string=three" -var "4_string=four"

#What will a default value of int 0 do? Evaluate as string 0
terraform apply -var "1_string=" -var "3_string=three" -var "4_string=four"

#All empty, evaluate to empty
terraform apply -var "1_string=" -var "2_string=" -var "3_string=" -var "4_string="

Why use it?

I have to admit I was a little stumped on when you would actually use this. It seems like you would need a situation where you would check the output of multiple resources, and then be okay with selecting the first non-empty value as the value you would want to use. I am sure there is a situation for this, but I have no idea what it might be.

Lessons Learned

The function will not take a list as a value, only multiple strings, which I found really odd. The function will evaluate the boolean value false as an empty string. It evaluates the int value of 0 as a string value of 0. Other than that, it does precisely what it advertises. I do find the naming a little confusing, since coalesce means to combine multiple things into a single entity. But then I found that there is a Coalesce function in T-SQL that also returns the first non-null value in a list. I suppose in the world of programming, coalesce has this other established definition.

Coming up next is the coalescelist() function, you’ll never guess what it does.