Terraform Error Guide: 'Duplicate variable declaration' — Remove the Redeclared Variable Block
Fix Terraform 'Duplicate variable declaration': the same variable name is declared twice across your .tf files. Find both blocks and delete the duplicate.
- #terraform
- #iac
- #troubleshooting
- #errors
Stuck on this Terraform error? Get the free incident triage checklist
A one-page PDF — the exact steps to isolate, fix, and verify a production error like this one. No spam, unsubscribe anytime.
Overview
Duplicate variable declaration is a configuration-loading error. Within a single Terraform module, every input variable name must be unique. Terraform reads all .tf files in the working directory and merges them into one namespace; if two variable "<name>" blocks share the same name — whether in the same file or, more often, split across variables.tf and another file — Terraform cannot decide which definition wins and aborts during init, validate, or plan.
The literal message points at the second declaration and back at the first:
Error: Duplicate variable declaration
on network.tf line 3:
3: variable "region" {
A variable named "region" was already declared at variables.tf:1,1-19.
Variable names must be unique within a module.
The message always names both locations: the file and line of the duplicate you are looking at, and the already declared at <file>:<line> for the original. That second path is the other half of the pair you need to reconcile.
This error is about declaring the same variable twice (two variable blocks), which is not allowed. It is different from assigning a value twice (in .tfvars, -var, or environment variables), which Terraform resolves by precedence rather than by erroring.
Symptoms
terraform init,terraform validate, orterraform planfails immediately withDuplicate variable declaration.- The message names two files/lines: the current duplicate and the
already declared at ...original. - It often appears after a merge, a refactor that split or moved variable blocks, or copy-pasting a
variableblock into the wrong file. - The same pattern can occur for other objects —
Duplicate resource,Duplicate output definition,Duplicate provider configuration— with the same underlying cause: a repeated named block within one module.
Common Root Causes
- Same variable in two files:
regiondeclared in bothvariables.tfandnetwork.tf. This is the classic case, common after modularising a largemain.tf. - Merge conflict artifact: a Git merge duplicated a
variableblock, or two branches each added the same variable. - Copy-paste a block into the wrong file without deleting the original.
- Confusing declaration with assignment: trying to “override” a variable by adding a second
variableblock instead of setting its value in a.tfvarsfile. - Generated + hand-written files: a codegen step and a human both emit the same variable into different
.tffiles in the same directory.
Diagnostic Workflow
Let terraform validate name both locations, then grep to confirm every declaration of that name:
terraform validate
# List every declaration of the duplicated variable across the module
grep -rn 'variable "region"' *.tf
# List all variable names to spot other duplicates at a glance
grep -rhn 'variable "' *.tf | sort | uniq -d
Here is a configuration that triggers the error — region is declared in two files that live in the same directory:
# variables.tf
variable "region" {
type = string
description = "AWS region for all resources"
default = "us-east-1"
}
# network.tf <-- duplicate declaration of the same name
variable "region" {
type = string
default = "us-west-2"
}
The fix keeps a single canonical declaration (usually in variables.tf) and deletes the other block:
# variables.tf — the ONLY declaration of "region"
variable "region" {
type = string
description = "AWS region for all resources"
default = "us-east-1"
}
# network.tf — variable block removed; just reference var.region here
resource "aws_subnet" "app" {
# ... uses var.region indirectly via the provider/region wiring
}
If you actually wanted a different value in one environment, do not add a second block — set it in a .tfvars file or with -var:
# Correct way to change a value (not a second declaration)
terraform plan -var 'region=us-west-2'
# or a file: terraform plan -var-file=west.tfvars
Example Root Cause Analysis
A team split a 900-line main.tf into variables.tf, network.tf, and compute.tf. The refactor moved most variable blocks into variables.tf but accidentally left a variable "region" block behind in network.tf while also adding one to variables.tf. The next terraform plan failed:
Error: Duplicate variable declaration
on network.tf line 3:
3: variable "region" {
A variable named "region" was already declared at variables.tf:1,1-19.
Variable names must be unique within a module.
Running grep -rn 'variable "region"' *.tf returned two hits — variables.tf:1 and network.tf:3 — confirming the pair the error named. Because both blocks lived in the same module directory, Terraform merged them and hit the duplicate. The team deleted the network.tf block (keeping the fully documented one in variables.tf) and re-ran terraform validate, which passed. Root cause: an incomplete file split during refactoring that left the variable declared in two places. Adding grep -rhn 'variable "' *.tf | sort | uniq -d to CI would have caught it before merge.
Prevention Best Practices
- Keep all input variable declarations in a single
variables.tfper module so duplicates are obvious. - After merges and refactors, run
grep -rhn 'variable "' *.tf | sort | uniq -dto detect any repeated variable names. - Remember the difference between declaring (
variableblock, must be unique) and assigning (.tfvars/-var, resolved by precedence); change values via tfvars, never a second block. - Add
terraform validateto pre-commit hooks and CI so duplicate declarations fail before merge. - Watch for merge-conflict duplication; review the whole hunk, not just the conflict markers.
- If a codegen step emits variables, ensure it does not write names that hand-authored files already declare in the same directory.
Quick Command Reference
# Pinpoint both declarations
terraform validate
# Find all declarations of a specific variable
grep -rn 'variable "region"' *.tf
# Detect ANY duplicated variable name in the module
grep -rhn 'variable "' *.tf | sort | uniq -d
# Set a value the correct way (not a second declaration)
terraform plan -var 'region=us-west-2'
terraform plan -var-file=west.tfvars
# Re-validate after removing the duplicate
terraform validate && terraform plan
Conclusion
Duplicate variable declaration means the same variable "<name>" block appears twice in one module — Terraform merges every .tf file in the directory and needs each variable name to be unique. The error conveniently names both locations, so the fix is to keep one canonical declaration (in variables.tf) and delete the other. If you were trying to change a value, use a .tfvars file or -var instead of a second block. A quick uniq -d grep and a terraform validate gate in CI make this a merge-time catch rather than a plan-time failure.
Fixed it? Get 500 Terraform & DevOps AI prompts — free
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.
Stuck on this? Start guided troubleshooting
Open an interactive diagnostic session with this error already loaded. Work a step-by-step plan, record what each check returns, land on a root cause, and export a clean incident summary — no account needed to start.
Did this fix your issue?
Solved it a different way?
Share the fix that worked for you — reviewed, then published to help the next engineer.
That looks like it may contain a secret (key, token, password, or connection string). Please remove it — a note with a detected secret can’t be published.
Thanks — that helps. Published notes appear after a quick review.
Trending errors this week
The error guides other engineers are actually reading right now.
- 1mount: wrong fs type, bad option, bad superblock
- 2Docker 'failed to set up container networking': Fix the Bridge and IP Pool
- 3Docker 'failed to create shim task': How to Fix the containerd Runtime Error
- 4Transport endpoint is not connected
- 5modprobe: FATAL: Module not found
- 6mount: wrong fs type, bad option, bad superblock
Get 500 Battle-Tested DevOps AI Prompts — Free
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.