Home / AWS

AWSRunbookIntermediate

AWS Batch jobs stuck in RUNNABLE: find the blocker and clear the queue

Work out why AWS Batch jobs never leave RUNNABLE, from invalid compute environments and vCPU limits to instances that never join ECS, then unblock the queue.

Deepak Bhardwaj6 min read

Applies toAWS Batch on EC2, Spot and Fargate (ECS orchestration), AWS CLI v2

Symptom

Jobs sit in RUNNABLE and nothing starts. RUNNABLE means dependencies are met and the job is waiting for capacity, so the application code has not run. Look at the queue, compute environment and instances, not the container logs.

Step 1: read the status reason

list-jobs returns only RUNNING jobs by default, so always pass the status:

QUEUE=prod-queue

aws batch list-jobs --job-queue $QUEUE --job-status RUNNABLE \
  --query "jobSummaryList[].{Id:jobId,Name:jobName,Created:createdAt,Reason:statusReason}" \
  --output table

See which job is at the front. On a first-in, first-out queue, one impossible job at the head can hold up everything behind it:

aws batch get-job-queue-snapshot --job-queue $QUEUE \
  --query "frontOfQueue.jobs[0:5].[jobArn,earliestTimeAtPosition]" --output table

Match the reason:

statusReason starts with Cause Fix
CAPACITY:INSUFFICIENT_INSTANCE_CAPACITY Every connected compute environment is getting capacity errors Add instance types, Availability Zones or an On-Demand environment
MISCONFIGURATION:COMPUTE_ENVIRONMENT_MAX_RESOURCE maxvCpus is smaller than the job needs Raise maxvCpus or reduce the job
MISCONFIGURATION:JOB_RESOURCE_REQUIREMENT No allowed instance type is big enough for the vCPU, memory or GPU request Add larger types or reduce the job
MISCONFIGURATION:EC2_INSTANCE_CONFIGURATION_UNSUPPORTED Type not offered in the chosen zones, or launch template incompatible with it Fix the types, subnets or template
MISCONFIGURATION:SERVICE_ROLE_PERMISSIONS Batch service role lacks a permission Step 3
ACTION_REQUIRED All compute environments are INVALID Step 3
UNDETERMINED or empty Batch could not infer the cause Steps 2 to 5

Step 2: check the queue and its compute environments

aws batch describe-job-queues --job-queues $QUEUE \
  --query "jobQueues[].{State:state,Status:status,Reason:statusReason,Order:computeEnvironmentOrder}"

aws batch describe-compute-environments --compute-environments prod-ce \
  --query "computeEnvironments[].{State:state,Status:status,Reason:statusReason,Role:serviceRole,Cluster:ecsClusterArn,Res:computeResources}"

You want state ENABLED and status VALID on both. Then look at computeResources:

  • maxvCpus: compare it with the total vCPUs already running. A full environment is normal queueing, not a fault.
  • instanceTypes: can one instance hold the whole job?
  • subnets and securityGroupIds: do they still exist and have a route out?

Tip

A job asking for all the memory of an instance type will never be placed. The ECS agent and operating system reserve some of it. Ask for less than the advertised memory.

Check what the job asks for:

aws batch describe-jobs --jobs <job-id> \
  --query "jobs[0].container.{Req:resourceRequirements,Image:image,Log:logConfiguration}"

If maxvCpus is the limit, raise it after checking your EC2 vCPU quota:

aws batch update-compute-environment --compute-environment prod-ce \
  --compute-resources maxvCpus=512

Step 3: repair an INVALID environment

The usual cause is a service role or Spot Fleet role that was deleted, misspelt, or has the wrong trust relationship. Read statusReason, which names the failed action (often sts:AssumeRole).

Check that the role exists and who it trusts. The service role must trust batch.amazonaws.com:

aws iam get-role --role-name AWSServiceRoleForBatch \
  --query "Role.{Arn:Arn,Trust:AssumeRolePolicyDocument}"

AWS recommends the service-linked role. If the environment uses a custom service role, repair its trust and compare its permissions with the AWS managed AWSBatchServiceRole policy. To point at a different role, use its full ARN:

aws batch update-compute-environment --compute-environment prod-ce \
  --service-role arn:aws:iam::111122223333:role/service-role/AWSBatchServiceRole

Warning

Don't edit the Auto Scaling groups, launch templates or ECS clusters that Batch manages. Make changes through the Batch API, or the environment can drift or become invalid.

Step 4: EC2 environments — are instances launching and joining?

Find the cluster from step 2 and count its instances:

