This error occurs when Terraform encounters a reference to a variable (using var.something) that has not been declared with a variable block. This commonly happens in dynamic blocks or resource configurations when you forget to define the input variable first.
The 'Unknown variable' error indicates that Terraform is trying to reference a variable that does not exist in your configuration. When you use var.variable_name in your Terraform code, Terraform requires that variable to be explicitly declared using a variable block before it can be used. Without this declaration, Terraform cannot resolve the reference and stops execution with this error.
Look at the error message to find which variable is missing. The error will state 'There is no variable named X'. For example: 'There is no variable named acr_ip_allowlist'.
In a .tf file (typically variables.tf), add a variable declaration matching the name from the error. Use the appropriate type based on how you intend to use it:
variable "acr_ip_allowlist" {
type = list(string)
description = "List of IP addresses to allow access to ACR"
}Verify that the variable name in your variable block matches exactly how you reference it in var.name. Terraform is case-sensitive. If declared as variable "my_var", reference it as var.my_var (not var.my_variable or var.myvar).
Choose the correct type for your variable based on how it's used:
# Simple string
variable "region" {
type = string
}
# List of strings (common for for_each loops)
variable "ip_rules" {
type = list(string)
}
# Map/object (common for dynamic blocks)
variable "config" {
type = map(any)
}After adding the variable declaration, run terraform validate to confirm the syntax is correct and the variable is now recognized:
terraform validateIf validation passes, the variable reference error is resolved.
Terraform needs a value for each variable. You can provide values in multiple ways:
1. Command line: terraform apply -var='acr_ip_allowlist=["192.168.1.1"]'
2. Environment variable: export TF_VAR_acr_ip_allowlist='["192.168.1.1"]'
3. terraform.tfvars file:
acr_ip_allowlist = ["192.168.1.1", "10.0.0.0/8"]4. Interactive prompt: Terraform will ask for the value if not provided elsewhere
This error is especially common when using dynamic blocks in resource configurations (e.g., dynamic 'ip_rule' { for_each = var.rules }). The variable must be declared at the same scope level where it is referenced—if using modules, variables must be declared in the module's variables.tf and passed as module arguments. For Terragrunt users, a similar error occurs with dependency blocks; ensure dependency references are properly configured in terragrunt.hcl. Terraform validates the entire configuration before execution, so fixing all variable references before running terraform plan prevents partial failures during infrastructure provisioning.
Error: Error installing helm release: cannot re-use a name that is still in use
How to fix "release name in use" error in Terraform with Helm
Error: Error creating GKE Cluster: BadRequest
BadRequest error creating GKE cluster in Terraform
Error: External program failed to produce valid JSON
External program failed to produce valid JSON
Error: Unsupported argument in child module call
How to fix "Unsupported argument in child module call" in Terraform
Error: network is unreachable
How to fix "network is unreachable" in Terraform