# 服务无法放置任务：原因：CPU资源不足

- **ID:** `aws/ecs-task-stopped-resource-cpu`
- **领域:** aws
- **类别:** resource_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

ECS集群或容量提供者没有足够的可用CPU单元来运行任务定义中预留的CPU资源。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| ECS 1.62.0 | active | — | — |
| AWS CLI 2.15.0 | active | — | — |

## 解决方案

1. ```
   通过增加更多EC2实例或扩展Auto Scaling组来扩展ECS集群：
aws autoscaling update-auto-scaling-group --auto-scaling-group-name my-asg --desired-capacity 10 --min-size 5 --max-size 20
   ```
2. ```
   使用Fargate启动类型或Fargate容量提供者并启用自动扩展：
aws ecs create-service --cluster my-cluster --service-name my-service --task-definition my-task --launch-type FARGATE --network-configuration ...
   ```
3. ```
   在任务定义中减少CPU预留以适应集群可用资源：
{
  "family": "my-task",
  "containerDefinitions": [{
    "name": "my-container",
    "cpu": 256,
    "memory": 512
  }]
}
   ```

## 无效尝试

- **Increase task CPU reservation in task definition** — Increasing CPU reservation makes the problem worse, as it requires even more CPU from the cluster. (95% 失败率)
- **Set service desired count to 0 and back to original** — Recycling the service does not create new capacity; the underlying resource shortage remains. (90% 失败率)
- **Add more memory without adding CPU capacity** — Memory is separate from CPU; the error is explicitly about CPU, so memory adjustments don't help. (85% 失败率)
