Draining Amazon ECS container instances
There might be times when you need to remove a container instance from your cluster, for
example, to perform system updates or to scale down the cluster
capacity. Amazon ECS provides the ability to transition a container instance to a
DRAINING status. This is referred to as container instance
draining. When a container instance is set to DRAINING, Amazon ECS
prevents new tasks from being scheduled for placement on the container instance.
Besides draining a container instance yourself, two features can set it to
DRAINING. Managed instance draining, which is on by default for new Auto
Scaling group capacity providers, drains an instance when the capacity provider scales in or
replaces it. Spot Instance draining, if you turn it on, drains an instance when it receives a Spot Instance
interruption notice. Service tasks on Amazon EC2 container instances
that are stopped this way have the stopped reason Service . For more information, see Safely stop Amazon ECS workloads running on EC2 instances and
Configuring Amazon ECS Linux container instances to receive Spot Instance notices.service-name: container instance
is in DRAINING state
Draining behavior for services
Any tasks that are part of a service that are in a PENDING state are
stopped immediately. If there is available container instance capacity in the cluster,
the service scheduler will start replacement tasks. If there isn't enough container
instance capacity, a service event message will be sent indicating the issue.
Tasks that are part of a service on the container instance that are in a
RUNNING state are transitioned to a STOPPED state. The
service scheduler attempts to replace the tasks according to the service's deployment
type and deployment configuration parameters, minimumHealthyPercent and
maximumPercent. For more information, see Amazon ECS services and Amazon ECS service definition parameters.
-
If
minimumHealthyPercentis below 100%, the scheduler can ignoredesiredCounttemporarily during task replacement. For example,desiredCountis four tasks, a minimum of 50% allows the scheduler to stop two existing tasks before starting two new tasks. If the minimum is 100%, the service scheduler can't remove existing tasks until the replacement tasks are considered healthy. If tasks for services that do not use a load balancer are in theRUNNINGstate, they are considered healthy. Tasks for services that use a load balancer are considered healthy if they are in theRUNNINGstate and the container instance they are hosted on is reported as healthy by the load balancer.ImportantIf you use Spot Instances and
minimumHealthyPercentis greater than or equal to 100%, then the service will not have enough time to replace the task before the Spot Instance terminates. -
The
maximumPercentparameter represents an upper limit on the number of running tasks during task replacement, which allows you to define the replacement batch size. For example, ifdesiredCountof four tasks, a maximum of 200% starts four new tasks before stopping the four tasks to be drained (provided that the cluster resources required to do this are available). If the maximum is 100%, then replacement tasks can't start until the draining tasks have stopped.ImportantIf both
minimumHealthyPercentandmaximumPercentare 100%, then the service can't remove existing tasks, and also cannot start replacement tasks. This prevents successful container instance draining and prevents making new deployments.
Draining behavior for standalone tasks
Any standalone tasks in the PENDING or RUNNING state are
unaffected; you must wait for them to stop on their own or stop them manually. The
container instance will remain in DRAINING status.
Draining behavior for Amazon ECS Managed Instances
Amazon ECS Managed Instances termination ensures graceful workload transitions while optimizing costs and maintaining system health. The termination system provides the following decision paths for instance termination, each with different timing characteristics and customer impact profiles.
- Customer-initiated termination
-
Provides direct control over instance removal when you need to remove container instances from service immediately. You run
deregister-container-instancewith theforcerequest parameter set to true. This means that immediate termination is required despite any running workloads. - System-initiated idle termination
-
Amazon ECS Managed Instances continuously monitors and proactively optimizes costs by terminating idle Amazon ECS container instances not running any tasks. ECS uses a heuristic delay to give container instances a chance to acquire newly launched tasks before being terminated. This can be customized with the
scaleInAfterAmazon ECS Managed Instances capacity provider configuration parameter. - Infrastructure refresh termination
-
Amazon ECS Managed Instances automatically manages and updates software on managed container instances to ensure security and compliance while maintaining workload availability. For more information, see patching in Amazon ECS Managed Instances.
- Instance health replacement
-
Amazon ECS drains and replaces an instance when its container instance health is impaired, for example by a failed GPU or a critical daemon, or when Amazon EC2 reports a problem with the instance. For more information, see Container instance-health issues and Instance lifecycle.
- Infrastructure optimization
-
Amazon ECS drains underutilized instances and moves their service tasks to other instances. For more information, see Underutilized Instance Detection.
Service tasks stopped by this draining have the stopped reason Task in
service , where service-name stopped due to instance draining:
reasonreason
says why the instance is draining. For example, it starts with
INSTANCE_HEALTH or is INFRASTRUCTURE_HEALTH for
instance health replacement, is INFRASTRUCTURE_OPTIMIZATION for
infrastructure optimization, and is PATCHING or
EVENT_WINDOW_PATCHING for infrastructure refresh.
The termination system implements a two-phase approach that balances workload continuity against infrastructure management requirements.
Phase 1: Graceful completion period
During this phase, the system implements graceful draining strategies that prioritize workload continuity. Service tasks are gracefully drained through normal Amazon ECS scheduling processes. Standalone tasks continue running because they might complete naturally. The system monitors for all tasks to reach stopped state through natural completion processes.
Phase 2: Hard deadline enforcement
When graceful completion does not achieve termination objectives within acceptable timeframes, the system implements hard deadline enforcement. The hard deadline is typically set to draining initiation time plus seven days, providing substantial time for graceful completion while maintaining operational requirements. The enforcement includes automatic force deregistration procedures and immediate termination of all remaining tasks regardless of the completion status.
A container instance has completed draining when all tasks running on the instance
transition to a STOPPED state. The container instance remains in a
DRAINING state until it is activated again or deleted. You can verify the
state of the tasks on the container instance by using the ListTasks operation with the
containerInstance parameter to get a list of tasks on the instance followed
by a DescribeTasks operation with
the Amazon Resource Name (ARN) or ID of each task to verify the task state.
When you are ready for the container instance to start hosting tasks again, you change the
state of the container instance from DRAINING to ACTIVE. The Amazon ECS
service scheduler then considers the container instance for task placement again.
Procedure
The following steps can be used to set a container instance to draining using the new AWS Management Console.
You can also use the UpdateContainerInstancesState API action or the update-container-instances-state command to change the status of a
container instance to DRAINING.
AWS Management Console
Open the console at https://console.aws.amazon.com/ecs/v2
. -
In the navigation pane, choose Clusters.
-
On the Clusters page, choose a cluster that hosts your instances.
-
On the Cluster :
namepage, choose the Infrastructure tab. Then, under Container instances select the check box for each container instance you want to drain. -
Choose Actions, Drain.