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
Fixing errors like this? Get 500 free DevOps AI prompts
500 copy-paste AI prompts for the stack you actually run — one PDF, free.
Overview
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.
Symptoms
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.
Common Root 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.
How to diagnose
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
Fixes
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
What to watch out for
- 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
- 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
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.