Learning center

ECS unable to place a task: insufficient CPU or memory on EC2

Compare the new revision's CPU and memory reservation with each container instance's registeredResources and remainingResources. If the reservation is larger than every instance's registered total, the revision can never fit on this instance type. If it fits the registered total but not what remains, the cluster is full while old and new tasks run side by side.

The event and the two causes behind it#

On the EC2 launch type, a stalled deployment often shows this service event and no stopped task for the new revision, because ECS never placed one:

service (service-name) was unable to place a task because no container instance met all of its requirements.

When the closest candidate failed on CPU, the event adds The closest matching container-instance container-instance-id has insufficient CPU units available. A memory variant names memory in the same position. Both mean the same thing: the best instance ECS found did not have enough of that resource unreserved for this task. Service event messages

Two causes produce that text during a deployment, and they need opposite fixes:

  • The revision outgrew the instance type. The new task definition reserves more CPU or memory than any container instance registered. Adding instances of the same type changes nothing.
  • The cluster is full during the rollout. The new task would fit on an empty instance, but every instance is occupied by old tasks. With the default maximumPercent of 200, ECS starts new tasks before it stops old ones, provided the cluster resources are available, so a cluster sized for one copy of the service has no room for the overlap. Deployment configuration

If you have not yet sorted the failure by layer, start from the signature table.

Read what the revision reserves#

Take the new deployment's exact task definition ARN from the service, then read its reservation. Use a read-only profile and replace the uppercase placeholders.

aws ecs describe-services \
  --profile PROFILE --region REGION --cluster CLUSTER --services SERVICE \
  --query 'services[0].{desired:desiredCount,config:deploymentConfiguration.{max:maximumPercent,min:minimumHealthyPercent},deployments:deployments[].{id:id,status:status,definition:taskDefinition,running:runningCount,pending:pendingCount},events:events[:10].[createdAt,message]}' \
  --output json --no-cli-pager

aws ecs describe-task-definition \
  --profile PROFILE --region REGION --task-definition TASK_DEFINITION_ARN \
  --query 'taskDefinition.{cpu:cpu,memory:memory,network:networkMode,containers:containerDefinitions[].{name:name,cpu:cpu,memory:memory,reservation:memoryReservation}}' \
  --output json --no-cli-pager

Run the second command for the old revision too. Work out each revision's reservation this way:

  • A task-level cpu or memory is what the task needs from an instance.
  • Without a task-level value, add the containers. For memory, a container's memoryReservation is subtracted from the instance's available memory when it is set; otherwise its memory is used.
  • CPU is in units, and an instance has 1,024 units for each vCPU.

TaskDefinition fields · ContainerDefinition memory and memoryReservation

Read what each instance registered and has left#

registeredResources is the CPU and memory the agent offered to tasks when the instance registered, which is the most one instance can ever hold. remainingResources is what is not yet reserved by tasks on it now. ContainerInstance fields

aws ecs list-container-instances \
  --profile PROFILE --region REGION --cluster CLUSTER \
  --query 'containerInstanceArns' --output json --no-cli-pager

aws ecs describe-container-instances \
  --profile PROFILE --region REGION --cluster CLUSTER \
  --container-instances CONTAINER_INSTANCE_ARN_1 CONTAINER_INSTANCE_ARN_2 \
  --query 'containerInstances[].{instance:ec2InstanceId,status:status,agent:agentConnected,running:runningTasksCount,cpuRegistered:registeredResources[?name==`CPU`].integerValue|[0],cpuLeft:remainingResources[?name==`CPU`].integerValue|[0],memRegistered:registeredResources[?name==`MEMORY`].integerValue|[0],memLeft:remainingResources[?name==`MEMORY`].integerValue|[0]}' \
  --output table --no-cli-pager

Registered memory is lower than the memory advertised for the instance type, because of platform overhead, kernel memory and any ECS_RESERVED_MEMORY set in the agent configuration. A task that asks for exactly the advertised amount cannot be placed. Compare against memRegistered, never against the instance type's specification. Container instance memory

What the comparison tells you#

