Deploying a High-Availability Web Application with AWS EC2 Instances and Application Load Balancer
Introduction:
In this blog, we will walk through the process of launching EC2 instances with Ubuntu AMIs, installing Apache Web Server using User Data, modifying the index.html file, and then creating an Application Load Balancer (ALB) to distribute traffic across the instances. By following these steps, we will deploy a high-availability web application that showcases our personal touch and leverages the load-balancing capabilities of AWS.
Task 1: Launching EC2 Instances with Apache Web Server
Step 1: Launching the First EC2 Instance
Sign in to the AWS Management Console and navigate to EC2.
Launch a new EC2 instance using the Ubuntu AMI.
In the User Data field, enter the following script to install Apache and modify the index.html file:
#!/bin/bash
apt-get update -y
apt-get install apache2 -y
echo "My Name" > /var/www/html/index.html
- Complete the EC2 instance launch process and note down the public IP address.
Step 2: Launching the Second EC2 Instance
Repeat the steps above to launch a second EC2 instance with the Ubuntu AMI.
Modify the User Data script to include the following:
#!/bin/bash
apt-get update -y
apt-get install apache2 -y
echo "TrainWithShubham Community is Super Awesome :)" > /var/www/html/index.html
- Complete the launch process and note down the public IP address of the second instance.
Step 3: Testing the Web Servers
Open a web browser and enter the public IP address of the first EC2 instance.
Verify that the webpage displays your name.
Repeat the process for the second EC2 instance and ensure that the page shows the customized message.
Task 2: Creating an Application Load Balancer
Step 1: Creating an ALB
Go to the AWS Management Console and navigate to the EC2 service.
Click on "Load Balancers" and choose "Create Load Balancer".
Select "Application Load Balancer" and configure the necessary settings, such as listener ports and availability zones.
Create a new target group for the EC2 instances.
Step 2: Adding EC2 Instances to the ALB
In the target group settings, choose "Edit" and add the EC2 instances launched in Task 1.
Configure the health check settings to ensure the instances are healthy.
Step 3: Testing the ALB
Access the ALB's DNS name provided in the console.
Observe that the requests are distributed across the EC2 instances.
Verify the health status of the instances in the ALB console.
Step 1: Creating an ALB
aws elbv2 create-load-balancer \
--name my-application-load-balancer \
--subnets subnet-abc12345 subnet-def67890 \
--security-groups sg-12345678 \
--type application \
--scheme internet-facing
Step 2: Creating a Target Group
aws elbv2 create-target-group \
--name my-target-group \
--protocol HTTP \
--port 80 \
--vpc-id vpc-12345678
Step 3: Registering EC2 Instances to the Target Group
aws elbv2 register-targets \
--target-group-arn arn:aws:elasticloadbalancing:region:123456789012:targetgroup/my-target-group/abcdefghijkl \
--targets Id=i-1234567890abcdef0 Id=i-0987654321abcdef0
Step 4: Creating a Listener for the ALB
aws elbv2 create-listener \
--load-balancer-arn arn:aws:elasticloadbalancing:region:123456789012:loadbalancer/app/my-application-load-balancer/abcdefghijkl \
--protocol HTTP \
--port 80 \
--default-actions Type=forward,TargetGroupArn=arn:aws:elasticloadbalancing:region:123456789012:targetgroup/my-target-group/abcdefghijkl
Step 5: Testing the ALB Once the ALB is created and the instances are registered, you can access the ALB's DNS name provided in the console through a web browser. The requests will be automatically distributed across the registered EC2 instances, and you can verify the health status of the instances in the ALB console.
Please note that you need to replace the placeholders like subnet-abc12345
, sg-12345678
, vpc-12345678
, region
, and instance IDs (i-1234567890abcdef0
, i-0987654321abcdef0
) with your actual values before running the commands.
Conclusion:
By following this guide, you have successfully launched EC2 instances with Ubuntu AMIs, installed Apache Web Server, modified the index.html file, and created an Application Load Balancer. This setup ensures high availability and load balancing for your web application. Now, you can scale your infrastructure to handle increased traffic and provide a seamless user experience. Happy deploying!
To connect with me - https://www.linkedin.com/in/subhodey/