Custom Variable Validation in Terraform 0.13
We’re excited to announce that custom variable validation is being released as a production-ready feature in Terraform 0.13. Custom Variable Validation was introduced as a language experiment in Terraform 0.12.20 and builds upon the type system introduced in Terraform 0.12 by allowing configurations to contain validation conditions for a given variable.
In the example below, we define a condition for the variable ami_id
which requires the id to begin with the string ami
:
variable "ami_id" {
type = string
validation {
condition = (
length(var.ami_id) > 4 &&
substr(var.ami_id, 0, 4) == "ami-"
)
error_message = "The ami_id value must start with \"ami-\"."
}
}
Each variable
block can have zero or more validation
blocks, allowing an author to potentially write a specific error message associated with each distinct check.
The condition
expression is evaluated in a reduced evaluation context that supports all of Terraform's built-in functions but allows referring only to the variable being validated — in this case, var.ami_id
— which evaluates to the value given by the caller after any automatic conversion to the given type constraint string
. The expression can therefore safely assume var.ami_id
is a string in this case.
If the expression returns true
then the validation is considered to have succeeded. If it returns false
then validation has failed and Terraform will return an error at the module call site, using the text given in error_message
:
Error: Invalid value for variable
on variable-validation-rfc.tf line 4, in module "example":
4: ami_id = "amo-abc123"
The ami_id value must start with "ami-".
This was checked by the validation rule at example/variables.tf:4,3-13.
Terraform’s custom variable validation takes the ergonomics of this feature one step further and will distinguish between an error produced by validation of the variable itself or an error with the expression used as the condition for validation. More information is available in the Terraform documentation.
Download the latest Terraform 0.13 pre-release today and give custom variable validation a try. Don’t forget to join us on our community forums for questions and discussion about this and the other new features of Terraform 0.13.
Sign up for the latest HashiCorp news
More blog posts like this one
Fix the developers vs. security conflict by shifting further left
Resolve the friction between dev and security teams with platform-led workflows that make cloud security seamless and scalable.
HashiCorp at AWS re:Invent: Your blueprint to cloud success
If you’re attending AWS re:Invent in Las Vegas, Dec. 2 - Dec. 6th, visit us for breakout sessions, expert talks, and product demos to learn how to take a unified approach to Infrastructure and Security Lifecycle Management.
Speed up app delivery with automated cancellation of plan-only Terraform runs
Automatic cancellation of plan-only runs allows customers to easily cancel any unfinished runs for outdated commits to speed up application delivery.