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.
- An AWS account.
- Two EC2 instances running a web server (e.g., Apache or Nginx) with your web application deployed.
- Security groups configured to allow HTTP (port 80) traffic.
- Launch two EC2 instances in the same VPC.
- Install and configure a web server (e.g., Apache or Nginx) on each instance.
- Deploy your web application to both instances.
- Ensure both instances have a security group allowing inbound traffic on port 80.
- Create a new target group:
- Name: Provide a name for the target group.
- Target type: Instances.
- Protocol: HTTP.
- Port: 80.
- Health Check: Configure health check settings.
- Register your two EC2 instances as targets.
- Navigate to the EC2 dashboard in the AWS Management Console.
- Under Load Balancing, select Load Balancers and click Create Load Balancer.
- 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.
- Obtain the DNS name of the ALB from the AWS Management Console.
- Access the DNS name in your browser. Traffic should be distributed between the two EC2 instances.
When accessing the ALB DNS name, you should see the messages alternate as traffic is routed to different instances.
[Client] --> [ALB (DNS Name)] --> [EC2 Instance 1]
--> [EC2 Instance 2]
- 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!