Introduction
This article helps you understand and resolve the “Invalid reference in variable validation” error seen in Terraform versions prior to 1.9, especially when attempting to validate input variables against other variables or dynamic values. It includes a practical example and demonstrates a workaround using the precondition block.
Problem
Terraform throws an error when you try to reference another variable (or a data source, local value, or resource) within a variable’s validation block in versions before 1.9.
This article applies to:
Product: HashiCorp Terraform Version prior 1.9
Cause
This error is encountered when using cross-variable references inside a variable block’s validation logic in Terraform <1.9.
You may see the following error during terraform plan or terraform apply:
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." } }
Error Output
│ 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.
This error is caused because:
🚫 You are referencing another variable (var.create_file) inside a validation block for var.file_content. ✅ Prior to version 1.9, Terraform only allowed self-reference in variable validation blocks.
Overview of Possible Solutions
Depending on your Terraform version, you have two main options:
Use precondition in your resource block as a workaround (for Terraform <1.9) ✅ Upgrade to Terraform ≥1.9 to use cross-variable validations directly
Solutions
Solution 1: Use precondition Block (Terraform <1.9 Workaround)
Move the validation to a precondition block inside a dummy or dependent resource such as null_resource.
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
When create_file = false: Nothing is created; validation is skipped. ❌ When create_file = true and file_content = "": Fails with a descriptive error at the resource level.
Solution 2: Upgrade to Terraform 1.9+
Terraform 1.9 introduces cross-object input validations, allowing this cleaner pattern:
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 when create_file is true." } }
This validation will now work natively and block execution early with an appropriate error message.
Outcome
Terraform does not throw the “Invalid reference in variable validation” error during terraform plan. If using precondition, Terraform fails with the correct error message only when the input combination is invalid. If using Terraform 1.9+, the validation works within the variable block directly.
Additional Information
Terraform Input Variable Validation Docs
Terraform Resource precondition Documentation