Amazon ECS task placement strategies - Amazon Elastic Container Service

Amazon ECS task placement strategies

A task placement strategy is an algorithm for selecting instances for task placement or tasks for termination. Task placement strategies can be specified when either running a task or creating a new service. The task placement strategies can be updated for existing services as well. For more information, see Amazon ECS task placement.

For tasks running as part of an Amazon ECS service, the task placement strategy is spread using the attribute:ecs.availability-zone. There isn't a default task placement constraint for tasks in services. For more information, see Scheduling Amazon ECS tasks.

You can create a task placement strategy that uses multiple strategies by creating arrays of strategies in the order that you want them performed. For example, if you want to spread tasks across Availability Zones and then bin pack tasks based on memory within each Availability Zone, specify the Availability Zone strategy followed by the memory strategy. For example strategies, see Example strategies.

Strategy types

Amazon ECS supports the following task placement strategies:

binpack

Tasks are placed on container instances so as to leave the least amount of unused CPU or memory. This strategy minimizes the number of container instances in use.

When this strategy is used and a scale-in action is taken, Amazon ECS terminates tasks. It does this based on the amount of resources that are left on the container instance after the task is terminated. The container instance that has the most available resources left after task termination has that task terminated.

random

Tasks are placed randomly.

spread

Tasks are placed evenly based on the specified value. Accepted values are instanceId (or host, which has the same effect), or any platform or custom attribute that's applied to a container instance, such as attribute:ecs.availability-zone.

Service tasks are spread based on the tasks from that service. Standalone tasks are spread based on the tasks from the same task group. For more information about task groups, see Task groups.

When the spread strategy is used and a scale-in action is taken, Amazon ECS selects tasks to terminate that maintain a balance across Availability Zones. Within an Availability Zone, tasks are selected at random.

Example strategies

You can specify task placement strategies with the following actions: CreateService, UpdateService, and RunTask.

Distribute tasks evenly across Availability Zones

The following strategy distributes tasks evenly across Availability Zones.

"placementStrategy": [ { "field": "attribute:ecs.availability-zone", "type": "spread" } ]

Distribute tasks evenly across all instances

The following strategy distributes tasks evenly across all instances.

"placementStrategy": [ { "field": "instanceId", "type": "spread" } ]

Bin pack tasks based on memory

The following strategy bin packs tasks based on memory.

"placementStrategy": [ { "field": "memory", "type": "binpack" } ]

Place tasks randomly

The following strategy places tasks randomly.

"placementStrategy": [ { "type": "random" } ]

Distribute tasks evenly across Availability Zones and then distributes tasks evenly across the instances within each Availability Zone

The following strategy distributes tasks evenly across Availability Zones and then distributes tasks evenly across the instances within each Availability Zone.

"placementStrategy": [ { "field": "attribute:ecs.availability-zone", "type": "spread" }, { "field": "instanceId", "type": "spread" } ]

Distribute tasks evenly across Availability Zones and then bin pack tasks based on memory within each Availability Zone

The following strategy distributes tasks evenly across Availability Zones and then bin packs tasks based on memory within each Availability Zone.

"placementStrategy": [ { "field": "attribute:ecs.availability-zone", "type": "spread" }, { "field": "memory", "type": "binpack" } ]

Distribute tasks evenly across instances and then bin pack tasks based on memory

The following strategy distributes tasks evenly across evenly across all instances and then bin packs tasks based on memory within each instance.

"placementStrategy": [ { "field": "instanceId", "type": "spread" }, { "field": "memory", "type": "binpack" } ]