Trigger AWS Autoscale automatically

22 views Asked by At

I have an Autoscale Web Application in AWS. When I don't use it, I terminated EC2 instances automatically with Lambda (with Cloudwatch). But I couldn't find an automatic structure to trigger it again.

What I have tried.

  • I tried with Cloudwatch but I have to wait after the first request. It's not suitable for my setup.
  • I trigger ELB by redirecting the requests coming to port 80 to Lambda and then redirect my main project 443, but it didn't seem very healthy.

If you have other suggestions or services I can use, can you share them?

1

There are 1 answers

1
John Rotenstein On

If you want an immediate response to traffic, then you should not scale to zero instances. Launching or Starting an instance takes time, which probably won't meet your requirement. Therefore, you should always keep a minimum of one instance running at all times.

It is possible to create a warm pool of instances that can be quickly added to the Auto Scaling group. These instances can be kept in a Stopped or Hibernated state to reduce costs, but they will still take time to launch. See: Warm pools for Amazon EC2 Auto Scaling - Amazon EC2 Auto Scaling

The typical pattern is:

  • Create a Scale-Out Alarm that can detect when the current instances in the Auto Scaling group are above desired capacity. Use the Alarm to trigger an Auto Scaling policy that adds another instance to the group.
  • Create a Scale-In Alarm that can detect when there are too many resources, which means you are over-paying for the app. Use the Alarm to trigger an Auto Scaling policy that removes an instance from the group. You can specify a minimum size for the Auto Scaling group, such as specifying that there should always be a minimum of one instance.

A general rule is "Scale-Out quickly, Scale-In slowly":

  • You would want the Scale-Out Alarm to respond reasonably quickly (eg based on a metric over a few minutes). However, be careful because responding too quickly to a usage spike might add capacity that isn't needed. This would cause the Auto Scaling group to 'flap' -- that is, adding and removing capacity too quickly.
  • To prevent flapping, configure the Scale-In Alarm to wait some time before removing resources (eg using a metric over a 15-minute period or longer). It is often better to have some under-utilised resources rather than continually adding and removing resources, so go slowly when deciding to scale-in.

You mention "I terminated EC2 instances automatically with Lambda (with CloudWatch)". However, EC2 Auto Scaling does not use Lambda functions. Instead, it uses CloudWatch Alarms to monitor metrics and Auto Scaling policies to determine how to respond to Alarms. You can, of course, create your own auto-scaling capabilities using Lambda, but you would miss out on the many capabilities of EC2 Auto Scaling.