try()
The function that returns the first of its arguments that evaluates without an error, used to supply a fallback.
In more detail
try(local.names[0], "nobody") returns the first name, or "nobody" if the list is empty. Use it for deliberate fallbacks, not to hide real mistakes.
Example
first = try(local.normalised[0], "nobody")Related terms
- built-in function — One of Terraform's fixed set of functions, such as lower(), join() or jsonencode(), that transform values inside expressions.
- conditional expression — An expression that checks a condition and evaluates to one of two values, one for true and one for false.
Taught in
- Functions And Expressions In HCL — Infrastructure as Code with Terraform