Custom condition
The Custom Conditions enable you to define your own conditions or rules within a configuration. Such custom conditions are used to check the state or properties of different configuration objects. When such a custom condition is not fulfilled, it can come up with custom error messages instead of some generic, default error messages.
Terraform provides different kinds of custom conditions, which can best be suited for different use cases based on your needs. Some broad guidelines have been provided herein to help you choose the best custom condition that fits your needs:
- Validation conditions
- Preconditions and postconditions
- Check blocks with assertions
Validation conditions
To add custom conditions to validate the input variables, you can insert one or more
validation
blocks within the variable block. Each validation block contains:
condition
: This is an expression that must use the value of the variable to return true if the value is valid, or false if it is invalid. The expression should not throw an errors on it's own.
error_message
: If the condition expression evaluates to false, Terraform will produce an error message using this expression.
Let's take an example:
variable "server_port" { default = 8080 type = number description = "The port the server will use for HTTP requests" validation { condition = var.server_port > 1000 && var.server_port < 65536 error_message = "The port number must be between 1000 and 65536." } }
When you run terraform apply, the Terraform will check the value of server_port and ensure it is within that range (1000 and 65536). If the value is not valid, Terraform would display the error message defined with the error_message expression.
Multi-Level Validation conditions
In Terraform, you can specify more than one validation rule for a variable. The rules are checked one after another, and once any of the conditions fail, Terraform will show an error message. The interesting part here is that Terraform simply doesn't stop after getting the first failed validation rule, but rather it will go ahead and check all the remaining rules, returning error messages for all the failed conditions.
Let's take an example:
variable "username" { type = string validation { condition = length(var.username) >= 8 error_message = "Username must be at least 8 characters.." } validation { condition = length(var.username) <= 20 error_message = "Username must be no more than 20 characters." } validation { condition = can(regex("^[a-zA-Z0-9]+$", var.username)) error_message = "Username must only contain letters and numbers." } }
In the example below, we have three blocks of validation for the variable username. The first validation block will check if a username is at least 8 characters in length. The second validation block would check that the username is no longer than 20 characters. The third validation block checks whether the username consists only of letters and numbers.
Now, suppose we have assigned the value of the username variable to something that will make all of multiple conditions fail, for instance, "longerusername-it-will-fail". This value is Too long (more than 20 characters) and Contains a hyphen, which is not a letter or number. For this case, when we run terraform apply or plan, Terraform will return error messages for all validation conditions that failed:
terraform apply -var 'username=longerusername-it-will-fail'
Error: Invalid value for variable Username must be no more than 20 characters. Error: Invalid value for variable Username must only contain letters and numbers.
Related Pages
Feedback
Was this page helpful?