AWS Elastic Load Balancer and Auto Scaling Groups
Tags: aws, cloud
Published: 2023-06-28 04:42:29


## AWS Elastic Load Balancer and Auto Scaling Groups

### Amazon Web Services tutorial — ELB and ASG

![](https://cdn-images-1.medium.com/max/3748/1*CnUYtKWpanSAszLmjbKTRw.png)

In this tutorial, we will review two essential AWS services that help us with our application scalability, ELB (elastic load balancer) and ASG (Auto scaling groups).
>  Note: If you are new with the scaling concept I recommend you take a look at this article where I talk about scalability — [https://medium.com/dev-genius/vertical-scaling-vs-horizontal-scaling-cfad0ff2d7bf](https://medium.com/dev-genius/vertical-scaling-vs-horizontal-scaling-cfad0ff2d7bf)

### Introduction

First, let us talk about what a load balancer is.

*“A load balancer is a device or software that distributes network traffic among multiple servers or computing resources to improve the performance, scalability, availability, and reliability of an online service or application”.*

A load balancer acts as an intermediary between clients and servers and uses routing algorithms to distribute network traffic across multiple servers or application instances, depending on the number of requests that the service is receiving.

Imagine the following scenario. You have an e-commerce application and you detect the dates with more traffic (could be black Friday for example), then you and your team decide to generate multiples instances of your application in different servers, and the next step, how are we going to work with those deployments as one? the answer is, implementing a load balancer to distribute the request along the different instances.

Cool, now we understand what is a load balancer and when we could implement it, but how we can implement a load balancer?, there are a lot of services, frameworks, and approaches to work with a load balancer, for example, if we are using a cloud like AWS we are able to use a service named ELB which is one that we going to use in this tutorial.

### What is ELB?

ELB stands for “Elastic Load Balancer”, which is an AWS cloud service that helps us to implement a load balancer in our solutions. Elastic Load Balancing (ELB) automatically distributes incoming application traffic across multiple targets and virtual appliances in one or more Availability Zones.

AWS offers three different types of load balancers:

 1. Application Load Balancer

 2. Gateway Load Balancer

 3. Network Load Balancer

In this tutorial, we going to focus on the Application load balancer option. In the following image you can see how it works from a general perspective:

 1. Your client makes a request to your application.

 2. The listeners in your load balancer receive requests matching the protocol and port that you configure.

 3. The receiving listener evaluates the incoming request against the rules you specify, and if applicable, routes the request to the appropriate target group. You can use an HTTPS listener to offload the work of TLS encryption and decryption to your load balancer.

 4. Healthy targets in one or more target groups receive traffic based on the load-balancing algorithm, and the routing rules you specify in the listener.

![](https://cdn-images-1.medium.com/max/2000/1*bqmbPwys3LN4i3EHpDZ8Bg.png)

Hands-on!… and let start with an example to learn how to use ELB

For this tutorial we are going to need:

Two EC2 instances (configured with internet access).
 *— If you are new and want to learn how to make this step, you can follow this tutorial [https://medium.com/@josueparra2892/tutorial-elastic-compute-cloud-ec2-1d20dd8d64a1](https://medium.com/@josueparra2892/tutorial-elastic-compute-cloud-ec2-1d20dd8d64a1)*

* Configure an ELB to distribute the traffic between the EC2 instances.

### **Creating the EC2 instances**

At the time to create your instance, you should use the following configurations:

* The instance type could be t2.micr .

* AMI: Amazon Linux

* Your instance should have internet access.

* As part of the Advanced details in the field of user data . You need to use add the following lines. 
The configuration above will help us to create a basic service that displays the instance IP address.

    #!/bin/bash
    # Utiliza esto para tus datos de usuario
    # Instala httpd (Version: Linux 2)
    sudo su
    yum update -y
    yum install -y httpd
    systemctl start httpd
    systemctl enable httpd
    echo "<h1>Hello from $(hostname -f)</h1>" > /var/www/html/index.html

Note: you should create 2 instances for this tutorial, but in a real-world application you can define as much as you need.

Example:

![](https://cdn-images-1.medium.com/max/5928/1*AGXnIjNgVWpJyk_H-p-reg.png)

Instance 1:

![](https://cdn-images-1.medium.com/max/2680/1*FrqYQIGolbjrALgwfy7wAg.png)

Instance 2:

![](https://cdn-images-1.medium.com/max/2468/1*ZinPoxo1Fw_HRQXJO1GsOg.png)

### Important considerations at a time to build our ELB

The following points are very important to make the ELB works correctly

* The VPC should be the same in your instances and in your ELB configuration

* You need at least 2 availability zones, each one with its subnets with internet access

* Your instances should be configured in the same VPC

### Configure ELB (Elastic Load Balancing)

![](https://cdn-images-1.medium.com/max/2000/0*Ovn05YAi3fV2_f2C.png)

To do this step you first have to navigate to the “load balancing” menu in the left panel inside of the EC2 dashboard.

![](https://cdn-images-1.medium.com/max/2000/1*3xk0iGeK1KxzPa-GzVQgxw.png)

Then make click on “load Balancer”, it going to take you to the following screen:

![](https://cdn-images-1.medium.com/max/6104/1*qpQXP0WWQdcACjNGqTeuLg.png)

Once there you should click on create “load balancer”, then you going to see something like the following:

![](https://cdn-images-1.medium.com/max/4256/1*w2jNPwyjkoVDpT7VIV42Dw.png)

As I mentioned before we going to review in this tutorial the option of “Application Load Balancer”, it will help us to distribute the request using the ELB between the EC2 instances.

Then you need to click on create of the first option “Application Load Balancer”.

After you clicked you will see a configuration page, there we going to configure all the needed parameters for our load balancer.

The first step is to provide a name, and we going to keep the internet access and the IPv4 options selected, like in the image below.

![](https://cdn-images-1.medium.com/max/3352/1*REQwtfISS1Tfs_X6mevEbA.png)

The next step is to configure the network mapping.

Here we going to configure the VPC and the availability zones.

For the VPC you can select one of the ones that you already have configured, and then select the AZ and the subnet related to it.

![](https://cdn-images-1.medium.com/max/3488/1*UtlbBRfjTJ9qrPCsXYP1Lw.png)

![](https://cdn-images-1.medium.com/max/3264/1*ImXCdc7l4OlT0rh2eMazjw.png)

The next step is to configure the security group, in this case, we going to see that is one selected by default, but we need to create a new one used only by our ALB (Application Load Balancer). To do that you need to make click on “Create new security group”.

![](https://cdn-images-1.medium.com/max/3256/1*kBbZxBbl7kxyloZUupnoxQ.png)

![](https://cdn-images-1.medium.com/max/3552/1*zCyuuTlnzEYg9Pdn9YeuvQ.png)

we need to add Inbound rules, you can take as example the image below.

![](https://cdn-images-1.medium.com/max/3480/1*iClRXGehZWufG02MamjN5Q.png)

Ready your security group configurations are done just click on “Create security group”

![](https://cdn-images-1.medium.com/max/3060/1*21pU6ESGCfBXttrgk7AsbQ.png)

Go back to the load balancer configuration page and choose your new security group as part of its configurations.

![](https://cdn-images-1.medium.com/max/3386/1*PVj9Wn2eyiNMEE8SZcFdjw.png)

![](https://cdn-images-1.medium.com/max/3328/1*vd6EMZxfVq25UrHwVfU3Fg.png)

Then the following step is one of the most important because there we going to configure all the instances that will be used by our load balancer.

For the “Listener and routing” configuration we going to follow a couple of steps.

![](https://cdn-images-1.medium.com/max/3266/1*fc8f3OlRhlACMc6IZM3LsA.png)

The first one is to create a new “target group”, configured for instances, you can take as a reference the images below:

![](https://cdn-images-1.medium.com/max/3340/1*kPPyY7pbfMuWMm6eqR-Siw.png)

![](https://cdn-images-1.medium.com/max/2538/1*ISenxRBhaHVRtM6ft0aGaw.png)

As you can see we only modified those values, then you can click on “next” to proceed to add the instances

![](https://cdn-images-1.medium.com/max/3290/1*VArDTxtYrw7SCnfP87vbmg.png)

once selected click on include as pending

![](https://cdn-images-1.medium.com/max/3334/1*WOyMl25GYGHhJVs6pBkYYQ.png)

then click on create.

![](https://cdn-images-1.medium.com/max/2938/1*33dbNsJJH2xutmWiYrkIYw.png)

Once configured the target group, you should go back to the load balancer configuration page and select the created target group.

![](https://cdn-images-1.medium.com/max/3318/1*9zuiN-ZztHLckmLImK-mOA.png)

Scroll down to see the summary, and then click on create.

![](https://cdn-images-1.medium.com/max/3764/1*aIKvY_5PocTRZZwTYFMYkw.png)

![](https://cdn-images-1.medium.com/max/2978/1*yAGsn1lS5kR0QnXvfiatGg.png)

To test our Load balancer we need to use the DNS name that comes on the load balancer details

![](https://cdn-images-1.medium.com/max/2966/1*z4cE1KLfTIdVnfpcQo-wtg.png)

Copy and paste in a browser and then EC2 instance should response

![](https://cdn-images-1.medium.com/max/2216/1*zLUmqP0QPV0q3GVcq-PjVA.png)

Maybe the result will the same many times but if you test doing the request with some tool to help you to get the limit of request the load balancer will make its work and distribute the request.

Pretty easy right ?. Now you know how to configure manually a load balancer and how to configure the instances that it will run, maybe you are wondering, but, what happens if we don’t want to create or delete the instances and make it automatically?, the answer is, use an Auto Scaling Group (ASG), that will help you to create instances as the demand needs and also it will automatically decrease the number of instances.

In the next section of this tutorial, we going to see how to use and configure an Auto Scaling group.

![](https://cdn-images-1.medium.com/max/2000/0*TYhNdrSe3cBocmMk.png)

ASG stands for “Auto Scaling Group”, and is a service that helps us to scale our application automatically monitoring our EC2 containers and increasing or decreasing the instances as needed based on the traffic and request on your instances.

![](https://cdn-images-1.medium.com/max/2000/1*U3fOHiHTjEIoi7gC2CwTLA.png)

### **Benefits and features**

* **Fault tolerance**:
Detect when an instance is unhealthy, terminate it, and launch an instance to replace it.

* **Availability:
**Ensure that your application always has the right amount of capacity to handle the current traffic demand.

* **Cost management:
**Save money by dynamically launching instances when they are needed and terminating them when they aren’t.

One example could be the following. If an application experiences a sudden increase in workload, Amazon Auto Scaling can automatically add more computing resources to ensure that the application continues to function smoothly. Similarly, if the workload decreases, Amazon Auto Scaling can automatically reduce the number of computing resources to avoid unnecessary costs.

Knowing that is time to build our ASG and see it in action.

### Configuring ASG

The first step to configure a ASG is to go to the ASG dashboard, you can access making click in the “Auto Scaling Groups” in the left menu. And then select “create auto scaling group”.

![](https://cdn-images-1.medium.com/max/2000/1*K7kgf8c6pggYWnQLLVXhMw.png)

Doing that you will see the following page:

![](https://cdn-images-1.medium.com/max/4812/1*Dta7OLnBLT0mDIf0ektPrw.png)

Here we going to start with the configurations. As you can see there are 7 steps to follow, and I will guide you on each one of them.

**Step 1)**

Here we need to provide a name and create the launch template for our instances.

![](https://cdn-images-1.medium.com/max/4560/1*OuReZOUyN8oJRWy6qGLzbA.png)

In order to create the template you should make click on “Create launch template”

There you can take as a base the following configurations (just focus in the edited sections for this tutorial):

![](https://cdn-images-1.medium.com/max/3300/1*VeM6fr6_RkWgdXtZNN94Hg.png)

![](https://cdn-images-1.medium.com/max/3272/1*SjliMwKhDXpsuicQ3mjBdg.png)

![](https://cdn-images-1.medium.com/max/2250/1*A6OgE8l1vz1WmnKtawfyQw.png)

For the network settings you need to provide a valid security group. You could use the same that we used in the previous sections.

![](https://cdn-images-1.medium.com/max/2178/1*A4j1-9y5o2HxXjOVXLR3vA.png)

![](https://cdn-images-1.medium.com/max/2192/1*sO_sV-VJeU1VV7H5hYqXqw.png)

![](https://cdn-images-1.medium.com/max/2000/1*c9bBr4Hu6-Oc-jic6112Yw.png)

Once you have the configurations make click on create launch template

![](https://cdn-images-1.medium.com/max/3388/1*zxU44tSRTrk6CAPXSdh_vA.png)

Then go back to the ASG view, there you need to choose the created template

![](https://cdn-images-1.medium.com/max/2382/1*xrdh_nI99k_-2FaQkk0QvA.png)

click on next.

select your VPC and at least 2 subnets with internet access. here you can use the configured in the previous section.

![](https://cdn-images-1.medium.com/max/2344/1*pktgv2WanTznafesRGC6XA.png)

Click on next.

In this step you need to specify which load balancer the ASG will use, then you can select the one that we created in the previous sections.

![](https://cdn-images-1.medium.com/max/2434/1*imBXL90TvpAPYLpj3n-8mw.png)

![](https://cdn-images-1.medium.com/max/2400/1*-udmGjT5bofp6chmhLouKA.png)

![](https://cdn-images-1.medium.com/max/2416/1*r9oh_zCw8SYeWmRyQ54Dxg.png)

Click on next.

The fourth step is to configure the ASG size, defining the number of instances to use.

You can use the following configurations:

![](https://cdn-images-1.medium.com/max/2426/1*k7KFV-54xoX8tivCkorDtg.png)

![](https://cdn-images-1.medium.com/max/2454/1*tU0FEQLALtBk8_5UmbtuHw.png)

Click on next until step 7. we going to omit the others configurations in this tutorial.

Then click on create.

![](https://cdn-images-1.medium.com/max/3618/1*jXUluyoWfQGwsD35RrXfTA.png)

As you can see we have a new ASG created.

If you go to the EC2 instances you also will see 2 instances created by default and automatically.

![](https://cdn-images-1.medium.com/max/3076/1*EebKh_s8jYYUax4tulTMYQ.png)

Wait a bit until the instances are running, in order to test the results. We need to go to our Load Balancer and get the DNS to make the request in a browser:

![](https://cdn-images-1.medium.com/max/2000/1*kTn2ooyDqbZQYqFl0PPeEQ.png)

At time to load the page you will see in the screen the IP related to the instance that is getting your request.

Ready as you can see we now you know how to start configuring a load balancer for your system, and make an auto scaling group to make the scaling automatically.

I recommend to you to use the ASG because it will help you to manage the instances adding or removing them as needed.

![](https://cdn-images-1.medium.com/max/2000/0*dEKIv46P7fq8pG45.png)