Table of Contents
In the previous tutorial, we created an Auto Scaling Group and applied Dynamic Scaling Policy to it. Note that we used an AWS Application Load Balancer while configuring our Auto Scaling Group but we didn't get into detail about that. In this tutorial, we will learn about AWS Application Load Balancer and will configure one from scratch. Before getting our hands dirty first we will see that what an Application Load Balancer is and how it works.
AWS Application Load Balancer
According to what is mentioned on the official website of AWS,
The Application Load Balancer distributes incoming HTTP and HTTPS traffic across multiple targets such as Amazon EC2 instances, microservices, and containers, based on request attributes. When the load balancer receives a connection request, it evaluates the listener rules in priority order to determine which rule to apply, and if applicable, it selects a target from the target group for the rule action.
How Application Load Balancer works
When a client makes a request to our application, the listeners in our AWS Application Load Balancer will receive requests matching the protocol and port we configure. The receiving listener will evaluate the incoming request against the rules we specify, and if applicable, will route the request to the appropriate target group. We can use an HTTPS listener to offload the work of TLS encryption and decryption to our load balancer. Healthy targets in one or more target groups receive traffic based on the load balancing algorithm, and the routing rules we specify in the listener.
More details can be found at the official website of AWS. Now let the fun begin!
Create an EC2 instance
In order to create a load balancer, we need to have EC2 instances. First, we will log in to our AWS console and then type EC2 in our Services tab. From the left pane, we will click on instances. Currently, we don't have any instances.
Now we will click on Launch instances. The creation of an EC2 instance involves seven steps. We will see each of them one by one.
Step-1: Choose AMI
An Amazon Machine Image is a template that contains the software configuration (operating system, application server, and applications) required to launch our instance. We have different options to choose from AMI either by AWS, user community, or the AWS Marketplace; or we can select one of our own AMIs. For now, we will select Amazon Linux 2 AMI (HVM) - Kernel 5.10, SSD Volume Type. Note that it's free tier eligible.
Step-2: Choose Instance Type
Amazon EC2 provides us with a wide selection of instance types optimized to fit different use cases so we can choose one according to our requirements. Instances are virtual servers that can run applications and have varying combinations of CPU, memory, storage, and networking capacity. For now, we will select t2.micro that is free tier eligible.
Step-3: Configure Instance
Here we will configure the instance according to the requirements of the application. Through this, we can launch multiple instances from the same AMI, request Spot instances to take advantage of the lower pricing, assign an access management role to the instance, assign a VPC in which the instances will reside, and more. For now, we will keep all the configurations to default except for User Data under Advanced Details. We will set User Data to the following.
#!/bin/bash yum update -y yum install -y httpd systemctl start httpd systemctl enable httpd echo "<h1> Hello World from $(hostname -f)<a/h1>" > /var/www/html/index.html
Step-4: Add Storage
Here we will define storage device settings for our EC2 instance. We can attach additional EBS volumes that are durable, block-level storage devices that we can attach to your instances and instance store volumes that are ideal for temporary storage to our instance, or edit the settings of the root volume. For now, we will keep the configurations to default.
Step-5: Add Tags
A tag consists of a case-sensitive key-value pair. Here we will define a tag with key Name and value My First EC2 Instance.
Step-6: Configure Security Group
Here we can create a new Security Group that is a set of firewall rules that control the traffic for your instance or select from an existing one below. We can add rules to allow specific traffic to reach our instance according to the requirements of the application. For example, if we want to set up a web server and allow Internet traffic to reach our instance, we will add rules that allow unrestricted access to the HTTP and HTTPS ports. Here we have created a new Security Group launch-wizard-1 and allowed unrestricted access to HTTP ports only.
Step-7: Review your configuration
Here we will review the instance launch details we configured earlier. We can also go back to edit changes for each step if needed.
Now we will click Launch to assign a key pair to our instance and complete the launch process. Note that we can either create a new key pair or use an existing one. Here we have used an existing key pair EC2 Tutorial.
Now we will repeat the same steps for two more EC2 instances. After a few minutes, we can see that all our three instances are in the Running state.
Create an AWS Application Load Balancer
From the left pane under Load Balancing click on Load Balancers. Here we can find four types of Load Balancers that are Application Load Balancer, Network Load Balancer, Gateway Load Balancer, and Classic Load Balancer(previous generation). We will create an Application Load Balancer for our tutorial.
Step-1: Basic Configuration
Here we will provide the name of our Load Balancer i.e. My-Test-ALB. We will keep it to be Internet-facing as we want our load balancer to route requests from clients over the internet to target that in our case will be EC2 instances. We can select the type of IP addresses that our subnets will use, for now, we will leave it to IPV4.
Step-2: Network Mapping
Here we will select at least two Availability Zones and one subnet per zone so that the load balancer will route traffic to targets in these Availability Zones only. We will select us-east-2a, us-east-2b, and us-east-2c and one subnet in them accordingly.
Step-3: Security Groups
Here we can either create a new Security Group or choose from the existing ones. We will use the same Security Group i.e. launch-wizard-1 which we created while configuring our EC2 instance.
Step-4: Listeners and Routing
A listener is a process that checks for connection requests, using the protocol and port we configure while creating our AWS Application Load Balancer. Traffic received by the listener is then routed per our specification. Here we can either create a new Target Group or choose from the existing ones. For creating a new Target Group we will click on Create a target group. Note that here traffic on port 80 will be forwarded to the Target Group created.
First, we will specify group details. Note that our load balancer will route requests to the targets in a target group and performs health checks on the targets as well. Targets can be of different types such as Instances, IP addresses, Lambda function, and even an AWS Application Load Balancer. We will keep our target to be Instances that we created above.
Here we will provide the name of our Target Group. In our case, we will keep it to My-Test-Target-Group.
We will keep the remaining configurations as it is. Note that here we can also set the value of Unhealthy threshold, Timeout, and Interval for health checks according to our requirements. Currently, we will keep them to default values. Now we will click on Next.
Now the next step in the creation of Target Group is to register targets. We must register our targets to ensure that our AWS Application Load Balancer routes traffic to this target group. Here we can see the instances we created initially. Now we will click on Include as pending below to register them.
Then we will click on Create target group.
Now our Target group can be viewed in the list of available Target Groups.
We will again get back to the configuration of our AWS Application Load Balancer and select the Target Group created.
Step-5: Add Tags
We can also add tags to our load balancer like we did while creating our EC2 instance but we will leave it for now. Tags enable us to categorize our AWS resources so we can more easily manage them.
Here we can review and confirm configurations of our load balancer and then click on Create load balancer.
Testing AWS Application Load Balancer
Now our AWS Application Load Balancer can be seen in the list of available load balancers. Here we can view all the details related to the load balancer like Description, Listeners, Monitoring, Integrated Services, and Tags. Note that our load balancer is currently in the Provisioning state.
After a while, we can see that the state has changed to Active.
Now we will copy the DNS name of AWS Application Load Balancer from the description and enter it in the browser to see the magic happening!
Here we can clearly see from the hostname that the request from the Application Load Balancer is routed to all the three instances we attached to it as Targets.
In this tutorial, we learned about AWS Application Load Balancer and how it works. Then we walked through the process of creation of EC2 instances as they were to be used as Target Groups for our Application Load Balancer. After that, we created our AWS Application Load Balancer in which we discussed each configuration step briefly. In the end, we witnessed the behaviour of our AWS Application Load Balancer by viewing it distributing traffic to all three instances. Stay tuned for some more informative stuff ahead and feel free to give any feedback in the comments section.