Demo of EC2 Auto Scaling
Tags: aws, cloud, beginners, autoscaling
Published: 2022-04-02 03:44:26

![Auto Scaling Architecture](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yp4i4h06y9rgf8li2xqj.png)

This is Part II of [Introduction to EC2 Auto Scaling] (https://dev.to/kasukur/introduction-to-ec2-auto-scaling-1mp4)
This blog has been written for **AWS UG Madurai - AWS Cloud Practitioner BOOT CAMP**

> We are going to use `us-east-1` region for this workshop

## Table of Contents
1. [Create a Key Pair](#KP)
2. [Create a Security Group](#SG)
3. [Create an Application Load Balancer](#ALB)
4. [Create a Launch Template](#LT)
5. [Create an Auto Scaling Group](#ASG)
6. [Verification and Monitoring](#verify)
7. [Clean Up](#cleanup)
8. [Summary](#summary)
9. [Referrals](#referrals)


<a name="KP"></a>

### Step 1. Create a Key Pair

1. Navigate to EC2 > Key Pairs > Create Key Pair 

![Key Pair](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q60hwteydqo4p7mlg4u2.png)

<a name="SG"></a>

---

### Step 2. Create a Security Group

1. We are going to create two Security Groups and use Security Group chaining concept

2. Navigate to EC2 > Security Groups > Create a new security group for your ALB, and set the following values:
   * Name: `ALBSG`
   * Add two Inbound rules to allow `HTTP` traffic from `0.0.0.0/0` and `::/0` (IPV6)
   * Create another Security Group with a rule to allow `SSH` from your IP and another rule to allow `HTTP` from `ALBSG` 

---

![Security Group Chaining](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8z63y9r8dia4v5n3gmjn.png)

---

![Security Group ALB](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uthy54gd7odqhxz61s50.png)

---

![Security Group EC2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ke8ni43hz7bgd0k6ny2.png)

<a name="ALB"></a>

---

### Step 3. Create an Application Load Balancer

1. Navigate to EC2 > Load Balancers.
2. Click Create Load Balancer.
3. Click the Create button under the Application Load Balancer and set the following values:
    * Name: `ASALB`
    * Scheme: `internet-facing`
    * IP address type: `ipv4`
    * Load Balancer Protocol: `HTTP`
    * Port: `80`
    * Leave the default `VPC`.
    * Select `us-east-1a` and `us-east-1b` AZs.
4. Click Next: Configure Security Settings 
    * Note: Ignore the warning as we are not using HTTPS.
5. Select `AppLBSG` and Click Next
6. Configure Routing and enter the following values:
    * Name: `ASALBTG`
    * Target type: `Instance`
    * Protocol: `HTTP`
    * Port: `80`
7. Expand Advanced health check settings, and reduce the healthy and unhealthy threshold checks to 2.
    * Note: This means the load balancer can respond faster and instances come into service and vice versa.
8. Click Next: Register Targets.
9. Click Next: Review.
10. Click Create
11. Copy `DNS Name` of the load balancer, open a new tab and enter the URL `http://DNS Name`. 
   * The URL will error with `503 Service Temporarily Unavailable` as we have no operational EC2 instances associated with the load balancer at this stage.

![Application Load Balancer-1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2qozv7flwsleiy7lnbyz.png)

---

![Application Load Balancer-2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i4qfuohd9ruh203w5a4y.png)

---

![Application Load Balancer-3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bn8x1xi53v1v2mfct01s.png)

---

<a name="LT"></a>

### Step 4. Create a Launch Template

We can use Launch template or Launch Configurations. Launch Template are preferred over Launch Configurations as we can have different versions of the template. Also we can't modify a launch configuration after we have created it.

Create a launch template that will be used by the Auto Scaling group. The launch template defines what the instances are and how they are created.

1. Navigate to EC2 > Instances > Launch Templates.
2. Create a new template, and call it `ASLT` for the name.
3. Select `Provide guidance to help me set up a template that I can use with EC2 Auto Scaling`
4. Search for `AMI`, and pick the `Amazon Linux`.
5. Set the instance type as `t2.micro`.
6. Select `key pair` you created earlier.
8. Select the `EC2SG` security group you created earlier.
9. Expand Advanced Details, and paste the following user data.
   * Note: These are commands to install a web server and download website content.
10. Click Create Launch Template.
11. Click Close.

```bash
#!/bin/bash
yum update -y
yum install -y httpd
yum install -y wget
cd /var/www/html
curl http://169.254.169.254/latest/meta-data/local-hostname > index.html
service httpd start

sudo amazon-linux-extras install epel -y 
sudo yum install -y stress
```

![Launch Template-1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q0qa6xtsoouhtviu5ukm.png)

---

![Launch Template-2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z4kssw49bya1a1wasaq3.png)

---

![Launch Template-3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yaq32iad7etvhbkv3lzu.png)

---

<a name="ASG"></a>

### Step 5. Create an Auto Scaling Group

Note: Make sure the load balancer is ready at this point.

1. EC2 > Auto Scaling > Auto Scaling Groups
2. Click Create Auto Scaling group.
3. Call the group `ASG`.
4. Select Launch Template, and choose the template named `ASLT`.
5. We are using `default VPC`, which will be selected, so select `us-east-1a` and `us-east-1b` as subnets.
7. Click Next.
8. Select `Attach to an existing load balancer`.
9. Select target group `ASALBTG`.
10. Leave the default for Health checks, which is `EC2`.
11. Select Enable group metrics collection with CloudWatch.
12. For Group Size, enter the following values:
    * Desired Capacity: `2`
    * Minimum Capacity: `2`
    * Maximum Capacity: `6`
13. For Scaling Policies, select `Target Tracking Policy` and enter the following values:
    * Scaling Policy Name: `Target Tracking Policy`
    * Metric type: `Average CPU utilization`
    * Target value: `30`
    * Instances need: `300`
14. Click Next at `Add Notifications`.
15. Click Next at `Add tags`.
16. Click Create `Auto Scaling Group`.


![Auto Scaling Group-1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0jpur36ywbywy18so6w0.png)

---

![Auto Scaling Group-2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5xry0bikxvw5kbloi3rs.png)

---

![Auto Scaling Group-3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9zrilqmlbyenqbdvjwf5.png)

---

![Auto Scaling Group-4](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y0tzg1v1235zckzrtxt2.png)

---

<a name="verify"></a>

### Step 6. Verification and Monitoring

1. Connect to one of the EC2 instances via SSH by running `chmod 400 AS-KP.pem` and `ssh -i "AS-KP.pem" ec2-user@ec2-44-195-41-102.compute-1.amazonaws.com`
2. Put some CPU load on the server by executing `stress --cpu 4 --timeout 300`
3. After a few minutes, watch the number of instances increase under `EC2` or `ASG > Instance Management`. It enacts the scale-out policy.
4. After a few minutes, the stress test will stop and the ASG enacts the scale-in policy.
5. Navigate to EC2 > Auto Scaling > Auto Scaling Groups > Activity and check under `Activity History`, there should be two or more EC2 instances launched
6. Copy `DNS Name` of the load balancer, open a new tab and enter the URL `http://DNS Name`. Keep refreshing a few times, you would start seeing something similar to `ip-XXX-XX-XX-XXX.ec2.internal` and another `ip-YYY-YY-YY-YYY.ec2.internal`


Here is a sample of the stress test:

```bash
[ec2-user@ip ~]$ stress --cpu 4 --timeout 300
stress: info: [3598] dispatching hogs: 4 cpu, 0 io, 0 vm, 0 hdd
stress: info: [3598] successful run completed in 300s
```

![Scale Out](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jozv3cw1ofsykdyvpiws.png)
> Scale Out

---

![CloudWatch Metric](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7puwsiue2jyprspzwfuw.png)
> CloudWatch Metric

---

![Auto Scaling Groups Activity](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sez9xch10p99wj8v5r05.png)
> Auto Scaling Groups Activity

---

![Scale In](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pos0a16yq6k96jcsby3g.png)
> Scale In

---

<a name="cleanup"></a>
### Clean Up

1. Delete `ASG` under `Auto Scaling groups`.
2. Delete `ASLT` under `Launch Templates`
3. Delete `ASALB` under `Load Balancers`
4. Wait until the Load balancer `ASALB` is deleted and then delete `ASALBTG` under `Target Groups`
5. Delete `AS-KP` under `Key Pairs`
6. Delete Security Groups `EC2SG` first and then `AppLBSG` due to Security Group chaining.

---

<a name="summary"></a>
### Summary
- Use Security Group chaining to improve security of your application
- Best practice is to scale out fast and scale in slowly to prevent oscillation
- There should be a gap between scale-in and scale-out thresholds with step scaling, for example, lets say you have 3 instances, and the CPU goes to 60%, triggering the +1 step scaling policy. If the load stays constant, it will now be distributed to all 4 instances and the average CPU will drop to around 45% and the scale-in alarm will go off. This will then keep happening in a loop until the load goes up or down enough for one of the alarms to stay in the alarm state and the ASG reaches the minimum or maximum.

---

<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 [@marbocatcat](https://unsplash.com/@marbocatcat) from [unsplash](https://unsplash.com/photos/vi3fXw_6ySs).

---