# Auto Scaling Groups

Today we will learn about Auto Scaling Group.

Auto Scaling Groups (ASGs), as the name suggests, automatically adjust the number of EC2 instances in response to demand. They help ensure your application is both responsive and cost-effective by scaling up during high traffic and scaling down during low traffic.

Let's get started.

---

### Overview

In this setup, we will create an Auto Scaling Group (ASG) to manage the provisioning and scaling of EC2 instances based on changing demand. The ASG will automatically adjust the number of instances in response to load fluctuations, ensuring optimal performance and cost-efficiency.

---

### Task 1: Creating a VPC

Search for **VPC** in the search box.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717208882238/3700954c-b274-4058-9843-33d924b5b6ff.png align="center")

Click on `Create VPC`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717208954537/8ec256a2-c375-4b7d-b04d-06caba404e68.png align="center")

Provide a name for your VPC and input a CIDR range.

Keeping all other configurations default, click on `Create VPC`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717209052298/5ca7df13-2ce5-4520-9821-4f635147196f.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717299369291/09b6f635-0ce5-45c4-867d-2731ba2c2b70.png align="center")

---

### Task 2: Creating Internet Gateway

On **Internet gateways**, click on `Create internet gateway`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717209124156/646ff0be-a47f-4895-8bed-50208f0df7d0.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717209171030/e59da978-52b4-46b7-9c2b-4ec2e766a5a4.png align="center")

Provide a name for your Internet Gateway and click on `Create internet gateway`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717209215189/05a492a2-0430-4488-90f8-256f630d1f87.png align="center")

Attach the Internet Gateway to your VPC.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717210030148/a1a24053-7957-4e2c-9eea-0d7fa547c839.png align="left")

Now, we have attached an internet gateway to our VPC.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717299469434/eaa3ec5e-e0f5-4290-b92e-61108385ba57.png align="center")

---

### Task 3: Creating Subnets

On **Subnets**, click on `Create subnet`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717210167646/ab1ee8f4-61bc-4600-ab80-d2bfb26d446a.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717210202941/db8219be-c71f-43a7-b986-1caa2904b668.png align="left")

Select your VPC.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717210249053/0071dd31-ac69-4a8f-94b9-7e4c96312cce.png align="left")

Click on `Add new subnet` to create another subnet.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717210308782/e447fea6-6ccb-4f58-a404-d32cc6050d8a.png align="left")

Click on `Create subnet`.

We have created two subnets in different AZs.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717300036436/2416ce6d-7b18-4117-96fd-60f4b120a7b1.png align="center")

---

### Task 4: Creating Route table

On **Route tables**, click on `Create route table`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717256048988/7f72e6ef-2d15-4153-9441-5f9721732b28.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717256116882/bef1ce22-a123-4c42-940b-54266a76f16e.png align="center")

Provide name for your route table, select your VPC and click on `Create route table`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717256196411/c066c170-da8b-49a1-9ac4-9211c239f77b.png align="center")

On **Subnet associations,** click on `Edit subnet associations`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717256229837/15af3322-984d-46ac-b82d-6e16136a6395.png align="center")

Tick both of your subnets and click on `Save associations`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717256295615/821f7fc4-9968-4c6e-a6a5-8f49f3c2a350.png align="center")

On **Routes,** click on `Edit routes`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717256352593/2606ef93-96a9-4b6e-875c-ff82bfe729a5.png align="center")

Click on `Add route` and configure above configurations.

Click on `Save changes`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717301582449/0c272f74-92ec-4fac-b8e3-db2bc4d4cab5.png align="center")

---

### Task 5: Creating Launch template

Previously, launching EC2 instances involved manually configuring every detail for each launch. Launch templates simplify this process by offering a reusable definition - essentially a blueprint - that stores settings such as AMI, instance type, security groups, and more. This streamlines instance creation, enables straightforward configuration versioning, and maintains consistency across your deployments.

On **EC2 dashboard**, **on Launch Templates**, click on `Create launch template`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717207899386/bf3b48b2-cd03-42d3-bc00-5d03fac9b071.png align="left")

Provide a suitable name for your template.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717208621175/7e2d09d4-295d-4cdb-88d1-9660e8729b0c.png align="center")

On **Quick Start**, choose Ubuntu as an AMI.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717208734807/3e843fbe-64ff-4d29-ae57-1d0e23c4dc71.png align="center")

