They try function in HashiCorp Terraform allows values to be read from a Data Source, input variables, or some other attribute while providing a default value if the evaluation expression errors. This is particularly useful when data is retrieved from an external system in JSON or YAML format and then decoded. The decoded result may have attributes that are not guaranteed to exist or be set. The try function can be used to normalize the data structure to a predictable type and/or value to be used within the Terraform configuration. This article will explain how to use the try function to normalize a predictable value, or a soft of default, in the event that the variable or attribute isn’t set.

Terraform try Function Syntax

The Terraform try function is able to catch errors that occur when evaluating the arguments passed to the function. This is particularly useful when working with data whose structure is not well known at the time of implementation.

The try function has the following signature:

try(<expression>, <value>)
  • <expression> – This can be any expression that retrieves the value of a variable or attribute. This includes just a attribute value accessor, or even a more complex value evaluation.
  • <value> – This is the value to return from the try function if evaluating the expression errors. This enables a sort of “default” value to be used if the attribute is not set or theres an error evaluating the expression.

Here are a couple examples of what could be used for the expression and value attributes of the function:

// local variable
try(local.is_dev, false)

// attribute values
try(var.environment.is_dev, false)
try(var.environment.region, "eastus")

// boolean evaluation
try(var.environment == "Production", false)

Practical Example of Terraform try Function

When configuring resources, the attributes of those resources can be set using Terraform expressions and variable values to conditionally configure and provision the resources. There may be times where the structure of data may no be known exactly at the time of implementation, but the Terraform code must be written to gracefully handle errors in the data structure. To do this the Terraform try function can be used.

Here’s an example of using the Terraform try function to set the region location of a Microsoft Azure resource (via the azurerm provider) in a case where the data structure of a local variable may not be known at implementation:

locals {
  // imagine the JSON is loaded from some external source
  env_data = jsondecode("{\"domain\": \"build5nines.com\"}")
}

resource azurerm_resource_group b59_rg {
  name = "b59-rg"
  // set location based on decoded external data
  // use "eastus" if external source doesn't have it set
  location = try(local.env_data.location, "eastus")
}

In this example:

  • JSON data is decoded into a local variable named local.env_data. Imaging the JSON loaded from an external source as it would be in a real Terraform project.
  • The Azure Resource Group (via the azurerm_resource_group resource) is configured, with the location set based on the .location attribute of the decoded local.env_data data structure. The location is set using a try so that the value of "eastus" is used if the local.env_data.location data attribute is not set for some reason.

If this Terraform code referenced the local.env_data.location attribute of the data structure without using the try method, then Terraform would error when attempting to run a terraform apply on the project. By using the try function, then in the event that the local.env_data.location attribute of the data structure is not set, then the value of "eastus" will be used to set the value of the azurerm_resource_group.b59_rg.location attribute. This allows for a sort of “default” value to be applied in areas that would otherwise error, thus making the Terraform code more resilient with built in Convention over Configuration (CoC) practices.

Related: The above example uses the jsondecode function to decode a JSON string to a data object. For more help on working with JSON in Terraform, go read the “Terraform: How to work with JSON” article written by Chris Pietschmann.

Chris Pietschmann is a Microsoft MVP, HashiCorp Ambassador, and Microsoft Certified Trainer (MCT) with 20+ years of experience designing and building Cloud & Enterprise systems. He has worked with companies of all sizes from startups to large enterprises. He has a passion for technology and sharing what he learns with others to help enable them to learn faster and be more productive.
Microsoft MVP HashiCorp Ambassador

Discover more from Build5Nines

Subscribe now to keep reading and get access to the full archive.

Continue reading