Today, we will learn about the Application Load Balancer (ALB).
An Application Load Balancer distributes incoming application traffic across multiple targets, such as EC2 instances, containers, and IP addresses. Operating at the application layer (OSI layer 7), ALB is ideal for HTTP and HTTPS traffic. It offers advanced routing, integrates with AWS services, and includes features like user authentication and WebSocket support, ensuring high availability and reliability for modern applications.
Overview
In this setup, we will create two EC2 instances in different subnets within a VPC. We will then configure an Application Load Balancer (ALB) to distribute incoming traffic across these instances. The ALB will serve as the entry point for all traffic and will route requests to the targets.
Task 1: Creating a VPC
Search for VPC and click on Create VPC
.
Provide a name for your VPC and input a CIDR value for your VPC.
Task 2: Creating Internet Gateway
On Internet gateways, click on Create internet gateway
.
After creating an internet gateway, it's time to attach it to our VPC.
Select your VPC and click on Attach internet gateway
.
Task 3: Creating Subnets
On Subnets, click on Create subnet
.
Select your VPC.
Provide a name for your subnet, choose AZ and input an eligible CIDR range.
For this project, we are picking different AZs.
Create another subnet.
Once you are done, click on Create subnet
.
Task 4: Creating Route tables
On Route tables, click on Create route table
.
Provide name for your route table, select your VPC & click on Create route table
.
After creating route table, On Subnet associations, click on Edit subnet associations
.
Tick both of your subnets and click on Save associations
.
Now on Routes, click on Edit routes
.
Add route, select your internet gateway and click on Save changes
.
Task 5: Creating EC2 Instances
Search for EC2 in the search box.
On EC2 dashboard, click on Launch instance
.
Provide a name for your EC2, select an AMI, create or use an existing key pair.
Edit Network settings. Select your VPC, select your subnet and Enable
Auto-assign public IP.
We are provided with a default security group that has SSH configuration.
Now, let's add another security group rule to allow HTTP traffic to our instance.
Click on Add security group rule
.
Configure the above settings in your new security group rule.
Now, at the bottom, click Advanced Details to see more options.
On User data section, provide the following code:
#!/bin/bash
yes | sudo apt update
yes | sudo apt install apache2
echo "<h1>Hello from the first server</h1>" > /var/www/html/index.html
sudo systemctl restart apache2
Code Explained
User data in EC2 instances refers to a script or set of commands you can provide when launching an instance. This script runs automatically when the instance starts for the first time. This process is known as bootstrapping.
User data automates the setup of EC2 instances, saving time and ensuring consistency by automatically installing software and configuring settings when the instance starts, eliminating manual tasks.
But for now, we are just displaying Hello message.
Click on Launch instance
.
After your instance is up and running, go to the instance details, copy the Public IPv4 address, open a new tab, and paste the address there.
You may see something like this.
Now, let's create another instance.
On Network settings, for subnet, choose another subnet than you chose earlier.
Now add additional inbound rules like we did earlier.
For User data, add the following script:
#!/bin/bash
yes | sudo apt update
yes | sudo apt install apache2
echo "<h1>Hello from the second server</h1>" > /var/www/html/index.html
sudo systemctl restart apache2
And all the procedures are the same, and we get this.
We have created two instances.
Task 6: Create a Target Group
A target group is a set of resources that a load balancer routes traffic to. It ensures efficient traffic distribution and high availability by routing only to healthy targets. Target groups also support configuring rules to direct traffic based on specific conditions, helping manage and scale applications effectively.
On EC2 dashboard, scroll down to Target Groups.
Click on Create target group
.
Provide a name for your target group and select your VPC, keeping all other configurations as default.
Click on Next
.
Select both of your instances and click on Include as pending below
.
This option allows you to add targets to a target group in a non-active state initially. These targets won't receive traffic from the ALB until they pass the configured health checks.
Click on Create target group
.
Task 7: Creating Security Group
Security Groups (SGs) act as virtual firewalls for your instances to control inbound and outbound traffic. You can define rules to allow or deny specific traffic based on IP addresses, ports, and protocols. Security Groups are stateful, meaning if you allow an inbound request, the response is automatically allowed.
Let's create security group for our Load Balancer.
On EC2 dashboard, on Security Groups, click on Create security group
.
Provide a suitable name, description is optional & select your VPC.
On Inbound rules, click on Add rule
to configure above configurations.
We're allowing HTTP traffic from anywhere on the internet.
Click on Create security group
.
Task 8: Creating Load Balancer
A Load Balancer (LB) distributes incoming traffic across multiple targets, such as EC2 instances, to ensure high availability and reliability. It helps balance the load, improving application performance and fault tolerance. There are different types of load balancers, including Application Load Balancer (ALB) for HTTP/HTTPS traffic and Network Load Balancer (NLB) for TCP/UDP traffic.
On EC2 dashboard, On Load Balancer, click on Create load balancer
.
Choose ALB as the load balancer and click on Create
.
Provide a name for your ALB.
When you create a load balancer, you must decide whether to make it an internal load balancer or an internet-facing load balancer.
The nodes of an internet-facing load balancer have public IP addresses.
The nodes of an internal load balancer have only private IP addresses.
Both internet-facing and internal load balancers route requests to your targets using private IP addresses.
Select your VPC and tick on your AZs.
In addition to the default security group, select the security group that we created earlier for this load balancer.
A listener is responsible for checking or listening for incoming connection requests on a specified protocol and port, such as HTTP on port 80 or HTTPS on port 443. Once a listener accepts a connection, it uses routing rules to determine how to direct the traffic.
These rules can route traffic based on hostnames, URL paths, HTTP headers, or query string parameters. The traffic is then forwarded to the appropriate target group.
You can read a summary about your Application Load Balancer.
Click on Create load balancer
.
Let's wait for a while. It may take 3 to 4 minutes.
After a few minutes, our ALB is active. Copy the DNS name of your ALB and paste it into your browser.
Try refreshing the browser.
Our ALB is actively distributing traffic to the targets we set up.
Conclusion
We have successfully set up an Application Load Balancer (ALB) to distribute traffic, improving our application's availability and reliability. We added two EC2 instances as targets and configured the ALB to direct traffic to them.
Task 9: Clean Up
Deleting Load Balancer
Select your load balancer and on Actions panel, click on
Delete load balancer
.Deleting Target Group
Select your target groups and click on
Delete
.Terminating instances
Select both of your instances and click on
Terminate instance
.Deleting Subnets
Search for
VPC
in the search box and click on VPC.After navigating to the VPC dashboard, select both of your subnets under Subnets and click on
Delete subnet
.Deleting Route tables
Select your Route table and in Actions panel, click on
Delete route table
.If an error occurs, try refreshing the box and then click on
Delete
.Deleting VPC
Select your VPC and click on
Delete VPC
.This action will also delete our Internet Gateway. Alternatively, we could perform these actions individually.
We have successfully cleaned up the resources that were in use.
Hope you enjoyed the project!
I'll see you in the next one!