Sub topic 17
Diagnosing failed tasks and using ECS Exec
This chapter assumes you have a running Fargate service, know the task execution role from the task role, and can read CloudWatch Logs and use the AWS CLI. It shows how to find out why a task stopped, and how to open a shell in a running container.
Start with the stopped reason
ECS keeps stopped tasks visible for a short time. Each one records a stop code and a reason. Read these before you look anywhere else.
aws ecs list-tasks --cluster demo --service-name web --desired-status STOPPED
aws ecs describe-tasks \
--cluster demo \
--tasks <task-id> \
--query "tasks[0].{stop:stopCode,reason:stoppedReason,containers:containers[].{name:name,exit:exitCode,reason:reason}}"The service event log is also useful. aws ecs describe-services --cluster demo --services web --query "services[0].events[:5]" lists the latest scheduler messages.
Common failures
| Symptom in the stopped reason | Likely cause | Fix |
|---|---|---|
| `CannotPullContainerError` | No route to ECR, a wrong image tag, or missing pull permission | Give private subnets a NAT gateway or VPC endpoints, check the tag, and check the execution role |
| `ResourceInitializationError` for a secret | The execution role cannot read the secret, or Secrets Manager is unreachable | Grant `secretsmanager:GetSecretValue` to the execution role and fix the network path |
| `Essential container in task exited` with exit code 137 | The container ran out of memory or was killed | Raise task memory and check for leaks |
| Exit code 1 or 2 | The application failed at startup | Read the logs |
| `Task failed ELB health checks` | Wrong port, wrong path, or the app starts slowly | Check the target group and increase the grace period |
| `Task failed container health checks` | The health command fails inside the container | Test the command and extend `startPeriod` |
Pull and secret errors happen before your code starts. That is why no logs exist for them. They are almost always an execution role or networking problem.
Using ECS Exec
ECS Exec opens a shell in a running container through AWS Systems Manager. It needs no open port and no SSH key. Three things must be in place.
- The task role must allow the SSM messages actions.
- The service must be created or updated with exec enabled.
- The task must have a route to the SSM endpoints.
Add this statement to the task role. It is the task role, not the execution role, because the agent runs as part of your task.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ssmmessages:CreateControlChannel",
"ssmmessages:CreateDataChannel",
"ssmmessages:OpenControlChannel",
"ssmmessages:OpenDataChannel"
],
"Resource": "*"
}
]
}Enable exec on the service and replace the running tasks. Existing tasks do not pick up the setting.
aws ecs update-service \
--cluster demo \
--service web \
--enable-execute-command \
--force-new-deployment
aws ecs execute-command \
--cluster demo \
--task <task-id> \
--container web \
--interactive \
--command "/bin/sh"The container image must contain the shell you ask for. The local Session Manager plugin must also be installed.
If execute-command fails, run aws ecs describe-tasks and check that the managed agent ExecuteCommandAgent reports RUNNING. A task started before exec was enabled will not have it.
Exercise
A new task stops within seconds. The stopped reason is ResourceInitializationError while fetching a Secrets Manager value, and the log group is empty. Which role do you check first, and why is the log group empty?
Show answer
Check the task execution role, because ECS uses it to fetch secrets before the container starts. The log group is empty because the container never started, so the application wrote no logs.
Exercise
You enabled --enable-execute-command on the service, but execute-command still fails on an existing task. What do you do?
Show answer
Force a new deployment so replacement tasks start with the exec agent. Also confirm the task role has the ssmmessages permissions.