Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I prevent EC2 instance termination by Auto Scaling?

I would like to prevent EC2 instance termination by Auto Scaling feature if that instance is in the middle of some sort of processing.

Background:
Suppose I have an Auto Scaling group that currently has 5 instances running. I create an alarm on average CPU usage... Suppose 4 of the instances are idle and one is doing some heavy processing... The average CPU load will trigger the alarm and as a result the scale-down policy will execute.

How do I get Auto Scaling to terminate one of the idle instances and not the one that is in the middle of the processing?

like image 624
SunnyDark Avatar asked Jul 08 '13 12:07

SunnyDark


1 Answers

Update

As noted by Ryan Walls (+1), AWS meanwhile provides Instance Protection to control whether Auto Scaling can terminate a particular instance when scaling in (see the introductory blog post Instance Protection for Auto Scaling for a walk through):

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. [...]

It's worth noting that this instance protection only applies to regular Auto Scaling scale in events:

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.

As usual, the feature is available via the AWS Management Console (menu Actions->Instance Protection->Set Scale In Protection)), the AWS CLI (set-instance-protection command), and the API (SetInstanceProtection API action).

The latter two options allow automation of the scenario at hand, i.e. one would need to enable instance protection before running 'heavy processing' jobs, and disable instance protection once they are finished so that the instance is eligible for termination again.


Initial Answer

This functionality is currently not available for Auto Scaling of Amazon EC2 instances - while you are indeed able to Configure [an] Instance Termination Policy for Your Auto Scaling Group, the available policies do not include such a (fairly advanced) concept:

Auto Scaling provides the following termination policy options for you to choose from. You can specify one or more of these options in your termination policy.

  • OldestInstance — Specify this if you want the oldest instance in your Auto Scaling group to be terminated. [...]

  • NewestInstance — Specify this if you want the last launched instance to be terminated. [...]

  • OldestLaunchConfiguration — Specify this if you want the instance launched using the oldest launch configuration to be terminated. [...]

  • ClosestToNextInstanceHour — Specify this if you want the instance that is closest to completing the billing hour to be terminated. [...]

  • Default — Specify this if you want Auto Scaling to use the default termination policy to select instances for termination.

like image 77
Steffen Opel Avatar answered Sep 24 '22 14:09

Steffen Opel