Auto Scaling
Developer Guide (API Version 2011-01-01)
Did this page help you?  Yes | No |  Tell us about it...
« PreviousNext »
View the PDF for this guide.Go to the AWS Discussion Forum for this product.Go to the Kindle Store to download this guide in Kindle format.

Choosing a Termination Policy

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.

Default 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.

A basic Auto Scaling group.

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.

An Auto Scaling group after a scaling action occurs.

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:

  1. 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.

  2. 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 CreatedTime field.

  3. 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.

  4. If more than one instance is closest to the next billing hour, Auto Scaling selects the instance randomly.

A flowchart showing how Auto Scaling uses the default termination policy to terminate instances.

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.

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 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, --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 using the console

  1. Create the Auto Scaling group. For more information, see Creating Your Auto Scaling Groups.

  2. From the navigation pane, click Auto Scaling Groups.

  3. Select the group to update.

  4. From the Actions menu, select Edit.

  5. On the Details tab, locate the Termination Policies field. Select one or more termination policies.

  6. Click Save.

To customize a termination policy using the AWS CLI, use one of the following commands:

To customize a termination policy using the Auto Scaling CLI, use one of the following commands:

  • as-create-auto-scaling-group

  • as-update-auto-scaling-group