Interview Prep/AWS/ECS, EKS, ECR and Lambda

`aws ecs execute-command` fails: `The execute command failed because execute command was not enabled when the task was run or the execute command agent isn't running.`

IntermediateTroubleshootingECS, EKS, ECR and Lambda
Answer

Likely causes → The task was started without enableExecuteCommand. Other causes: the task role has no ssmmessages:* permissions, the private subnet cannot reach ssmmessages, or the Session Manager plugin is not installed locally.

Diagnose →

aws ecs describe-tasks --cluster prod --tasks <task-arn> \
  --query 'tasks[].[enableExecuteCommand,containers[].managedAgents]'
# AWS's open-source checker:
bash <(curl -s https://raw.githubusercontent.com/aws-containers/amazon-ecs-exec-checker/main/check-ecs-exec.sh) prod <task-id>

Fix →

  1. aws ecs update-service --cluster prod --service api --enable-execute-command --force-new-deployment (existing tasks must be replaced)
  2. Give the task role ssmmessages:CreateControlChannel, CreateDataChannel, OpenControlChannel and OpenDataChannel
  3. Add the ssmmessages endpoint for private subnets

Prevent → Enable exec in non-production by default, log sessions to CloudWatch or S3, and restrict it with IAM conditions in production.

aws All AWS questions Roadmap: EKS, ECS & serverless on AWS

More on ECS, EKS, ECR and Lambda