# 服务无法放置任务：原因：任务卡在 PENDING 状态；无法拉取镜像或资源不可用

- **ID:** `aws/ecs-task-stuck-in-pending`
- **领域:** aws
- **类别:** resource_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

ECS 任务无法从 PENDING 转换到 RUNNING，因为集群资源（CPU/内存/端口）不足或镜像拉取失败。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| ECS 1.30.0 | active | — | — |
| Amazon Linux 2 | active | — | — |
| Docker 20.10.7 | active | — | — |

## 解决方案

1. ```
   检查 ECS 集群容量：运行 `aws ecs describe-clusters --cluster your-cluster --include ATTACHMENTS` 查看运行任务数和剩余资源。通过添加 EC2 实例或增加最大任务数来扩展。
   ```
2. ```
   验证镜像拉取：在实例上测试 `docker pull your-image:tag`。确保 ECR 仓库存在且任务执行角色具有 ecr:GetDownloadUrlForLayer 和 ecr:BatchGetImage 权限。
   ```
3. ```
   检查任务放置约束：如果约束（如 `memberOf`）过于严格，请移除或放宽。使用 `aws ecs describe-tasks --cluster your-cluster --tasks task-id` 获取详细状态。
   ```

## 无效尝试

- **Increase task definition CPU/memory arbitrarily** — Incorrect resource sizing wastes capacity; actual cause is cluster saturation or image registry issues. (65% 失败率)
- **Restart the ECS agent on all instances** — Agent restart doesn't free resources or fix image pull problems; only a temporary workaround. (40% 失败率)
- **Delete and recreate the service** — Service recreation doesn't change underlying resource constraints or image pull failures. (50% 失败率)
