DEV Community

Israel Parra
Israel Parra

Posted on

AWS Elastic Load Balancer and Auto Scaling Groups

AWS Elastic Load Balancer and Auto Scaling Groups

Amazon Web Services tutorial — ELB and ASG

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

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.

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

  • 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 "

    Hello from $(hostname -f)

    " > /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:

Instance 1:

Instance 2:

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)

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

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

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

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.

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.

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”.

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

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

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

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.

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

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

once selected click on include as pending

then click on create.

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

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

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

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

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.

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.

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”.

Doing that you will see the following page:

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.

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):

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

Once you have the configurations make click on create launch template

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

click on next.

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

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.

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:

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

Then click on create.

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.

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:

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.

Top comments (0)