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.
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.
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.
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.
Error: Error rendering template: template not found
How to fix "template not found" error in Terraform
Error: Error generating private key
How to fix 'Error generating private key' in Terraform
Error creating Kubernetes Service: field is immutable
How to fix "field is immutable" errors in Terraform
Error: Error creating local file: open: permission denied
How to fix "Error creating local file: permission denied" in Terraform
Error: line endings have changed from CRLF to LF
Line endings have changed from CRLF to LF in Terraform