Skip to content

This guide demonstrates how to set up load balancing for two EC2 instances hosting a web application. We will use an Application Load Balancer (ALB) in AWS to distribute traffic between the instances.

Notifications You must be signed in to change notification settings

panwar100/aws-ec2-web-loadbalancer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 

Repository files navigation

aws-ec2-web-loadbalancer

Load Balancing with Two EC2 Instances for Web Hosting

This guide demonstrates how to set up load balancing for two EC2 instances hosting a web application. We will use an Application Load Balancer (ALB) in AWS to distribute traffic between the instances.

Prerequisites

  1. An AWS account.
  2. Two EC2 instances running a web server (e.g., Apache or Nginx) with your web application deployed.
  3. Security groups configured to allow HTTP (port 80) traffic.

Steps

1. Launch EC2 Instances

  • Launch two EC2 instances in the same VPC.

Screenshot from 2024-12-26 22-12-07 Screenshot from 2024-12-26 22-12-47 Screenshot from 2024-12-26 22-13-16 Screenshot from 2024-12-26 22-13-45 Screenshot from 2024-12-26 22-59-37

  • Install and configure a web server (e.g., Apache or Nginx) on each instance.

Screenshot from 2024-12-26 23-03-55 Screenshot from 2024-12-26 23-10-26 Screenshot from 2024-12-26 23-15-41

  • Deploy your web application to both instances.

for web1 Screenshot from 2024-12-26 23-13-53

for web2 Screenshot from 2024-12-26 23-28-49

2. Configure Security Groups

  • Ensure both instances have a security group allowing inbound traffic on port 80.

3. Configure Target Group

  • Create a new target group:

Screenshot from 2024-12-26 23-34-07

  • Name: Provide a name for the target group.
  • Target type: Instances.
  • Protocol: HTTP.
  • Port: 80.
  • Health Check: Configure health check settings.

Screenshot from 2024-12-26 23-35-58

  • Register your two EC2 instances as targets.

Screenshot from 2024-12-26 23-36-36 Screenshot from 2024-12-26 23-37-04 Screenshot from 2024-12-26 23-37-19

4. Create an Application Load Balancer (ALB)

  • Navigate to the EC2 dashboard in the AWS Management Console.
  • Under Load Balancing, select Load Balancers and click Create Load Balancer.

Screenshot from 2024-12-26 23-39-05

  • Choose Application Load Balancer.
    • Name: Provide a name for the ALB.
    • Scheme: Internet-facing.
    • Listeners: Add a listener for HTTP (port 80).
    • Availability Zones: Select at least two availability zones.
  • Click Next to configure security settings.

Screenshot from 2024-12-26 23-39-22 Screenshot from 2024-12-26 23-41-07 Screenshot from 2024-12-26 23-41-19 Screenshot from 2024-12-26 23-41-40 Screenshot from 2024-12-26 23-41-59 Screenshot from 2024-12-26 23-42-51

6. Test the Setup

  • Obtain the DNS name of the ALB from the AWS Management Console.

Screenshot from 2024-12-26 23-45-32

  • Access the DNS name in your browser. Traffic should be distributed between the two EC2 instances.

Screenshot from 2024-12-26 23-45-46 Screenshot from 2024-12-26 23-45-55

When accessing the ALB DNS name, you should see the messages alternate as traffic is routed to different instances.

Diagram

[Client] --> [ALB (DNS Name)] --> [EC2 Instance 1]
                                --> [EC2 Instance 2]

Notes

  • Ensure your instances and ALB are in the same VPC.
  • Monitor the ALB and target group health status for issues.
  • For a production setup, consider using HTTPS and configuring SSL/TLS.

Feel free to contribute to this project by submitting issues or pull requests!

About

This guide demonstrates how to set up load balancing for two EC2 instances hosting a web application. We will use an Application Load Balancer (ALB) in AWS to distribute traffic between the instances.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published