Choose instance type and either create or select your existing key pair.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717286778901/6148c882-81ea-46e8-91a6-a45cdd6cc394.png align="left")

In the **Network settings**, subnets are not selected. Instead, a Security Group is created for the instances. Select your VPC and add the inbound rules to your Security Group.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717257887623/df3b1943-b757-43f1-9634-d207c1333e5b.png align="center")

In the **Advanced network configuration**, click on `Add network interface` and `Enable` the Auto-assign public IP option.

With all other configurations left as default, click on `Create launch template`.

---

### Task 6: Creating Auto Scaling Group

On **EC2 dashboard**, scroll to bottom on Auto Scaling Groups.

Click on `Create Auto Scaling group`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717210908448/6f81e755-9d6e-4efc-bc56-41df763d1db1.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717210990649/2ccf7021-f8a2-426c-bf65-3b9fd0c8f50c.png align="center")

Name your Auto Scaling Group and select the Launch template you created.

Click on `Next`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717211089128/23005314-e3de-4c4e-8ff9-90e2f8fc19e5.png align="left")

Select your VPC and choose your subnets.

Click on `Next`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717211154956/aa03b683-9cc6-413c-bd3a-74ed9dab07c1.png align="center")

For this project, we are not going to associate a load balancer.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717211265566/67b282d2-515d-45ef-8a4e-1dc7fd71aa06.png align="center")

The Health Check Grace Period gives new instances time to start up and become healthy before health checks start. This prevents instances from being incorrectly marked as unhealthy and terminated during startup. Setting a grace period allows instances to boot up, configure themselves, and start applications, leading to more stable scaling.

*Determines how long to wait before starting health checks on a new instance.*

With all other configurations set to default, click on `Next`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717211856620/e4c74fd7-c681-4dea-95ac-d5160c6143d6.png align="left")

In normal operations, the Auto Scaling group keeps 2 instances running (**Desired capacity**). It can scale up to 3 instances in high traffic (**Max desired capacity**) but never goes below 1 instance in low traffic (**Min desired capacity**).

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717212328854/6efeb4e8-2e48-4fd5-9664-c1689d273f63.png align="left")

Scaling policies determine how the group responds to changes in demand. These policies are based on metrics like CPU utilization, network traffic, or custom CloudWatch alarms.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717213305239/2029ebf7-5c94-4b6d-9289-9b9824d9efbd.png align="center")

By setting up these policies, we can ensure our application always has enough resources to handle traffic spikes, while also optimizing costs during quieter periods.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717258904315/eed34c51-433b-4396-ba40-9309be7fe1d1.png align="left")

The Target Tracking Scaling Policy in AWS Auto Scaling automatically adjusts the number of instances in a group **to maintain a specific metric at a target value**.

Instance warm-up refers to time before a new instance is included in scaling activities. It ensures the instance is fully operational before being counted.

*Determines how long to wait before including a new instance in scaling decisions.*

Our scaling policy adjusts the number of instances to maintain a 50% average CPU utilization. During high traffic, it adds instances to handle the load and maintains performance. During low traffic, it reduces instances to the minimum required, prioritizing cost optimization over meeting the 50% target.

Click on `Next`, keeping all other configurations default until you reach the option to `Create Auto Scaling group`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717242978275/f83377d7-bd40-40f7-8b65-808ef23ce97c.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717301496352/a264ea05-2fe4-4f7b-9fb0-3838a7831c09.png align="center")

---

### Task 7: Exploring Auto Scaling groups

Let's perform various actions on our Auto Scaling Groups.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717245499403/69d94995-24bf-4d53-841c-90891f200308.png align="center")

Select your Auto Scaling group and click on the **Activity** section.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717287960416/0143ff1f-9071-4a3d-8ef8-54a214e12249.png align="center")

Our Auto Scaling Group is currently launching instances.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717288032514/e8e7ca10-8787-4c57-8a99-5112a95f68d6.png align="center")

The instances for our desired capacity have been launched.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717288079606/48e97823-2937-4a44-ae2d-3e798de38484.png align="center")

We can see the instances in the Instances console.

Now, let's terminate one of the EC2 instances.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717288141187/b16c5dd0-eb3e-4a4d-a2ec-dd89ee054941.png align="center")

Let's head up to the Auto Scaling Group's Activity section to see what happens next.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717288326785/bef15163-7774-4132-a854-47e3d76c2c06.png align="center")

