Using try() and coalesce() functions in Terraform (with examples)

Updated: February 3, 2024 By: Guest Contributor Post a comment

Introduction

In the world of infrastructure as code, the need for flexibility and error handling in configurations cannot be overstated. Terraform, a popular open-source tool developed by HashiCorp, allows for the building, changing, and versioning of infrastructure safely and efficiently. This tutorial delves into the try() and coalesce() functions in Terraform, providing you with a foundation to manage potential errors and variable configurations with ease in your infrastructure code.

Using try() Function

When you’re defining your infrastructure as code with Terraform, it’s common to encounter situations where a certain value may not be present or an operation might fail due to various reasons. The try() function lets you handle these scenarios gracefully. The try() function attempts to evaluate a given set of expressions and lets you specify a default value in case of failure.

Basic Usage of try() Function

resource "random_pet" "example" {
  keeper = try(var.maybe_undefined, "default-value")
}

In the example above, the try() function checks if var.maybe_undefined exists. If not, it falls back to “default-value”.

Advanced Use Cases of try() Function

Consider a more complex scenario where you might want to capture more specific data types or structures, e.g., a list:

output "example_list" {
  value = try(tostring(var.maybe_list[0]), "No first item")
}

This code tries to convert the first item of the list var.maybe_list to a string. If the list is undefined or empty, it returns “No first item”.

Using coalesce() Function

While try() is about catching errors, the coalesce() function is about dealing with nulls and undefined values in a list of given inputs. It returns the first non-null value in the given list.

Basic Usage of coalesce() Function

variable "preferred_value" {
  type = string
  default = null
}

variable "fallback_value" {
  type = string
  default = "fallback"
}

output "value" {
  value = coalesce(var.preferred_value, var.fallback_value)
}

In the example above, the coalesce() function checks each variable and returns the first one that isn’t null, preferentially var.preferred_value if it’s defined, or var.fallback_value otherwise.

Combining try() and coalesce() Functions

By combining these two powerful functions, you can significantly enhance the robustness of your Terraform configurations.

output "robust_output" {
  value = try(coalesce(var.dynamic_value, var.static_value), "default-safe-value")
}

In the example above, coalesce() attempts to find the first non-null value between var.dynamic_value and var.static_value. If both are null, or if there’s an error in evaluating the coalesce() function itself, try() catches that and returns “default-safe-value”.

Conclusion

Understanding and effectively using the try() and coalesce() functions in Terraform can significantly enhance the reliability and maintainability of your infrastructure as code projects. By gracefully handling errors and effectively managing default values, you ensure that your configurations remain robust under various conditions. Embrace these functions in your next project, and you’ll find your work with Terraform becoming more efficient and error-free.