DevOpsil
AWSHigh

Fix: AWS ECS Task Stopped Unexpectedly

ECS tasks in STOPPED state with 'Essential container exited'

!Symptoms

  • ECS tasks in STOPPED state with 'Essential container exited'
  • Service desired count not matching running count
  • Tasks cycling: starting, running briefly, then stopping
  • Application intermittently unavailable behind ALB

?Root Causes

  • Container exiting due to application error (exit code 1)
  • Container OOM killed — task memory limit exceeded (exit code 137)
  • Health check failing — ALB or ECS health check deregistering the task
  • Insufficient resources (CPU/memory) on the underlying EC2 or Fargate
  • IAM role missing permissions for the task (ECR pull, Secrets Manager, S3)

#Diagnosis Steps

  1. 1Check stopped task reason: AWS Console → ECS → Cluster → Tasks → Stopped tab → click task
  2. 2Check container exit code: 0=normal, 1=app error, 137=OOM, 139=segfault
  3. 3Check CloudWatch Logs for the container's log group
  4. 4Check ALB health check configuration and target group health
  5. 5Check task IAM role permissions if the app accesses AWS services

>Fix

  1. 1Fix application error shown in CloudWatch Logs
  2. 2Increase task memory if OOM killed: update task definition memory limits
  3. 3Increase health check grace period for slow-starting containers
  4. 4Fix health check endpoint to return 200 reliably
  5. 5Attach required IAM policies to the task execution role and task role

*Prevention

  • Set circuit breaker on ECS service to stop infinite restart loops
  • Configure proper health check grace period (startPeriod)
  • Monitor ECS service events and alert on repeated task failures
  • Load test containers to determine correct memory/CPU limits
  • Use ECS Exec to debug into running containers: `aws ecs execute-command`

Related Error Messages

Essential container in task exitedOutOfMemoryError: Container killed due to memory usageCannotPullContainerErrorResourceNotFoundException