Monday 22 June 2015

AWS Tip: Terminating instances in an Auto Scaling group

EC2 instances launched by Auto Scaling really should not be terminated outside of Auto Scaling but if for some reason you (really, really) need to terminate a number of instances you may see some unexpected behaviour if you terminate them using the EC2 console. Instead of all the instances being replaced by Auto Scaling simultaneously they are replaced individually over a period of time. This means that it may take significantly longer than you would expect to get the ASG back to the original size. As an example an instance in my test ASG was replaced every 2 minutes, taking around 6 minutes to terminate and replace 3 instances.

There are two ways of doing this more efficiently (using the supremely useful AWS CLI), firstly by setting the group capacity and then resetting it:

Note that update-auto-scaling-group is used due to the minimum size being set, if no minimum size is specified the same result can be achieved with set-desired-capacity.

The second option is to use terminate-instance-in-auto-scaling-group: