Pre-requisite

To understand this blog, you should have basic knowledge of AWS EC2 and Horizontal Scaling.

You can study EC2 from here.
You can study Horizontal Scaling from here.

Content of this blog

  1. What is Auto Scaling and Why do we need it?
  2. Amazon Machine Images (AMIs)
  3. Launch Template
  4. Create Target Group and Load Balancer
  5. Creating Auto Scaling Group
  6. Cleanup all resources

We will do everything with the hands-on tutorial on AWS Console.

What is Auto Scaling and Why do we need it?

Suppose you started a business and made it online. You rented an EC2 server to deploy your application. If a lot of users come to your website at the same time, then your website may crash because the EC2 server has limitations (CPU, RAM, etc) on serving a certain number of users concurrently at one time.

At this time, you will do horizontal scaling and increase the number of EC2 instances and attach a load balancer. We have discussed everything about this in our previous blog.

Suppose one EC2 machine can serve 1000 users without choking. If the number of users on our website is not constant. Some days, we have 10,000 users on our website, which can be served by 10 instances. Some days, we have 100,000 users then we need 100 EC2 instances. One solution might be to keep running the maximum number (100) of EC2 instances all the time. In this way, we can serve all users at all times without any problem. But during low-traffic periods, we are wasting our money in extra instances. We only need 10 instances, but we are running 100 instances all the time and paying for it.

The best solution for this is to run only the required number of instances every time. And add some sort of mechanism that if CPU usage of an EC2 instance goes up to a certain threshold (say 90%) then launch another instance and distribute the traffic without us manually doing this. This changing number of servers dynamically based on the traffic is called Auto Scaling.

Note: These numbers are all hypothetical to make you understand the topic. If you want to find the actual threshold, then you can do Load Testing on your instance. I will write a separate blog in future on Load Testing.

In this blog, we will deploy our application in an auto-scaling environment.

Before studying auto-scaling groups in AWS, we need these two things:
1. AMIs
2. Launch Template
Because these things are required to create auto-scaling groups.

Amazon Machine Images (AMIs)

Amazon Machine Images (AMIs) are templates for creating EC2 instances on AWS.

Remember, when we launched our EC2 instance and then selected Ubuntu Image. See the below image.

This gives us an EC2 instance where Ubuntu is configured by default and we will be installing our application on top of Ubuntu server.

Similarly, we can make our custom image where we have Ubuntu + NodeJS + NPM + Application code + File System + PM2 etc already configured. When we launch our instance from this image, we don’t have to configure all these.
If pm2 is a new word for you, then please refer to my previous blog.

Below is the AMI that we will create:

Our application code is this: https://github.com/shivam-bhadani/Simple-Nodejs-Server

Tutorial on Creating AMI

  1. Launch an EC2 instance. Do SSH and install nodejs, npm, pm2 and application code.
    Below are the commands to do this.
sudo apt update

sudo apt install nodejs

sudo apt install npm

sudo npm install -g pm2

git clone https://github.com/shivam-bhadani/Simple-Nodejs-Server.git

cd Simple-Nodejs-Server

npm install

I created an instance called “shivam-ec2” and ran the above commands.

2. Click on Actions > Images and templates > Create image.

3. Give the name of AMI (I have given shivam-ami). You can also attach an EBS volume if you want. We are going with the default EBS then click on “Create Image” button.

4. You can go to the AMI section under the Image on the left sidebar and check your AMIs.

We have successfully created our AMI. You can launch your instance from this AMI, and you will get npm, nodejs, code, etc, installed.

Launch Template

A launch template in AWS is a blueprint that defines the configuration for launching EC2 instances.

While launching an EC2 instance from an AMI, we still need to configure the security group, key pair (for SSH), EBS volumes, etc. AMI does not have an instance-level blueprint. It just contains the things that are inside the instance, such as the application code, file folder, etc.

In the launch template, we can configure instance-level things such as Security Group, Key-pair (SSH), EBS Volume, Instance type (t2.micro) etc. It will also contain AMI.

So, if we launch an instance from Launch Template, we don’t have to do anything because Launch Template has all the configurations required to launch the EC2 instance. We can also write a starter script while creating the launch template, and this script will run automatically just after our instance starts. In our case, we need to run pm2 start app.js .

Below is the Launch Template that we will create:

Tutorial on Creating Launch Template

  1. Create a security group that we will attach in our instances.
    Go to the security group in the left sidebar and click on the “Create security group” button.

2. Give the name of the security group (I have given shivam-security-grp) and allow SSH from anywhere in the in-bound rule. Then click on “Create security group”.

You can see the new security group that we have just created.

2. Go to Launch Template in the left sidebar and click on “Create Launch Template”.

3. Give any name and description to the launch template (I have given shivam-launch-template).

