# Application Load Balancer

Today, we will learn about the Application Load Balancer (ALB).

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717121944694/3807ab5f-eb94-42c1-89b9-a7662d4bb1eb.png align="center")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717035749546/1d94401a-af19-47a4-96a3-29b8c96c047b.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717035814350/6b2be349-13cb-4b34-b9ae-0edac0eec5ca.png align="center")

Provide a name for your VPC and input a CIDR value for your VPC.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717035901248/fab65679-46e1-41cd-8d0f-6ed00ee36f9c.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717122104962/509c1e80-4b75-425d-a242-07eedfdad1a2.png align="center")

---

### Task 2: Creating Internet Gateway

On **Internet gateways**, click on `Create internet gateway`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717035949718/c33e1f86-3505-497f-9aa9-f8417c563b6f.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717035990856/e05384a5-5b3b-4900-89f4-d328f4c35b21.png align="center")

After creating an internet gateway, it's time to attach it to our VPC.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717036044484/48d68c01-3bbf-40f0-b888-acc872351f56.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717036073296/a5ba9dad-1a73-49cf-936e-7bfaee9b2c9f.png align="center")

Select your VPC and click on `Attach internet gateway`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717122191893/116d9679-5706-4d57-b70f-3ec41c698a5a.png align="center")

---

### Task 3: Creating Subnets

On **Subnets**, click on `Create subnet`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717036775406/5e37f320-6def-4040-8ab5-fa02d478cbb4.png align="center")

Select your VPC.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717036864362/a18d3658-704d-4719-b3bf-7f63d05ea0eb.png align="center")

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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717036961524/7cc561c1-85f7-4b4f-b5af-41823403af50.png align="center")

Once you are done, click on `Create subnet`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717037023151/561fae64-6561-4627-9bbd-9edd049cac54.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717123658973/6009bc84-9a41-44a1-83f6-ef5cba7add39.png align="center")

---

### Task 4: Creating Route tables

On **Route tables**, click on `Create route table`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717037086241/f2d0d568-8cd0-4a17-9617-1ce716c15e35.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717040222703/0ab0738b-8252-43c6-b658-13a146f1e913.png align="center")

Provide name for your route table, select your VPC & click on `Create route table`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717040313776/018652ed-13f6-413d-9e59-a2d51667e4d8.png align="center")

After creating route table, On **Subnet associations**, click on `Edit subnet associations`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717040375495/11a43d21-9ec8-4751-b629-be9f7ddd1367.png align="center")

Tick both of your subnets and click on `Save associations`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717040457171/1a1d0da1-2aa6-4b90-80ef-844cde43308a.png align="center")

Now on **Routes**, click on `Edit routes`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717040488180/a6b7af67-f5d2-47a8-a773-228ab798504f.png align="center")

Add route, select your internet gateway and click on `Save changes`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717040569336/3f26ca20-2136-40db-8368-7080c30cee4c.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717123727966/ee847218-dc72-480b-aef4-8621913fd784.png align="center")

---

### Task 5: Creating EC2 Instances

Search for **EC2** in the search box.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717041285495/8667a9af-ad62-43b5-85e4-b1da944f64ba.png align="center")

On EC2 dashboard, click on `Launch instance`.

Provide a name for your EC2, select an AMI, create or use an existing key pair.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717041624118/40beae85-0ce0-4d7c-b487-a3c0b2c34d5a.png align="center")

Edit Network settings. Select your VPC, select your subnet and `Enable` Auto-assign public IP.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717041699521/d7a11f4d-1545-4896-be8d-d4ff8531ea4f.png align="center")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717041814491/5edbf381-06cc-45ea-921c-93ec3b14b6ee.png align="center")

Configure the above settings in your new security group rule.

Now, at the bottom, click **Advanced Details** to see more options.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717041932511/167505f5-ae75-4b10-91db-02e8e4b7ad18.png align="left")

On **User data** section, provide the following code:

```bash
#!/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
```

<details data-node-type="hn-details-summary"><summary>Code Explained</summary><div data-type="detailsContent">This script sets up the Apache web server and serves a simple HTML page with the message "Hello from the first server".</div></details>

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717061093900/a0fc032b-b436-426c-bebd-fdb6ce22c2ac.png align="center")

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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717061207481/3a2028c3-0387-4dae-a773-98b25d5992de.png align="center")

You may see something like this.

Now, let's create another instance.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717061327937/27fd86d0-83fc-4d35-8428-a250ff669371.png align="center")

On **Network settings**, for subnet, choose another subnet than you chose earlier.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717061402390/e2b14f27-f166-4a43-867b-8c2ad90fe35f.png align="center")

