Application Load Balancer
A load balancer serves as the single point of contact for clients. The load balancer distributes incoming application traffic across multiple targets, such as EC2 instances, in multiple Availability Zones. This increases the availability of your application. You add one or more listeners to your load balancer.
User Data script:
#!/bin/bash
sudo yum update -y
sudo yum install -y httpd
sudo systemctl start httpd
sudo systemctl enable httpd
Step 2: Connect to first EC2 instance and run below commands
cd /var/www/html
mkdir orders (this is case-sensitive! will reflect in other steps)
cd orders
echo "<h1> Orders App </h1> >> index.html
Step 3: Connect to second EC2 instance and run below commands
cd /var/www/html
mkdir payments (this is case-sensitive! will reflect in other steps)
cd payments
echo "<h1> Payments App </h1> >> index.html
Step 4: Define the target groups which helps to route request to appropriate application.
Select Target Group from Load Balancer Menu and Define below properties
Target Type : Instances
Target Group Name: OrderTG
Protocol HTTP: 80
VPC: default VPC
Protocol Version: HTTP1
Health check Path: orders/index.html
Click on Next button
Register Targets
Select first EC2 instance
Click on Include Pending Below
and Click on Create Target Group
Target Type : Instances
Target Group Name: PaymentsTG
Protocol HTTP: 80
VPC: default VPC
Protocol Version: HTTP1
Health check Path: payments/index.html
Click on Next button
Register Targets
Select second EC2 instance
Click on Include Pending Below
and Click on Create Target Group
Step 5: Create Application Load Balancer
Click on Create Load balancer Button
Select Application Load Balancer and Click on Create
Load Balancer Name: ALB
Mappings: Select all the AZs
VPC: default VPC
Security Group: WebserverSG
Default Listener : Port: 80
Click on Create Load Balancer Button
Step 6: Load Balancer should be created and it should have a DNS Name by which you are connecting to the applications. Copy the DNS name and put it in the browser, you should be able to see the default apache web page which is implemented in the First EC2 instance (cause default action is OrdersTG).
Step 7: Select Listener tab for ALB and click on view/edit rules link
Set below properties for the rule
Path : /orders*
Forward to : OrdersTG
and Click on Save
Click on + symbol on this page and then Insert Rule link
Set below properties for the rule
Path : /payments*
Forward to : PaymentsTG
and Click on Save
Step 8: Go to browser and put <DNS_name_of_ALB>/orders and <DNS_name_of_ALB>/payments it should redirect to respective application.