Lesson 1 of 3 Custom Conditions and Checks

Input Variable Validation

Validation blocks reject invalid input before Terraform plans anything. This lesson covers their syntax, common patterns and behaviour.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe validation blocks and their conditions and error messages.
  • Explain when validation runs and how failures affect a run.

Validation blocks

A type constraint ensures that a value is, for example, a string, but not that it is a sensible string. A validation block inside a variable block adds a rule. It has a condition, an expression that must be true for a valid value, and an error_message that Terraform shows when it is false.

variable "environment" {
  type        = string
  description = "Deployment environment."

  validation {
    condition     = contains(["dev", "test", "prod"], var.environment)
    error_message = "The environment must be dev, test or prod."
  }
}

variable "ami_id" {
  type = string

  validation {
    condition     = can(regex("^ami-[0-9a-f]{8,17}$", var.ami_id))
    error_message = "The ami_id value must be a valid AMI ID starting with \"ami-\"."
  }
}

A variable can have several validation blocks, and Terraform reports every one that fails. The can() function is useful in conditions because it turns an error, such as a regular expression that does not match, into false. Since Terraform 1.9, conditions may also refer to other variables and objects in the same module, such as requiring a larger instance type when var.environment is prod.

Scenario questions

Writing a validation

Exam skills 4.7.1

Write validation conditions that accept good values and reject bad ones.

Log in to try this activity Interactive activities are free with an account.

When validation runs

Terraform evaluates validation conditions as soon as the variable's value is known, normally at the start of plan. A failed condition is an error: the run stops before any change is planned, and the message names the variable and shows the error_message. If the value depends on something unknown until apply, Terraform checks it during apply instead.

$ terraform plan -var environment=staging
Error: Invalid value for variable

  on variables.tf line 1:
   1: variable "environment" {

The environment must be dev, test or prod.

Good error messages are full sentences that explain what a valid value looks like, so that the caller can fix the input without reading the module's code.

The following diagram shows a validation block and when each failure appears.

Rejecting an invalid bucket name before anything is created. Without validation the provider's API rejects the uppercase name during apply, after other resources exist. A validation block with a regular expression rejects it at plan time with a clear message.
The error message wording is illustrative.
Worked example

Protecting a module's input

A shared module creates a storage bucket whose name becomes part of a public URL. Callers sometimes pass names with uppercase letters, and the apply fails after other resources have been created. How can the module fail earlier?

The decisive requirement is to reject bad input before any change.

A validation block such as condition = can(regex("^[a-z0-9-]{3,63}$", var.name)) with a clear error message rejects invalid names at plan time, before anything is created. The provider's API error during apply was later, less clear and left a partly applied change.

Check yourself

What two arguments does a validation block require?

Show answer

condition, which must be true for a valid value, and error_message.

Exam Tips

Remember for the exam

  • validation = condition + error_message, inside a variable block.
  • A failed validation is an error raised before planning.
  • can() turns errors into false, ideal for regex checks.
  • Since 1.9, conditions can refer to other variables.