# 错误：重复的资源 "aws_instance" 配置：地址为 "module.ec2.aws_instance.web" 的资源已存在

- **ID:** `terraform/duplicate-resource-name-in-module`
- **领域:** terraform
- **类别:** config_error
- **验证级别:** ai_generated
- **修复率:** 90%

## 根因

同一模块中存在两个或更多具有相同资源类型和名称的资源块，导致重复定义。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Terraform v1.5.0 | active | — | — |
| Terraform v1.6.0 | active | — | — |
| Terraform v1.7.0 | active | — | — |

## 解决方案

1. ```
   Remove or rename one of the duplicate resource blocks in the module configuration file. For example, change `resource "aws_instance" "web"` to `resource "aws_instance" "web2"` and update references.
   ```
2. ```
   Use a single resource block with count or for_each if multiple instances are needed: `resource "aws_instance" "web" { count = 2 ... }`
   ```

## 无效尝试

- **Add count or for_each to the duplicate resource block** — Count/for_each creates multiple instances but doesn't resolve duplicate block definitions. (85% 失败率)
- **Rename the module call in the root module** — The duplicate is within the same module, not at the call site. (75% 失败率)
- **Delete the .terraform directory and re-run init** — The config file itself has duplicates; init doesn't fix config errors. (90% 失败率)
