Choosing a Termination Policy for Your Auto Scaling Group
With each Auto Scaling group, you control when Auto Scaling adds instances (referred to as scaling out) or remove instances (referred to as scaling in) from your network architecture. You can scale the size of your group manually by attaching and detaching instances, or you can automate the process through the use of a scaling policy.
When you have Auto Scaling automatically scale in, you must decide which instances Auto Scaling should terminate first. You can configure this through the use of a termination policy.
Default Termination Policy
The default termination policy is designed to help ensure that your network architecture spans Availability Zones evenly. When using the default termination policy, Auto Scaling selects an instance to terminate as follows:
Auto Scaling determines whether there are instances in multiple Availability Zones. If so, it selects the Availability Zone with the most instances. If there is more than one Availability Zone with this number of instances, Auto Scaling selects the Availability Zone with the instances that use the oldest launch configuration.
Auto Scaling determines which instances in the selected Availability Zone use the oldest launch configuration. If there is one such instance, it terminates it.
If there are multiple instances that use the oldest launch configuration, Auto Scaling determines which instances are closest to the next billing hour. (This helps you maximize the use of your EC2 instances while minimizing the number of hours you are billed for Amazon EC2 usage.) If there is one such instance, Auto Scaling terminates it.
If there is more than one instance closest to the next billing hour, Auto Scaling selects one of these instances at random.
The following flow diagram illustrates how the default termination policy works.
Consider an Auto Scaling group that has two Availability Zones, a desired capacity of two instances, and scaling policies that increase and decrease the number of instances by 1 when certain thresholds are met. The two instances in this group are distributed as follows.
When the threshold for the scale out policy is met, the policy takes effect and Auto Scaling launches a new instance. The Auto Scaling group now has three instances, distributed as follows.
When the threshold for the scale in policy is met, the policy takes effect and Auto Scaling terminates one of the instances. If the group does not have a specific termination policy assigned to it, Auto Scaling uses the default termination policy. Auto Scaling selects the Availability Zone with two instances, and terminates the instance launched from the oldest launch configuration. If the instances were launched from the same launch configuration, then Auto Scaling selects the instance that is closest to the next billing hour and terminates it.
Customizing the Termination Policy
The default termination policy assigned to an Auto Scaling group is typically sufficient for most situations. However, you have the option of replacing the default policy with a customized one.
When you customize the termination policy, Auto Scaling first assesses the Availability Zones for any imbalance. If an Availability Zone has more instances than the other Availability Zones that are used by the group, then Auto Scaling applies your specified termination policy on the instances from the imbalanced Availability Zone. If the Availability Zones used by the group are balanced, then Auto Scaling applies the termination policy that you specified.
Auto Scaling currently supports the following custom termination policies:
OldestInstance. Auto Scaling terminates the oldest instance in the group. This option is useful when you're upgrading the instances in the Auto Scaling group to a new EC2 instance type, and want to eventually replace instances with older instances with newer ones.
NewestInstance. Auto Scaling terminates the newest instance in the group. This policy is useful when you're testing a new launch configuration but don't want to keep it in production.
OldestLaunchConfiguration. Auto Scaling terminates instances that have the oldest launch configuration. This policy is useful when you're updating a group and phasing out the instances from a previous configuration.
ClosestToNextInstanceHour. Auto Scaling terminates instances that are closest to the next billing hour. This policy helps you maximize the use of your instances and manage costs.
Default. Auto Scaling uses its default termination policy. This policy is useful when you have more than one scaling policy associated with the group.
To customize a termination policy using the console
Create the Auto Scaling group. For more information, see Creating Auto Scaling Groups.
In the navigation pane, choose Auto Scaling Groups.
Select the group to update.
For Actions, choose Edit.
On the Details tab, locate Termination Policies. Choose one or more termination policies. If you choose multiple policies, list them in the order that you would like them to apply. If you use the
Defaultpolicy, make it the last one in the list.
To customize a termination policy using the AWS CLI
Use one of the following commands:
You can use these policies individually, or combine them into a list of policies that Auto Scaling
uses when terminating instances. For example, use the following command to update an Auto Scaling
group to use the
OldestLaunchConfiguration policy first, and then to use the
aws autoscaling update-auto-scaling-group --auto-scaling-group-name
If you use the
Default termination policy, make it the last one in the list of
termination policies. For example,