How to fix Error: Duplicate variable declaration in Terraform
Terraform raises a duplicate variable declaration error when the same variable is defined multiple times within a module. This prevents Terraform from validating and executing your configuration because variable names must be unique.
Quick fix
Fixes Error: Duplicate variable declaration
grep -rn 'variable "variable_name"' .- Search for the duplicate variable declaration
- Review each declaration location
- Remove the duplicate declaration
- Run terraform validate
grep -rn 'variable "variable_name"' .Error: Duplicate variable declaration
What this error means
When Terraform parses your configuration files, it scans all .tf files in a directory and loads them into a single module namespace. If it encounters the same variable name defined more than once, it halts with this error. This is a validation error that occurs during terraform validate, terraform plan, or terraform apply. The error indicates a configuration problem that must be resolved before Terraform can proceed with any operations.
Typical symptoms
- terraform validate fails with 'Error: Duplicate variable declaration'
- terraform plan or apply immediately halts with the duplicate error
- No terraform operations can run until the duplicate is fixed
- The error message includes the variable name and line numbers
Common causes
- Same variable defined in multiple .tf files within the same module directory (Terraform loads all .tf files)
- Variable accidentally copied twice in the same file
- Variable defined in both a root module and included via a module call without proper namespacing
- Corrupted .terraform cache directory causing module files to load twice (rare)
- Using modules that contain conflicting variable names at the same scope level
How to fix "Error: Duplicate variable declaration"
Use grep or your editor's find feature to locate all instances of the duplicate variable:
grep -rn 'variable "variable_name"' .Replace variable_name with the actual variable name from the error message. This will show you all files where this variable is declared.
Open each file returned by the grep search and examine the variable blocks. Determine which declaration is the correct one you want to keep. Consider:
- Is one definition incomplete or outdated?
- Are the default values or descriptions different?
- Which location is more logical for this variable?
Delete all but one of the duplicate variable blocks. If the declarations have different defaults or descriptions, merge the most accurate version into the single remaining declaration:
# KEEP THIS ONE
variable "region" {
description = "AWS region"
type = string
default = "us-east-1"
}
# DELETE THIS ONE
# variable "region" {
# default = "us-west-2"
# }After removing the duplicate, validate your configuration:
terraform validateIf the output shows Success! The configuration is valid., the duplicate has been resolved.
In rare cases where the error persists despite fixing the source files, the local .terraform cache may be corrupted:
rm -rf .terraform
terraform init
terraform validateThis reinitializes all module dependencies and clears any stale cache.
Advanced notes
Module-level duplicates: If you're using modules and see this error, check if you're declaring a variable in both the root module and a child module with the same name. Each module has its own variable namespace, so this shouldn't cause an error—but check for typos or unintended shadowing. tfvars considerations: While duplicate variable definitions in .tfvars files won't error (the last definition wins), it's still poor practice and can lead to confusion. Use unique variable names and rely on .tfvars files only to set values, not declare variables. Linting: Tools like tflint may not catch duplicate variables, so terraform validate is your primary safety net.
Frequently asked questions
- What does "Error: Duplicate variable declaration" mean?
- tf files in a directory and loads them into a single module namespace. If it encounters the same variable name defined more than once, it halts with this error. This is a validation error that occurs during terraform validate, terraform plan, or terraform apply.
- How do I fix "Error: Duplicate variable declaration"?
- Terraform raises a duplicate variable declaration error when the same variable is defined multiple times within a module. This prevents Terraform from validating and executing your configuration because variable names must be unique.
- What causes "Error: Duplicate variable declaration"?
- Same variable defined in multiple .tf files within the same module directory (Terraform loads all .tf files) Variable accidentally copied twice in the same file Variable defined in both a root module and included via a module call without proper namespacing
- Is the "Error: Duplicate variable declaration" error serious?
- It is a medium-severity error — more of a blocker than a risk. The steps above resolve it.