Skip to main content

can( ) and try( ) built-in functions of Terraform

There are a lot of built-in functions available in Terraform to use. These built-in functions can improve your code and its readability.

So let's get started with some of the built-in functions that can be beneficial for you while writing terraform code.

1. Can Function

can function evaluates the given expression and returns a boolean value. Return value would be 'true', if the expression produces a result without any errors otherwise it will return 'false'.

The primary purpose of can function is to turn an error condition into a boolean validation result when writing custom variable validation rules.

Let's take a look at a few examples to understand the working of can function. 

variable "eip_name" {
type = string
default = "eip-test"
validation {
condition = can(regex("^eip-", var.eip_name))
error_message = "The external IP name must be a valid name starting with \"eip-\"."
}
}

variable "vpc_name" {
type = string
default = "vpc-test"
validation {
condition = can(regex("^(?:[a-z](?:[-a-z0-9]{0,61}[a-z0-9])?)$", var.vpc_name))
error_message = <<EOF
The vpc name must be upto 62 characters long. The first character must be a lowercase
letter and all other characters must be a hyphen, lowercase letter, or digit, except
the last character, which cannot be a hyphen.
EOF
}
}

In both the variables mentioned in the above example, if regex produces an error, the can function will return false value and then error_message will be displayed by Terraform.

2. Try Function

try function evaluates all of its argument expressions in turn and returns the result of the first one that does not produce any errors.

Let's take an example and see the result by passing different types of value for the variable.

# Content of file: variables.tf
variable "example" {
type = any
}

# Content of file: main.tf
locals {
example = try(
[tostring(var.example)],
tolist(var.example),
"default"
)
}

# Content of file: outputs.tf
output "example" {
value = local.example
}

Case 01

Let's say we are passing the variable's value as a single string. The first expression will be evaluated successfully and it will be converted into a single-element list containing that string.

# Content of file: terraform.tfvars
example = "anupam"

# Console Output
Outputs:
example = [
"anupam",
]

Case 02

Let's say we are passing the variable's value as a tuple/set. The first expression will produce an error. But the second expression will convert it into a list containing the elements, including the implicit type conversion, if required and possible. 

# Content of file: terraform.tfvars
example = ["anupam", 00000]

# Console Output
Outputs:
example = tolist([
"anupam",
"0",
])

Case 03

Let's say we are passing the variable's value as an object. So, the first expression will throw an error as type conversion is not possible. Then the next expression will be checked. In this case, next will also throw an error as type conversion is not possible. Then the third expression will be evaluated. In our example, it will be successful and its value will be produced as a result.

# Content of file: terraform.tfvars
example = {
build_by = "anupam"
terraform = true
cost_center = 00000
}

# Console Output
Outputs:
example = "default"

Note: If no expression produces the success result, call to "try" function will fail. So make sure, at least one expression must produce a successful result in order to use the try function.

References