New revision's reservationInstancesCauseWhat to change
Larger than cpuRegistered or memRegistered on every instanceAnyThe revision does not fit this instance typeReduce the reservation, or run the service on a larger instance type
Fits the registered total; larger than every cpuLeft or memLeftEach runs old tasksThe cluster is full during the overlapAdd instances for the deployment, or let ECS stop old tasks first
Fits the remaining resources on some instanceEvent still repeatsSomething other than CPU or memory, or a stale eventRead the full event text for ports, attributes or the agent

For the full-cluster case, the deployment configuration decides whether ECS can make room itself. With minimumHealthyPercent at its default of 100, the scheduler must keep the full desired count running, so it waits for capacity. A lower value lets it stop some old tasks before starting new ones, at the cost of running fewer tasks during the rollout. The minimum is rounded up and the maximum rounded down. Rolling update deployments · Deployment configuration bounds

An illustrative fixture: same event, two causes#

This fixture is fictional. The service orders runs six tasks of orders:57 on three container instances. Each instance registered 2,048 CPU units and 3,828 MiB of memory. orders:57 reserves 512 CPU units and 1,536 MiB, two tasks per instance. The deployment runs with maximumPercent 200 and minimumHealthyPercent 100.

In the first case orders:58 raises task memory to 4,096 MiB:

InstancememRegisteredmemLeftFits 4,096 MiB?
i-0a13,828756No, even when empty
i-0b23,828756No, even when empty
i-0c33,828756No, even when empty

No instance of this type can ever hold the task. Scaling the cluster out adds more instances that also cannot hold it. Roll back to orders:57, then either bring the memory back under 3,828 MiB or move the service to capacity with larger instances.

In the second case orders:58 reserves 2,048 MiB. Each instance still has only 756 MiB left, because two old tasks hold 3,072 MiB. The task fits an empty instance, so the cluster is short of room only while both revisions run. One added instance lets ECS place a new task, after which it can stop an old one and continue. Setting minimumHealthyPercent to 50 would instead let ECS stop up to three old tasks first, freeing room on existing instances while the service runs at half its desired count.

Confirm on the replacement#

After the change, the event should stop repeating and the service should report service (service-name) has reached a steady state. Check that each new task has a containerInstanceArn and that its taskDefinitionArn is the revision you meant. Then read remainingResources again: an instance left with only a few MiB means the next deployment will hit the same wall. Service event messages

aws ecs describe-tasks \
  --profile PROFILE --region REGION --cluster CLUSTER --tasks TASK_ARN_1 TASK_ARN_2 \
  --query 'tasks[].{task:taskArn,definition:taskDefinitionArn,status:lastStatus,host:containerInstanceArn,deployment:startedBy}' \
  --output table --no-cli-pager

Where the reading misleads#

The event names one closest matching instance, not every instance ECS rejected. Task placement constraints are binding and can prevent placement, so an instance with free memory can still be excluded by a constraint. Read the service's placementConstraints if the numbers say a task should fit. Task placement

remainingResources is a snapshot. By the time you read it, ECS may have stopped tasks or the deployment may have rolled back, so a free instance now does not show that one was free at the failure. The service keeps its 100 most recent events, and the archived developer guide says duplicate messages are omitted until the cause is resolved or six hours pass, so the event may appear once for a long stall. describe-services

The same opening sentence covers other shortages with their own fixes: a host port already in use, too few ENI attachment points for awsvpc tasks, a missing required attribute, and an instance whose agent is disconnected. For ports, see the host port guide; for the agent, see the agent guide. With an Auto Scaling group capacity provider, a task larger than the smallest instance type in the group does not trigger scale-out at all; see tasks stuck in PROVISIONING. Managed scaling behavior

Questions#

Why does ECS say insufficient memory when the instance has free memory?

ECS compares the task's reservation with the memory not yet reserved by other tasks, not with memory actually in use. Containers that reserve more than they use still hold that reservation, and registered memory is already lower than the instance type's advertised size.

Will adding container instances fix an insufficient CPU or memory event?

Only when the cluster is full. If the new revision reserves more than any instance registered, more instances of the same type cannot hold it; reduce the reservation or use a larger instance type.

Why does the placement error appear only during deployments?

A rolling deployment with maximumPercent above 100 starts new tasks while old ones still hold their reservations. A cluster sized for one copy of the service has no room for that overlap, even though the same tasks fit once the old ones stop.

One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. Eligibility and fair-use terms.

Start free with GitHub

Related reading