AWS Quick Notes — EC2 Autoscaling

Gunschu Blog
3 min readSep 27, 2021

--

AutoScaling is a powerful feature in AWS that helps either add(scale in) or remove(scale out) instances to meet the load on an application.

In a simple case, if an instance fails, the instance is replaced. If an entire AZ fails, Auto Scaling can start instances in other AZ’s that may be still operational.

Similarly instances can be scaled in/down based on certain criteria,

Here is the configuration for an Auto Scaling group —

Max — How many can be added max when load goes up

Min — Whats the least number of instances will always run ?

Desired Capacity — No of instances required for current state — traffic.

Based on these configuration values, Auto Scaling can either maintain the fleet size.

Auto Scaling groups will help scale out and scale in as necessary, launches and terminates instances dynamically.

ASG can also automatically register new instances to a load balancer.

Although optional most often High Availability architectures have Load Balancers and ASGs that work hand in hand.

Cloudwatch Alarms can be used to scale out or scale in, target average CPU, no of requests on ELB , network in/out, etc. Custom Metric can be used and can be sent to Cloudwatch also.

ASGs use launch configurations or launch templates.

Warm pools are ready to go instances that can be used by the ASG.ma

Automatic Scaling Policies

Dynamic scaling techniques supported by AWS are as follows —

Target

For example if you can set the metric to the Avg CPU to stay at x%.

Simple

Example — If CPU goes >x add 2 instances.

Step

Example — if CPU goes > 40% add 1 instance, if CPU goes > 60% add 2 instances and so forth.

Scheduled Actions

Add or remove instances at a certain time.

Predictive Scaling

ML Based scaling , based on a forecast instead of a target.

Important Note: 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. If the instances were launched from the same launch template or launch configuration, Amazon EC2 Auto Scaling selects the instance that is closest to the next billing hour and terminates it. https://docs.aws.amazon.com/autoscaling/ec2/userguide/as-instance-termination.html

Good metrics to use

Average CPU Utilization

RequestCountPerTarget

Average Network In or Out

Any custom metric

What is Scaling cool down ?

After scaling activity occurs , you are in cool down — 300 seconds.

During cooldown no launch or terminate occurs since metrics are still stabilizing,

If we reduce configuration and bootstrap time , we would be able to reduce cooldown period and take live requests quicker.

ASG Termination Rules

How are the instances terminated ? There are termination rules.
Find the AZ that has most instances, delete the one with oldest launch config or template.

ASG Lifecycle Hooks

Lifecycle Hooks allow software configuration at startup or log collection at termination.

Uses

A popular use of lifecycle hooks is to control when instances are registered with Elastic Load Balancing. By adding a launch lifecycle hook to your Auto Scaling group, you can ensure that your bootstrap scripts have completed successfully and the applications on the instances are ready to accept traffic before they are registered to the load balancer at the end of the lifecycle hook. From . https://docs.aws.amazon.com/autoscaling/ec2/userguide/lifecycle-hooks.html

Another use of lifecycle hooks is when databases need to be backed up prior to termination.

https://aws.amazon.com/autoscaling/ — Lifecycle Hooks

ASG Event Notifications

All activity can be relayed through SNS Notifications .

Cost Optimization

When creating an ASG , you can now allocate percentages what type of instance types should be used to optimize costs. For example you can mix on-demand and spot instances for a cost effective architecture.

Launch Template vs Launch Configuration

Launch Configs have to be recreated every time and cannot be versioned.

Launch Configs are legacy and should not be used.

Resources

https://tutorialsdojo.com/aws-auto-scaling/

--

--

Gunschu Blog

Gunschu is creating the world’s greatest coaching platform through groundbreaking tools, advice, knowledge and personal expertise.