Menu
Auto Scaling
Developer Guide

Controlling Which Instances Auto Scaling Terminates During Scale In

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.

You can also use instance protection to prevent Auto Scaling from selecting specific instances for termination when scaling in.

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:

  1. Auto Scaling determines whether there are instances in multiple Availability Zones. If so, it selects the Availability Zone with the most instances and at least one instance that is not protected from scale in. 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.

  2. Auto Scaling determines which unprotected instances in the selected Availability Zone use the oldest launch configuration. If there is one such instance, it terminates it.

  3. If there are multiple instances that use the oldest launch configuration, Auto Scaling determines which unprotected 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.

  4. If there is more than one unprotected 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.

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

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.

A basic Auto Scaling group.

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.

An Auto Scaling group after a scaling action occurs.

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, so you can gradually replace instances of the old type with instances of the new type.

  • 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

  1. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.

  2. In the navigation pane, choose Auto Scaling Groups.

  3. Select the Auto Scaling group from the list.

  4. For Actions, choose Edit.

  5. 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 Default policy, make it the last one in the list.

  6. Choose Save.

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 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 it the last one in the list of termination policies. For example, --termination-policies "OldestLaunchConfiguration,Default".

Instance Protection

To control whether Auto Scaling can terminate a particular instance when scaling in, use instance protection. You can enable the instance protection setting on an Auto Scaling group or an individual Auto Scaling instance. When Auto Scaling launches an instance, the instance inherits the instance protection setting of the Auto Scaling group. You can change the instance protection setting for an Auto Scaling group or an Auto Scaling instance at any time.

Instance protection starts when the instance state is InService. If you detach an instance that is protected from termination, its instance protection setting is lost. When you attach the instance to the group again, it inherits the current instance protection setting of the group.

If all instances in an Auto Scaling group are protected from termination during scale in and a scale-in event occurs, Auto Scaling decrements the desired capacity. However, Auto Scaling can't terminate the required number of instances until their instance protection settings are disabled.

Instance protection does not protect Auto Scaling instances from manual termination through the Amazon EC2 console, the terminate-instances command, or the TerminateInstances API. Instance protection does not protect an Auto Scaling instance from termination if it fails health checks and must be replaced. Also, instance protection does not protect Spot instances in an Auto Scaling group from interruption.

Enable Instance Protection for a Group

You can enable instance protection when you create an Auto Scaling group. By default, instance protection is disabled.

To enable instance protection using the console

When you create the Auto Scaling group, on the Configure Auto Scaling group details page, under Advanced Details, select the Protect From Scale In option from Instance Protection.

The Instance Protection field in the wizard.

To enable instance protection using the AWS CLI

Use the following create-auto-scaling-group command to enable instance protection:

aws autoscaling create-auto-scaling-group --auto-scaling-group-name my-asg --new-instances-protected-from-scale-in ...

Modify the Instance Protection Setting for a Group

You can enable or disable the instance protection setting for an Auto Scaling group.

To change the instance protection setting for a group using the console

  1. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.

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

  3. Select the Auto Scaling group from the list.

  4. Select the Details tab, and then click Edit.

  5. Locate Instance Protection, click the text field, and then select Protect From Scale In.

    The Instance Protection field on the Details tab.
  6. Click Save.

To change the instance protection setting for a group using the AWS CLI

Use the following update-auto-scaling-group command to enable instance protection for the specified Auto Scaling group:

aws autoscaling update-auto-scaling-group --auto-scaling-group-name my-asg --new-instances-protected-from-scale-in

Use the following command to disable instance protection for the specified group:

aws autoscaling update-auto-scaling-group --auto-scaling-group-name my-asg --no-new-instances-protected-from-scale-in

Modify the Instance Protection Setting for an Instance

By default, an instance gets its instance protection setting from its Auto Scaling group. However, you can enable or disable instance protection for an instance at any time.

To change the instance protection setting for an instance using the console

  1. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.

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

  3. Select the Auto Scaling group from the list.

  4. Select the Instances tab and then select the instance.

  5. Click Actions, select Instance Protection, and then do one of the following:

    • Choose Set Scale In Protection. When prompted, click Set Scale In Protection.

    • Choose Remove Scale In Protection. When prompted, click Remove Scale In Protection.

To change the instance protection setting for an instance using the AWS CLI

Use the following set-instance-protection command to enable instance protection for the specified instance:

aws autoscaling set-instance-protection --instance-ids i-5f2e8a0d --auto-scaling-group-name my-asg --protected-from-scale-in

Use the following command to disable instance protection for the specified instance:

aws autoscaling set-instance-protection --instance-ids i-5f2e8a0d --auto-scaling-group-name my-asg --no-protected-from-scale-in