How to fix Error: Unknown variable - There is no variable named var in Terraform
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.
Quick fix
Fixes Error: Unknown variable - There is no variable named var
variable "acr_ip_allowlist" {
type = list(string)
description = "List of IP addresses to allow access to ACR"
}- Identify the missing variable name
- Create a variable block in your configuration
- Ensure variable naming matches exactly
- Define the variable type correctly
variable "acr_ip_allowlist" {type = list(string)description = "List of IP addresses to allow access to ACR"}Error: Unknown variable - There is no variable named var
What this error means
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.
Typical symptoms
- Terraform plan or apply fails immediately with 'Unknown variable' error
- Error message includes the specific variable name that could not be found (e.g., 'There is no variable named acr_ip_allowlist')
- Error occurs in dynamic blocks using for_each = var.something
- Error appears when using var.variable_name without a corresponding variable declaration
- The error prevents validation and plan from completing successfully
Common causes
- Variable is referenced with var.name but never declared with a variable block
- Typo in variable name: declared as variable 'my_var' but referenced as var.my_variable
- Variable block is in a different module and not passed as an input variable
- Copy-pasted configuration includes variable references without the corresponding variable definitions
- Using a variable name that is not declared at the scope where it is referenced
How to fix "Error: Unknown variable - There is no variable named var"
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
Advanced notes
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.
Frequently asked questions
- What does "Error: Unknown variable - There is no variable named var" mean?
- The 'Unknown variable' error indicates that Terraform is trying to reference a variable that does not exist in your configuration. 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.
- How do I fix "Error: Unknown variable - There is no variable named var"?
- 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.
- What causes "Error: Unknown variable - There is no variable named var"?
- Variable is referenced with var.name but never declared with a variable block Typo in variable name: declared as variable 'my_var' but referenced as var.my_variable Variable block is in a different module and not passed as an input variable
- Is the "Error: Unknown variable - There is no variable named var" error serious?
- Yes. This is a high-severity error that usually blocks work until it is fixed, but the steps above resolve it.