Now add additional inbound rules like we did earlier.

For **User data**, add the following script:

```bash
#!/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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717062993815/aebdfc07-8212-4a11-a675-1b980d85d26c.png align="center")

We have created two instances.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717063145387/775f3c53-b086-4485-a330-28653384deb1.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717124529476/fd24958e-b6e7-4535-afac-fdc1b518e713.png align="center")

---

### 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**.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717063403032/4cfa77c2-b374-4a4b-9163-1b4d943260fc.png align="center")

Click on `Create target group`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717064226339/8781a6bd-b135-402e-ade2-85bf218531aa.png align="center")

Provide a name for your target group and select your VPC, keeping all other configurations as default.

Click on `Next`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717064331564/6bbcc0ac-01bb-4660-a78a-aa40e5b7e21f.png align="center")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717079708458/97443a11-e884-4fdf-ba88-cad9aa24801d.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717122959084/79a22960-7c8d-4439-9e03-77c9a2057bec.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717125010247/fc2f6cda-9cd7-4023-8316-3c09caa236f2.png align="center")

---

### 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`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717079922216/e5c1dc17-0c17-48c8-85db-aa457c1cff7a.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717080006391/e1772bcb-1910-483d-8778-302208abe42f.png align="center")

Provide a suitable name, description is optional & select your VPC.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717080206600/cf712e30-e2c3-4765-873e-62575bcedc63.png align="center")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717080265718/b44b896a-dc0c-424a-93e1-69006a57636d.png align="center")

---

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717080417541/0cc96121-eff8-46bd-9ec6-10f5e947fea7.png align="center")

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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717080732544/e4f21089-daa4-4d99-9cb1-44b5980c9808.png align="center")

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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717080892075/28c24814-0b3d-4a7c-9b08-5dd1bab89922.png align="center")

Select your VPC and tick on your AZs.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717080985619/c020842b-c20f-4e1a-a56b-e9f6355946d3.png align="center")

In addition to the default security group, select the security group that we created earlier for this load balancer.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717081087646/134e85a9-f746-421f-8a79-788b4032de95.png align="center")

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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717081146101/9f13e4ed-2aba-4ac6-8049-36f8393624b1.png align="center")

You can read a summary about your Application Load Balancer.

Click on `Create load balancer`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717081219679/afee87ab-3a01-4a66-adf1-b7fc3b039118.png align="center")

Let's wait for a while. It may take 3 to 4 minutes.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717081378748/426dc27d-fbf2-4595-85a5-9ab4c60104f4.png align="center")

After a few minutes, our ALB is active. Copy the DNS name of your ALB and paste it into your browser.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717081440457/727174bf-ec9a-4122-affb-25203821a7c0.png align="center")

Try refreshing the browser.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717081466094/97d85aae-a6ff-47fa-8996-e554ab736031.png align="center")

Our ALB is actively distributing traffic to the targets we set up.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717125308673/24d0abde-2458-4347-b339-2689bbd7bd3e.png align="center")

---

### 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

1. **Deleting Load Balancer**
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717084402990/351d1c7c-d0f3-47ce-8730-d283db266249.png align="center")
    
    Select your load balancer and on **Actions panel**, click on `Delete load balancer`.
    
2. **Deleting Target Group**
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717084505833/2f9a8b37-6cf8-4428-8104-dfead5315c9d.png align="center")
    
    Select your target groups and click on `Delete`.
    
3. **Terminating instances**
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717084604420/9b49ce83-df23-49f4-ade2-6331b492745f.png align="center")
    
    Select both of your instances and click on `Terminate instance`.
    
4. **Deleting Subnets**
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717084698271/dab9c734-5a5a-4b3c-afb4-ee9d7af147ee.png align="center")
    
    Search for `VPC` in the search box and click on VPC.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717084784982/8c0f3116-76cb-4fb5-9edb-1ab69840ded7.png align="center")
    
    After navigating to the VPC dashboard, select both of your subnets under **Subnets** and click on `Delete subnet`.
    
5. **Deleting Route tables**
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717084875009/5b39406e-69e6-49bb-9138-cc50db538894.png align="center")
    
    Select your Route table and in **Actions panel**, click on `Delete route table`.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717084950667/2f0dc5eb-395a-4e40-aa63-eac9d2ba814e.png align="center")
    
    If an error occurs, try refreshing the box and then click on `Delete`.
    
6. **Deleting VPC**
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717085061970/4752dd09-51d3-42d2-b7fa-92085bea5c8b.png align="center")
    
    Select your VPC and click on `Delete VPC`.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717085117263/41e8b645-13c7-4f7f-81d0-0069bb214dd6.png align="center")
    
    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!
