Setting up an Application Load Balancer with AWS EC2

Setting up an Application Load Balancer with AWS EC2

Task 1: Launch 2 EC2 Instances and Install Apache

Step 1: Launch EC2 Instances

  1. Log in to AWS Console.

  2. Go to the EC2 Dashboard.

  3. Click on “Launch Instance.”

  4. Select “Ubuntu Server” as the AMI (Amazon Machine Image).

  5. Choose an instance type (e.g., t2.micro if you're on free tier).

  6. Click “Next” until you reach “Advanced Details.”

Step 2: Install Apache Automatically (Using User Data)

  1. Scroll down to “Advanced Details” and expand the section.

  2. In the “User Data” field, paste the following commands:

     #!/bin/bash
     sudo apt update
     sudo apt install -y apache2
     sudo systemctl start apache2
     sudo systemctl enable apache2
    
  3. Continue with the next steps: Add Storage, Configure Security Group (allow HTTP (port 80) and SSH (port 22)), and then Launch the Instance.

  4. Repeat the steps to create the second instance.


Step 3: Modify Web Page (index.html) on Both Instances

  1. Connect to the first EC2 instance using SSH:

     ssh -i /path/to/your-key.pem ubuntu@<EC2-Public-IP>
    
  2. Check Apache Status:

     sudo systemctl status apache2
    
  3. Edit the Default Web Page:

     sudo nano /var/www/html/index.html
    
    • Add your name, e.g., Welcome to John's Server!

    • Press Ctrl + O to save, then Ctrl + X to exit.

  4. Repeat the same for the second instance but update the content to:

     #For First Instance
     This is Server A
    
     #For Second Instance
     This is Server B
    
  5. Test in a Browser:

    • Copy the public IP of each instance.

    • Open the browser and paste the IP. You should see your custom message.


Task 2: Create an Application Load Balancer (ALB)

Step 1: Set Up the Load Balancer

  1. Go to the EC2 Dashboard.

  2. On the left menu, click “Load Balancers”“Create Load Balancer.”

  3. Choose “Application Load Balancer” and click “Create.”

  4. Set a name for the load balancer.

  5. Under Listeners, select HTTP (port 80).

Step 2: Configure Security and Availability

  1. Choose the same VPC and Availability Zones as your EC2 instances.

  2. Assign a Security Group that allows HTTP (port 80).

Step 3: Create a Target Group

  1. Choose “Create Target Group.”

  2. Select “Instances” as the target type.

  3. Name the target group and select HTTP (port 80).

  4. Under Targets, add both EC2 instances from Task 1.

  5. Finish setup and review everything before creating.


Step 4: Test the Load Balancer

  1. Go back to the Load Balancer page.

  2. Copy the DNS name of the load balancer (something like alb-1234567890.us-east-1.elb.amazonaws.com).

  3. Open the DNS name in your browser.

Result:

  • Refresh the page multiple times.

  • You should see the content switching between the two messages:

    • Name from the first instance - This is Server A

    • Name from the second instance - This is Server B

This proves that the load balancer is distributing traffic evenly.