Introduction
This article explains how to resolve the “Invalid reference in variable validation” error in Terraform versions prior to 1.9. This error occurs when attempting to validate an input variable by referencing other variables or dynamic values.
This guidance applies to Terraform CLI versions before 1.9.
Problem
In Terraform versions prior to 1.9, referencing another variable, data source, local value, or resource within a variable’s validation block is not permitted and causes an error during terraform plan or terraform apply.
Consider the following variable configuration.
variable "create_file" {
type = bool
default = false
}
variable "file_content" {
type = string
description = "Content for the local file."
validation {
condition = var.create_file == false || length(var.file_content) > 0
error_message = "file_content must be provided if create_file is true."
}
}Terraform returns the following error.
│ Error: Invalid reference in variable validation │ │ on variables.tf line 10: │ 10: │ condition = var.create_file == false || length(var.file_content) > 0 │ │ Variables in variable validation may not refer to other variables.
Cause
This error occurs for the following reasons:
- You are referencing another variable (
var.create_file) inside the validation block forvar.file_content. - Prior to version 1.9, Terraform only allowed self-reference within a variable's
validationblock.
Solutions
Depending on your Terraform version, you can either use a precondition block as a workaround or upgrade Terraform to use cross-variable validation natively.
Solution 1: Use a precondition Block (Terraform <1.9)
For Terraform versions prior to 1.9, move the validation logic to a precondition block inside a resource. You can use a null_resource for this purpose.
variable "create_file" {
type = bool
default = false
}
variable "file_content" {
type = string
description = "Content for the local file."
default = ""
}
resource "null_resource" "validate_file_content" {
count = var.create_file ? 1 : 0
lifecycle {
precondition {
condition = length(var.file_content) > 0
error_message = "file_content must be provided when create_file is true."
}
}
}
resource "local_file" "example" {
count = var.create_file ? 1 : 0
content = var.file_content
filename = "${path.module}/output.txt"
}Behavior:
- If
create_fileisfalse, thenull_resourceis not created and the validation is skipped. - If
create_fileistrueandfile_contentis an empty string, the plan fails with the descriptive error message from thepreconditionblock.
Solution 2: Upgrade to Terraform 1.9 or Newer
Terraform 1.9 and newer versions support cross-object references in input variable validation. Upgrading allows you to define the validation directly within the variable block.
variable "create_file" {
type = bool
default = false
}
variable "file_content" {
type = string
description = "Content for the local file."
validation {
condition = var.create_file == false || length(var.file_content) > 0
error_message = "file_content must be provided if create_file is true."
}
}This validation works natively in Terraform 1.9+ and stops execution with the appropriate error message if the condition is not met.
Outcome
After applying one of the solutions, Terraform no longer produces the “Invalid reference in variable validation” error.
- With the
preconditionworkaround, Terraform fails with your custom error message only when the input combination is invalid. - With Terraform 1.9 or newer, the validation works directly within the variable block.