We can see our terminating instance while the Auto Scaling Group (ASG) is launching a new instance to reach our desired capacity.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717288422672/c28ebed3-194f-46af-a966-7abbadb27ec2.png align="center")

In the meantime, an Auto Scaling Group terminated one EC2 instance. As there is currently no traffic, the ASG is operating efficiently by keeping only the minimum required resources running.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717288608075/3574f38c-9a2a-402c-b857-21f630fafb4c.png align="center")

Now, click on the ID of your running instance, then navigate to the **Monitoring section**.

Enlarge the CPU utilization metric.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717260954272/8222954e-4465-4ce0-86eb-e284da802d2d.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717290038574/56268c3b-a063-4f47-bb73-95fe12b61e98.png align="center")

Let's increase the CPU utilization.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717261040080/a79f2ff7-df15-4fb8-b10d-76c4f75d3196.png align="center")

On your instance details, click on `Connect`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717290168642/e67e3f03-f2ef-4098-a7de-39b466c58b0a.png align="left")

On **EC2 Instance Connect**, click on `Connect`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717290266657/d00d3d38-ef4a-4f83-9b96-4e192d8ec887.png align="center")

We're now connected to our instance.

Copy the command and paste into your terminal.

```bash
sudo apt-get update # updating package lists
sudo apt-get install stress -y # installing stress
```

* stress utility is used to simulate high CPU usage scenarios for testing and benchmarking purposes.
    

And now it's time to spike the CPU utilization.

```bash
# Stress the CPU cores for 5 minutes
stress --cpu $(nproc) --timeout 300
```

This command stresses all available CPU cores for 5 minutes using the `stress` tool. `$(nproc)` dynamically determines the number of CPU cores.

Let's check the CPU utilization metric of our instance.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717292729808/6aaea258-5320-41ce-89b7-2a9398f5249d.png align="left")

We can observe that the CPU utilization is increasing.

Now, let's check the Auto Scaling Group's Activity section to see if there are any changes.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717292750255/0123e4a7-b23c-4bef-8640-062ef3d8f709.png align="left")

We can see that the Auto Scaling Group (ASG) has launched an instance in response to the increasing load.

As the CPU utilization continues to rise, as we have configured it to spike for 5 minutes, another instance is being launched by the Auto Scaling Group (ASG), and now we've reached our maximum EC2 limit as set by us.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717292764746/1ac88f68-94cd-4bb9-87e9-3bc22b461b2e.png align="left")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717292942116/1ebdd45a-1a96-43b5-99d0-2856dcd08b24.png align="center")

Let's go to the Instances list.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717292855956/225eaccb-4041-4930-8f73-f7605db26734.png align="center")

We can see that 2 additional instances have been launched by ASG as the CPU utilization metric crossed the threshold that we had set.

And when the CPU utilization is low, a scale-in event will occur.

That's the beauty of Auto Scaling.

---

### Conclusion

We created an Auto Scaling Group with a minimum capacity of one instance, a desired capacity of two instances, and a maximum capacity of three instances. We observed both scale-in and scale-out events during the testing process. To stimulate CPU utilization, we used a stress utility tool.

---

### Tasks 8: Clean Up

1. **Deleting Auto Scaling Group**
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717293818975/8a0a1b3d-728e-4a72-b1d4-7f3c0e24b5f2.png align="center")
    
    Select your Auto Scaling Group, and in the **Actions panel**, click on `Delete`.
    
2. **Deleting Launch Template**
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717293889155/a4c020b8-028b-4956-a51d-a8e757c96a3b.png align="center")
    
    Select your Launch Template and in **Actions panel**, click on `Delete template`.
    
3. **Deleting Subnet**
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717294027713/86b72cd4-e382-467f-ab65-8dd84b83ef50.png align="center")
    
    Go to the VPC Dashboard. In the **Subnets** section, select both of your subnets, and in the **Actions panel**, click on `Delete subnet`.
    
4. **Deleting Route Table**
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717294114664/b802109f-f6a8-4cab-974f-766ba0667613.png align="center")
    
    Select your route table, and in the **Actions panel**, click on `Delete route table`. If you're unable to do so, try refreshing the box.
    
5. **Deleting Internet Gateway & VPC**
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717294236374/2a6becb4-33db-437c-85ba-916d01e1ba64.png align="center")
    
    Select your VPC and in the **Actions** panel, click on `Delete VPC`.
    
    Deleting the VPC will also delete the internet gateway associated with it.
    

We have now cleaned up our used resources.

---

Alright! See you next time!
