OpenTofu Error: 'Duplicate resource configuration' — Cause, Fix, and Troubleshooting Guide
Fix OpenTofu 'Error: Duplicate resource configuration' from repeated resource addresses across files, bad copy-paste, or count/for_each conflicts.
- #opentofu
- #iac
- #troubleshooting
- #errors
Stuck on this OpenTofu 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.
What this error means
A resource address — the type plus the name, like aws_instance.web — must be unique within a module. If two blocks declare the same type and name, even in different .tf files, OpenTofu cannot tell them apart and refuses to load the configuration:
Error: Duplicate resource "aws_instance" "web" configuration
on instances.tf line 20, in resource "aws_instance" "web":
20: resource "aws_instance" "web" {
A aws_instance resource named "web" was already declared at main.tf:8,1-33.
Resource names must be unique per type in each module.
When it appears
tofu validate/planfails withDuplicate resource ... configuration.- The message names both locations: the new declaration and the earlier one.
- Common after copy-pasting a block, splitting one file into several, or merging branches.
- The same error shape appears for duplicate
data,variable,output,module, andproviderblocks.
Configuration causes
- Copy-paste — a block duplicated within or across files without renaming.
- File split — moving resources into new files left a copy behind in the original.
- Merge conflict resolved wrong — both sides of a merge kept the same block.
count/for_eachmisuse — trying to express multiples with two identical blocks instead of one meta-argument.- Re-declared provider or variable with the same name.
Validating the configuration
OpenTofu names both files and lines — go straight there:
tofu validate
Find every declaration of the address across the module:
grep -rn 'resource "aws_instance" "web"' *.tf
Check for duplicated variables/outputs too:
grep -rhn 'variable "' *.tf | sort | uniq -d
Resolution
Rename or delete one of the duplicates so each address is unique:
resource "aws_instance" "web" { ... }
resource "aws_instance" "web_api" { ... } # was a second "web"
Use count or for_each instead of two near-identical blocks:
resource "aws_instance" "web" {
for_each = toset(["a", "b"])
ami = var.ami
instance_type = "t3.micro"
tags = { Name = "web-${each.key}" }
}
Resolve merge duplicates by keeping exactly one copy of the block.
Re-validate after the edit:
tofu validate && tofu plan
Keeping definitions valid
- OpenTofu treats all
.tffiles in a directory as one module — splitting files does not create separate namespaces. - Multiplicity is expressed with
count/for_each, never by duplicating blocks. - The same uniqueness rule applies to
data,variable,output,module, andprovider(per alias) blocks. - Run
tofu validatein pre-commit so duplicates from merges are caught before review.
Related configuration errors
- OpenTofu Error: ‘Cycle’ — Cause, Fix, and Troubleshooting Guide
- OpenTofu Error: ‘Invalid count argument’ — Cause, Fix, and Troubleshooting Guide
- OpenTofu Error: ‘Reference to undeclared resource’ — Cause, Fix, and Troubleshooting Guide
Fixed it? Get 500 OpenTofu & 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.
Did this fix your issue?
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
- 4modprobe: FATAL: Module not found
- 5mount: wrong fs type, bad option, bad superblock
- 6Kernel panic - not syncing: VFS: Unable to mount root fs on unknown-block(0,0)
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.