Error: Duplicate resource "aws_vpc" configuration
Fixes terraform's 'Error: Duplicate resource configuration' failure. Use when validate or plan fails because two resource blocks share the same type and name in one module, often from a forgotten backup .tf file. Not for resources that exist in the cloud but are missing from state.
Fix terraform 'Error: Duplicate resource "aws_vpc" configuration'
TL;DR: Two resource blocks in the same module share the same type and name. Terraform loads every *.tf file in the directory, including forgotten backup files like main.bak4.tf, so grep the whole directory for the duplicated type and name, then rename one block or delete the stale copy.
The error
Error: Duplicate resource "aws_vpc" configuration
on main.tf line 6:
6: resource "aws_vpc" "main" {
A aws_vpc resource named "main" was already declared at vpc.tf:1,1-39. Resource names must be unique per type in each module.Steps
- Search the module directory:
grep -rn 'resource "aws_vpc" "main"' --include='*.tf' .. Expected: two (or more) hits across your.tffiles. - Decide which one is canonical. Rename the other block's name (for example
"main"to"secondary"), or delete the file if it is a stale copy or backup. Expected: exactly one declaration remains. - Check for backup files Terraform still loads:
ls *.tfand look for*.bak.tf,main.bak4.tf, or editor autosaves. Move them out of the directory. Expected: no stray.tffiles. - Run
terraform validate. Expected:Success! The configuration is valid.
When this applies
validateorplanfails naming the same resource type and name twice, with "already declared at" pointing at another file.- You copied a file as a backup inside the same directory.
When it does NOT apply
- The resource already exists in the cloud but is not in state. That is an import case (
terraform import), not a config duplication. - Intentional merging via
_override.tffiles. Those are a separate mechanism and do not trigger this error.
Tool and version compatibility
- All Terraform CLI versions. The uniqueness rule for type plus name per module has never changed.
Why it happens
A resource's address is its type plus its name. Two blocks with the same address in one module is ambiguous, so Terraform rejects the configuration statically. File names are cosmetic: every *.tf file in the directory is one configuration, backups included.
Edge cases and pitfalls
- Files ending in
_override.tfintentionally redeclare blocks and merge instead of erroring. Do not use them casually; they make configs hard to read. - The same rule applies to
datablocks,variableblocks, andoutputblocks: names must be unique per kind within the module.