With each Auto Scaling group, you control when to add 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, and you can automate the process through the use of a scaling policy.
When you have Auto Scaling automatically scale in, you need to decide which instances should terminate first. You can configure this through the use of a termination policy.
Consider an Auto Scaling group that has a desired capacity of two instances, and scaling policies that increase and decrease the number of instances by 1 when certain thresholds are met.
An increase in traffic occurs, which results in the Auto Scaling group launching a new instance. The Auto Scaling group now has three instances in it.
After a period of time, the amount of traffic subsides, and the scale in policy takes effect. This results in Auto Scaling terminating one of the instances. Because this group does not have a specific termination policy assigned to it, Auto Scaling uses the default termination policy. This means that:
Auto Scaling first checks to see if it has instances in multiple Availability Zones. If one Availability Zone has more instances in it than another, it terminates an instance from that Availability Zone. If both Availability Zones have an equal number of instances, Auto Scaling selects a random Availability Zone from which to terminate an instance.
Auto Scaling next looks to see which instance in the Availability Zone uses the oldest launch
configuration. Auto Scaling makes this determination by using
DescribeLaunchConfigurations and using the value in the
If Auto Scaling detects that more than one instance use the oldest launch configuration, it identifies which instance is closest to the next billing hour, and terminates that instance.
If more than one instance is closest to the next billing hour, Auto Scaling selects the instance randomly.
This default termination policy helps ensure that your network architecture spans Availability Zones evenly. By selecting an instance closest to the next billing hour, Auto Scaling also helps you maximize the use of your EC2 instances while minimizing the number of hours you are billed for Amazon EC2 usage.
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 selects an Availability Zone at random and applies the termination policy that you specified.
Auto Scaling currently supports the following custom termination policies:
OldestInstance. With this policy in place, 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. This policy instructs Auto Scaling to terminate the newest instance in the Auto Scaling group. You might use this policy if you are testing a new launch configuration but don’t want to keep the new configuration in production.
OldestLaunchConfiguration. Use this termination policy when you want to Auto Scaling to terminate instances that have the oldest launch configuration. This policy can help when you’re updating an Auto Scaling group and want to phase out instances using a previous configuration.
ClosestToNextInstanceHour. This policy helps you run your Auto Scaling group in a cost-effective manner. With this policy in place, Auto Scaling terminates instances that are closest to the next billing hour. This allows you to maximize your use of the EC2 instances in your Auto Scaling group.
Default. This policy instructs Auto Scaling to use its default termination policy. You might use this policy when you have more than one scaling policy associated with a given Auto Scaling group.
You can use these policies individually, or combine them into a list of policies that Auto Scaling uses when terminating instances. For example, the following AWS CLI command updates an Auto Scaling group to use the OldestLaunchConfiguration policy first, and then to use the ClosestToNextInstanceHour policy:
aws autoscaling update-auto-scaling-group --auto-scaling-group-name my-asg --termination-policies "OldestLaunchConfiguration,ClosestToNextInstanceHour"
If you use the default termination policy, make sure it's the last one in the list of termination policies. For example:
aws autoscaling update-auto-scaling-group --auto-scaling-group-name my-asg --termination-policies "OldestLaunchConfiguration,Default"
You can customize a termination policy for an Auto Scaling group using the AWS Management Console, AWS CLI, or the Auto Scaling API.
To customize a termination policy with the console:
Create the Auto Scaling group. For more information, see Creating Your Auto Scaling Groups.
From the EC2 Dashboard, click Auto Scaling Groups.
Select the group to update.
From the Actions menu, select Edit.
On the Details tab, locate the Termination Policies field. Select one or more termination policies.
To customize a termination policy using the AWS CLI, use one of the following commands:
aws autoscaling create-auto-scaling-group
aws autoscaling update-auto-scaling-group
To customize a termination policy using the Auto Scaling API, use one of the following actions: