Configure termination policies for Amazon EC2 Auto Scaling - Amazon EC2 Auto Scaling

Configure termination policies for Amazon EC2 Auto Scaling

A termination policy provides the criteria that Amazon EC2 Auto Scaling follows to decide the order for terminating instances.

By default, Amazon EC2 Auto Scaling uses a termination policy that's designed to terminate instances that are using outdated configurations first. However, you can change the termination policy to control which instances are most important to terminate first.

When Amazon EC2 Auto Scaling terminates instances, it tries to maintain balance across the Availability Zones that are enabled for your Auto Scaling group. Maintaining zonal balance takes precedence over the termination policy. If one Availability Zone has more instances than others, Amazon EC2 Auto Scaling applies the termination policy to the imbalanced zone first. If the Availability Zones are balanced, it applies the termination policy across all zones.

How the default termination policy works

When Amazon EC2 Auto Scaling needs to terminate an instance, it first identifies which Availability Zone (or Zones) has the most instances and at least one instance that is not protected from scale in. Then, it proceeds to evaluate unprotected instances within the identified Availability Zone as follows:

Instances that use outdated configurations
  • For groups that use a launch template – Determine whether any of the instances use outdated configurations, prioritizing in this order:

    1. First, check for instances launched with a launch configuration.

    2. Then, check for instances launched using a different launch template instead of the current launch template.

    3. Finally, check for instances using the oldest version of the current launch template.

  • For groups that use a launch configuration – Determine whether any of the instances use the oldest launch configuration.

If no instances with outdated configurations are found, or there are multiple instances to choose from, Amazon EC2 Auto Scaling considers the next criteria of instances approaching their next billing hour.

Instances approaching next billing hour

Determine whether any of the instances that meet the previous criteria are closest to the next billing hour. If multiple instances are equally close, terminate one at random. This helps you maximize the use of your instances that are billed hourly. However, most EC2 usage is now billed per second, so this optimization provides less benefit. For more information, see Amazon EC2 pricing.

The following flow diagram illustrates how the default termination policy works for groups that use a launch template.


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

Default termination policy and mixed instances groups

Amazon EC2 Auto Scaling applies additional criteria when terminating instances in mixed instances groups.

When Amazon EC2 Auto Scaling needs to terminate an instance, it first identifies which purchase option (Spot or On-Demand) should be terminated based on the settings of the group. This makes sure that the group trends toward the specified ratio of Spot and On-Demand instances over time.

It then applies the termination policy independently within each Availability Zone. It determines which Spot or On-Demand Instance in which Availability Zone to terminate to keep the Availability Zones balanced. The same logic applies to a mixed instances group with weights defined for the instance types.

Within each zone, the default termination policy works as follows to determine which unprotected instance within the identified purchase option can be terminated:

  1. Determine whether any of the instances can be terminated to improve alignment with the specified allocation strategy for the Auto Scaling group. If no instances are identified for optimization, or there are multiple instances to choose from, the evaluation continues.

  2. Determine whether any of the instances use outdated configurations, prioritizing in this order:

    1. First, check for instances launched with a launch configuration.

    2. Then, check for instances launched using a different launch template instead of the current launch template.

    3. Finally, check for instances using the oldest version of the current launch template.

    If no instances with outdated configurations are found, or there are multiple instances to choose from, the evaluation continues.

  3. Determine whether any of the instances are closest to the next billing hour. If multiple instances are equally close, choose one at random.

Predefined termination policies

You choose from the following predefined termination policies:

  • Default – Terminate instances according to the default termination policy.

  • AllocationStrategy – Terminate instances in the Auto Scaling group to align the remaining instances to the allocation strategy for the type of instance that is terminating (either a Spot Instance or an On-Demand Instance). This policy is useful when your preferred instance types have changed. If the Spot allocation strategy is lowest-price, you can gradually rebalance the distribution of Spot Instances across your N lowest priced Spot pools. If the Spot allocation strategy is capacity-optimized, you can gradually rebalance the distribution of Spot Instances across Spot pools where there is more available Spot capacity. You can also gradually replace On-Demand Instances of a lower priority type with On-Demand Instances of a higher priority type.

  • OldestLaunchTemplate – Terminate instances that have the oldest launch template. With this policy, instances that use the noncurrent launch template are terminated first, followed by instances that use the oldest version of the current launch template. This policy is useful when you're updating a group and phasing out the instances from a previous configuration.

  • OldestLaunchConfiguration – Terminate 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. With this policy, instances that use the noncurrent launch configuration are terminated first.

  • ClosestToNextInstanceHour – Terminate instances that are closest to the next billing hour. This policy helps you maximize the use of your instances that have an hourly charge.

  • NewestInstance – Terminate 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.

  • OldestInstance – Terminate 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. You can gradually replace instances of the old type with instances of the new type.

    Note

    Amazon EC2 Auto Scaling always balances instances across Availability Zones first, regardless of which termination policy is used. As a result, you might encounter situations in which some newer instances are terminated before older instances. For example, when there is a more recently added Availability Zone, or when one Availability Zone has more instances than the other Availability Zones that are used by the group.

Change the termination policy for an Auto Scaling group

To change the termination policy for your Auto Scaling group, use one of the following methods.

Console

You can't change the termination policy when you initially create an Auto Scaling group in the Amazon EC2 Auto Scaling console. The default termination policy is used automatically. After your Auto Scaling group has been created, you can replace the default policy with a different termination policy or multiple termination policies listed in the order in which they should apply.

To change the termination policy for an Auto Scaling group
  1. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/, and choose Auto Scaling Groups from the navigation pane.

  2. Select the check box next to the Auto Scaling group.

    A split pane opens up in the bottom of the page.

  3. On the Details tab, choose Advanced configurations, Edit.

  4. For Termination policies, choose one or more termination policies. If you choose multiple policies, put them in the order that you want them evaluated in.

    You can optionally choose Custom termination policy and then choose a Lambda function that meets your needs. If you have created versions and aliases for your Lambda function, you can choose a version or alias from the Version/Alias drop-down. To use the unpublished version of your Lambda function, keep Version/Alias set to its default. For more information, see Create a custom termination policy with Lambda.

    Note

    When using multiple policies, their order must be set correctly:

    • If you use the Default policy, it must be the last policy in the list.

    • If you use a Custom termination policy, it must be the first policy in the list.

  5. Choose Update.

AWS CLI

The default termination policy is used automatically unless a different policy is specified.

To change the termination policy for an Auto Scaling group

Use one of the following commands:

You can use termination policies individually, or combine them into a list of policies. For example, use the following command to update an Auto Scaling group to use the OldestLaunchConfiguration policy first and then 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".

To use a custom termination policy, you must first create your termination policy using AWS Lambda. To specify the Lambda function to use as your termination policy, make it the first one in the list of termination policies. For example, --termination-policies "arn:aws:lambda:us-west-2:123456789012:function:HelloFunction:prod" "OldestLaunchConfiguration". For more information, see Create a custom termination policy with Lambda.