Introduction to EC2 Auto Scaling
Tags: aws, cloud, autoscaling, beginners
Published: 2022-03-19 08:07:47

![aws-auto-scaling-how-it-works-diagram](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ay5j5fov1h7qvvbcwfsu.png)
   > Picture source: AWS

This blog has been written for **AWS UG Madurai - AWS Cloud Practitioner BOOT CAMP**
 
## Table of Contents
1. [Introduction](#intro)
    * [Benefits](#benefits)
    * [Veritcal vs Horizontal Scaling](#vs)
2. [What is EC2 Auto Scaling?](#autoscaling)    
3. [EC2 Auto Scaling Lifecylce](#lifecycle)
4. [Types of Scaling](#types)
5. [Warm Pool](#warmpool)
6. [Scaling Termination Policy](#terminationpolicy)
7. [Recommended Further Reading](#recommended)
8. [Referrals](#referrals) 

<a name="intro"></a>
### Introduction
AWS Auto Scaling monitors your applications and automatically adjusts capacity to maintain steady, predictable performance at the lowest possible cost. Using AWS Auto Scaling, it’s easy to setup application scaling for multiple resources across multiple services in minutes.

<a name="benefits"></a>
#### Benefits
- Setup Scaling quickly
- Make SMART scaling decisions
- Automatically maintain performance
- Pay only for what you need

<a name="vs"></a>
#### Veritcal vs Horizontal Scaling
- **Veritcal Scaling:** Upgrading server hardware, For example, Changing Instance type from t2.medium (2 vCPU, 24GB Memory) to t2.2xlarge (8 vCPU, 32GB Memory)

_Use-Case:_ A data-processing job may require more vCPU and memory in order to complete in a shorter period of time. 
> Considerations:
> - An Instance restart is required. 
> - You could only be able to vertically scale to the maximum limit of instance availability, For example, m6a.48xlarge	(192 vCPU, 192 Memory).

- **Horizontal Scaling:** Add more instances to the existing instances

![Types of Scaling](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3v372m35kr2aqfsacngg.png)

<a name="autoscaling"></a>
### What is EC2 Auto Scaling?

Amazon EC2 Auto Scaling helps to maintain the number of instances in each Auto Scaling Group.

For example, If we define minimum size of one instance, a desired capacity of two instances, and a maximum size of four instances.
The Auto Scaling policies will maintain the number of instances within the minimum and maximum number of instances. By default, the desired number of instances will always be available, which is two in this scenario.

![as-basic-diagram](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sqax2ycx2x6xu5wysfvs.png)
   > Picture source: AWS

<a name="lifecycle"></a>
### EC2 Auto Scaling Lifecylce
The following illustration shows the lifecycle of an Auto Scaling instance

![auto_scaling_lifecycle](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kjjwxat42pu0hqzhj83f.png)
   > Picture source: AWS

<a name="types"></a>
### Types of Scaling

- **Manual Scaling:** Change the size of Auto Scaling Group manually by updating the desired capacity.
- **Predictive Scaling:** To increase the number of EC2 instances in your Auto Scaling group in advance of daily and weekly patterns in traffic flows.
- **Scheduled Scaling:** To set up your own scaling schedule according to predictable load changes. For example, let's say every week on Wednesday, the traffic to the application increases and decreases on Friday. 
- **Dynamic Scaling:** 

  - **Simple Scaling:** We choose scaling metrics and threshold values for the CloudWatch alarms that invoke the scaling process. 
  - **Step Scaling:** We specify one or more step adjustments that automatically scale the number of instances dynamically based on the size of the alarm breach. Each step adjustment specifies the following:
     * A lower bound for the metric value
     * An upper bound for the metric value
     * The amount by which to scale, based on the scaling adjustment type 

        For example:

         Step 1: Add 1 instance if CPU utilization is over 40%. 
    
         Step 2: Add 3 instances if CPU utilization is over 60% and similarly scale in when the CPU utilization reduces.

  - **Target Tracking:** We can select a scaling metric and set a target value. For example, to keep the average aggregate CPU utilization of your Auto Scaling group at 50 percent

  > The main issue with simple scaling is that after a scaling activity is started, the policy must wait for the scaling activity or health check replacement to complete and the cooldown period to expire before responding to additional alarms. Cooldown periods help to prevent the initiation of additional scaling activities before the effects of previous activities are visible.activities are visible.

<a name="warmpool"></a>
### Warm Pool
- A warm pool is a pool of pre-initialized EC2 instances that sits alongside an Auto Scaling group. Whenever your application needs to scale out, the Auto Scaling group can draw on the warm pool to meet its new desired capacity. This helps you to ensure that instances are ready to quickly start serving application traffic, accelerating the response to a scale-out event. As instances leave the warm pool, they count toward the desired capacity of the group. This is known as a warm start.
- The warm pool is calculated as the difference between the Auto Scaling group's maximum capacity and its desired capacity.
- The instances in the warm pool in one of three states: Stopped, Running, or Hibernated. Keeping instances in a Stopped state is an effective way to minimize costs. With stopped instances, you pay only for the volumes that you use and the Elastic IP addresses attached to the instances. But you don't pay for the stopped instances themselves. You pay for the instances only when they are running.
  Alternatively, you can keep instances in a Hibernated state to stop instances without deleting their memory contents (RAM). When an instance is hibernated, this signals the operating system to save the contents of your RAM to your Amazon EBS root volume. When the instance is started again, the root volume is restored to its previous state and the RAM contents are reloaded. While the instances are in hibernation, you pay only for the EBS volumes, including storage for the RAM contents, and the Elastic IP addresses attached to the instances.

<a name="terminationpolicy"></a>
### Scaling Termination Policy
The default termination policy is as following, however you can change or add termination policies to an ASG.

![Termination Policy](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jrm1gpzjfip7c9mjkrhf.png)

<a name="recommended"></a>
### Recommended Further Reading
- [Suspending and resuming a process for an Auto Scaling group] (https://docs.aws.amazon.com/autoscaling/ec2/userguide/as-suspend-resume-processes.html)


<a name="referrals"></a>
### Referrals
- [Amazon EC2 Auto Scaling] (https://docs.aws.amazon.com/autoscaling/ec2/userguide/what-is-amazon-ec2-auto-scaling.html)
- Cover Image by [@tomparkes](https://unsplash.com/@tomparkes) from [unsplash](https://unsplash.com/photos/Ns-BIiW_cNU).