CLUSTER=<ecsClusterArn>
aws ecs list-container-instances --cluster $CLUSTER

aws ec2 describe-instances \
  --filters "Name=instance-state-name,Values=pending,running" \
            "Name=subnet-id,Values=subnet-0aaa,subnet-0bbb" \
  --query "Reservations[].Instances[].[InstanceId,InstanceType,LaunchTime,IamInstanceProfile.Arn]" \
  --output table

No EC2 instances. For an On-Demand or Spot environment backed by Auto Scaling, read the launch error:

aws autoscaling describe-auto-scaling-groups \
  --query "AutoScalingGroups[?contains(AutoScalingGroupName,'prod-ce')].AutoScalingGroupName"

aws autoscaling describe-scaling-activities \
  --auto-scaling-group-name <asg-name> --max-items 5 \
  --query "Activities[].[StartTime,StatusCode,StatusMessage]" --output table

VcpuLimitExceeded is a quota problem. InsufficientInstanceCapacity is an EC2 supply problem, so widen the instance types. Group names vary; if the filter returns nothing, list them all.

Instances run but no container instances appear. They launched but never registered with ECS. Check:

Check What to look for
Network NAT gateway, public IP, or interface endpoints for ecs, ecs-agent and ecs-telemetry
Image pulls on private subnets Endpoints for ecr.api and ecr.dkr, plus S3 access
Instance role AmazonEC2ContainerServiceforEC2Role attached and an instance profile set
AMI ECS agent installed and running
User data A long script delays registration; the job stays RUNNABLE until it ends

With Session Manager access to an instance:

sudo systemctl status ecs --no-pager
sudo tail -n 50 /var/log/ecs/ecs-agent.log
sudo tail -n 50 /var/log/cloud-init-output.log
curl -s http://localhost:51678/v1/metadata

Instances registered, jobs still waiting. On a custom AMI, confirm the job's log driver is allowed. awslogs must be in ECS_AVAILABLE_LOGGING_DRIVERS:

grep ECS_ /etc/ecs/ecs.config

Also check for a job that needs a GPU on a cluster with no GPU instances.

Step 5: clear the blocking job

Once you know the head job can't run, cancel it so the rest can move:

aws batch cancel-job --job-id <job-id> \
  --reason "Requests more memory than any instance in prod-ce"

Caution

Cancelling discards the queued run, and dependent jobs won't run. Save the job's parameters and tell the owner first. Don't bulk-cancel a queue to clear one bad job.

Correct the job definition and resubmit. Then confirm jobs move through STARTING to RUNNING:

for s in RUNNABLE STARTING RUNNING; do
  echo "$s $(aws batch list-jobs --job-queue $QUEUE --job-status $s --query 'length(jobSummaryList)' --output text)"
done

A job that fails in STARTING is a different problem: an image pull error, or a missing execution role or secret. Read statusReason and the attempts in describe-jobs.

Prevent it next time

Cancel impossible jobs automatically. Batch can cancel a job that stays RUNNABLE for a known reason. maxTimeSeconds must be between 600 and 86,400:

cat > limits.json <<'EOF'
[
  {"reason": "MISCONFIGURATION:JOB_RESOURCE_REQUIREMENT", "state": "RUNNABLE", "maxTimeSeconds": 600, "action": "CANCEL"},
  {"reason": "MISCONFIGURATION:COMPUTE_ENVIRONMENT_MAX_RESOURCE", "state": "RUNNABLE", "maxTimeSeconds": 600, "action": "CANCEL"},
  {"reason": "CAPACITY:INSUFFICIENT_INSTANCE_CAPACITY", "state": "RUNNABLE", "maxTimeSeconds": 14400, "action": "CANCEL"}
]
EOF

aws batch update-job-queue --job-queue $QUEUE \
  --job-state-time-limit-actions file://limits.json

These actions don't cover role problems, invalid environments or UNDETERMINED, so alert on those.

Alert on blocked queues. Send the event to an SNS topic whose policy lets events.amazonaws.com publish:

aws events put-rule --name batch-queue-blocked \
  --event-pattern '{"source":["aws.batch"],"detail-type":["Batch Job Queue Blocked"]}'

aws events put-targets --rule batch-queue-blocked \
  --targets "Id=ops,Arn=arn:aws:sns:eu-west-1:111122223333:ops-alerts"
  • Attach a second, On-Demand environment behind a Spot one.
  • Allow several instance families and at least two zones.
  • Keep experimental jobs on their own queue.
  • Track vCPU quota against the sum of maxvCpus across environments.