Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible with AWS AutoScaling to never terminate instances until a billable hour boundary?

Since AWS instances are billed by the hour it never makes sense to terminate an instance that's been running for less than an hour if there is any chance you might need it again.

I'd like to avoid an autoscaling situation where I add an instance, then terminate it and then add another instance all within the same hour. This would result in two billable hours.

I've written my own autoscaler that skips terminating any instance that has less than 55 minutes of runtime and for rapidly change loads this has saved us a lot of expense. Just wondering if AWS itself has the capability.

like image 526
Brian Tarbox Avatar asked Aug 20 '15 15:08

Brian Tarbox


People also ask

How does Auto Scaling terminate an instance?

Amazon EC2 Auto Scaling terminates Spot instances when either of the following occurs: Capacity is no longer available. Spot price exceeds the maximum price that you specified for the instances.

What is default termination policy in Auto Scaling group?

If you did not assign a specific termination policy to the group, Amazon EC2 Auto Scaling uses the default termination policy. It selects the Availability Zone with two instances, and terminates the instance that was launched from the oldest launch template or launch configuration.


1 Answers

Auto Scaling cannot 'wait' until the end of an hour to terminate an instance. However, there's a few options to explore!

When a Scaling Policy is triggered that instructs Auto Scaling to scale-in (remove an Amazon EC2 instance), it first selects an Availability Zone with the most instances and then determines which instance to terminate within that Availability Zone. This choice is made by a Termination Policy, which can have the values of:

  • 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, and want to eventually replace instances with older instances with newer ones.
  • 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.

That last option, ClosestToNextInstanceHour, is almost what you are seeking, in that it will terminate an instance that will next cause an hourly charge. However, it won't 'wait' until the end of the hour.

One option is to write your own application that determines when to scale-in an instance, waiting until the instance has provided its full value. The app could then call TerminateInstanceInAutoScalingGroup to remove and terminate the instance.

Another option, if the instance is being used as a 'back-end' (not directly processing web requests) is to use an Auto Scaling Lifecycle Hook that sends a signal when an instance is removed from an auto scaling group, but before it is terminated. This is normally used to give the instance a chance to "finish off" work, such as copying log files and finishing a task. If the instance is being used to process long-running background tasks, the lifecycle hook could be used to wait until the work is finished or until the billing hour is almost finished, before allowing the instance to be terminated.

All of this, however, requires some custom scripting. The out-of-the-box auto scaling configuration will terminate an instance immediately in response to a scaling policy (unless Connection Draining is in effect).

like image 157
John Rotenstein Avatar answered Nov 07 '22 03:11

John Rotenstein