How to fix Error: Duplicate variable declaration in Terraform

TerraformBEGINNERMEDIUMUpdated Dec 30, 2025· 124 views

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"' .
  1. Search for the duplicate variable declaration
  2. Review each declaration location
  3. Remove the duplicate declaration
  4. Run terraform validate
terraform — error
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

  1. Same variable defined in multiple .tf files within the same module directory (Terraform loads all .tf files)
  2. Variable accidentally copied twice in the same file
  3. Variable defined in both a root module and included via a module call without proper namespacing
  4. Corrupted .terraform cache directory causing module files to load twice (rare)
  5. Using modules that contain conflicting variable names at the same scope level

How to fix "Error: Duplicate variable declaration"

1Search for the duplicate variable declaration

Use grep or your editor's find feature to locate all instances of the duplicate variable:

bash
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.

2Review each declaration location

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?

3Remove the duplicate declaration

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:

hcl
# KEEP THIS ONE
variable "region" {
  description = "AWS region"
  type        = string
  default     = "us-east-1"
}

# DELETE THIS ONE
# variable "region" {
#   default = "us-west-2"
# }
4Run terraform validate

After removing the duplicate, validate your configuration:

bash
terraform validate

If the output shows Success! The configuration is valid., the duplicate has been resolved.

5If the error persists, clear the .terraform cache

In rare cases where the error persists despite fixing the source files, the local .terraform cache may be corrupted:

bash
rm -rf .terraform
terraform init
terraform validate

This 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.

Official resources & further reading