4. Scroll down and select the AMI that we have created.

5. Scroll down and select the Instance Type (t2.micro) and key-pair for SSH.

6. Scroll down and select the security group (shivam-security-grp) that we have created in step 1.

7. Very Important step: Open the “Additional Setting” and, under “User data”, give the starter script that you want to run just after the instance launch.

For our case, we want to navigate to our application code folder and run pm2 start app.js . When we create an instance from AMI, our application resides in the/home/ubuntu folder.

Below is the “User data”.

#!/bin/bash

cd /home/ubuntu/Simple-Nodejs-Server
pm2 start app.js

Whatever you write inside user data will run just after the instance gets launched.

After that, click on “Create launch template” button.

8. Finally, our launch template is created. You can also launch an instance from this launch template to check if everything is working fine.

Create Target Group and Load Balancer

If you don’t know what the target group and load balancer are, then please refer to my previous blog.

Follow the exact same steps of the previous blog to create target group and load balancer and give port 8080 to the target group because our nodejs application is running on port 8080.

  1. Below is the target group that I have created. The name is “shivam-target-group”, and it is listening on port 8080.

Now, I will attach this target group to the load balancer, but before creating ALB, we need to create a security group for it.

2. Create a Security Group for the application load balancer. In this security group, allow HTTP traffic from anywhere option.
In this way, anyone can hit our load balancer DNS to access the website.
We don’t want people to directly hit our instance’s IP address, so we haven’t opened the Port 8080 of the instance’s security group.

Below is the screenshot. I named this security group as “shivam-alb-security-grp”. In the inbound rule, I enabled HTTP and custom port 8080 to “anywhere ipv4”

3. Copy the security group of ALB (shivam-alb-security-grp) and add this to the inbound rule of the instance’s security group (shivam-security-grp) by making Custom TCP 8080 to this alb security group. In this way, our load balancer can only access the instance, and users can only access the load balancer. This is what we want. We already did this in our previous blog, so I guess you know this stuff already. If you don’t know, then please read steps 16 to 20 in the previous blog.

From the above thing, users will not be able to directly access the instance. They can only access the load balancer, and the load balancer can access the instance.

4. Finally, create the application load balancer, attach “shivam-alb-security-grp” and “shivam-target-group” to it and name it “shivam-alb”.

Creating Auto Scaling Group (ASG)

Finally, we came to the stage where we could create ASG.
We created a launch template. ASG will use this launch template to automatically create new instances. We also created the target group. It will also used by ASG to attach to the newly created instances.

  1. Go to the Auto Scaling Groups on the left sidebar and click on “Create Auto Scaling group” button.

2. Give name to this auto-scaling group (I have given shivam-ASG). Select the launch template that we have created (shivam-launch-template). Then click on Next.

3. Select the availability zones in which you want your new instance to be created. I selected all availability zones of ap-south-1. Then click on Next.

4. Select “Attach to an Exisiting Load Balancer” then choose the target group (shivam-target-group) that we have created. Whenever ASG will create a new instance, it will attach shivam-target-group to it.
Then click on Next.

5. Keep everything default. We will not attach any scaling policy for now. We will do it later.

6. Don’t do anything. Click on Next, then Next, then Create Auto Scaling group button.

7. Our ASG is ready.

One instance is already running because we gave a minimum of 1 instance running all the time. See in step 5.

You can hit the DNS of the Load balancer on the browser and check that the site is running.

You can see, we haven’t done anything. ASG has automatically created this instance for us.

8. We will add a dynamic auto-scaling policy.

Click on shivam-ASG. Then click on “Edit” button of Group details.

Choose your numbers. I have set the minimum no. of instances as 1 and the maximum as 5. This means if a lot of traffic comes, then it will spin up 5 instances, and when the traffic is very low, then we will only have 1 instance.

Then click on Update.

9. Go to Automatic scaling tab then click on “create dynamic scaling policy”.

10. Give your metric. I have given that if average CPU utilization goes more than 60% then spin-up new machine. Then click on Create button.

Congratulation!! Our auto-scaling group is completed.

Try doing load testing. Make a lot of fake API calls on your server simultaneously (with the help of some script) and see if the number of instances scales up and down with your own eyes.

Cleanup all resources

We will delete/ terminate all the resources which we have created in this tutorial so you will not get any bill from AWS.

  1. Make group size of this auto-scaling group 0 for minimum, maximum and desired all. Go to Edit Group Detail as shown in step 8 and set everything 0. This will terminate all the instances created by this ASG.

2. Delete the Auto Scaling Group.

3. Delete Launch Template.

4. Delete the Load Balancer.

5. Delete the target group.

6. Terminate all the instances which are in the running or stopped state.

7. Deregister AMIs.

Post a Comment

Previous Post Next Post