# 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

```text
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

1. Search the module directory: `grep -rn 'resource "aws_vpc" "main"' --include='*.tf' .`. Expected: two (or more) hits across your `.tf` files.
2. 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.
3. Check for backup files Terraform still loads: `ls *.tf` and look for `*.bak.tf`, `main.bak4.tf`, or editor autosaves. Move them out of the directory. Expected: no stray `.tf` files.
4. Run `terraform validate`. Expected: `Success! The configuration is valid.`

## When this applies

- `validate` or `plan` fails 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.tf` files. 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.tf` intentionally redeclare blocks and merge instead of erroring. Do not use them casually; they make configs hard to read.
- The same rule applies to `data` blocks, `variable` blocks, and `output` blocks: names must be unique per kind within the module.