DEV Community

Kerisnarendra
Kerisnarendra

Posted on

AWS Application Load Balancer Setup

Image description

Introduction

In this blog post, I am going to remind myself the step-by-step process of setting up an Application Load Balancer (ALB) in AWS using the command line interface (CLI). ALB helps distribute incoming traffic across multiple EC2 instances, ensuring high availability, fault tolerance, and efficient scaling of web applications.

Prerequisites

Before we get started, make sure we have the following:

  • An AWS account with permissions to create security groups, launch templates, auto scaling groups, target groups, and load balancers.
  • Familiarity with AWS services and the AWS CLI.

Step-by-Step Guide

  • Create a Security Group: A security group acts as a virtual firewall for instances, controlling inbound and outbound traffic. Run the following command to create a security group named "my-security-group" allowing incoming traffic on ports 22 (SSH) and 80 (HTTP) from any source:
aws ec2 create-security-group --group-name my-security-group --description "my-security-group" --vpc-id OUR_VPC_ID --region OUR_REGION
Enter fullscreen mode Exit fullscreen mode
  • Create a Launch Template: Launch templates define the launch configuration for instances. Create a launch template named "my-launch-template" using the Amazon Linux 2 AMI and T2 Micro instance type, and specify the "my-security-group" security group and user data for instance initialization:
aws ec2 create-launch-template --launch-template-name my-launch-template --image-id OUR_AMI_ID --instance-type t2.micro --security-group-ids OUR_SECURITY_GROUP_ID --user-data file://user-data-script.txt --region OUR_REGION
Enter fullscreen mode Exit fullscreen mode

user-data-script.txt contains these command below to install and start Apache HTTPD and deploy a web page to let us know where the EC2 instance is located:

#!/bin/bash
yum update -y
yum install -y httpd
systemctl start httpd
systemctl enable httpd
AZ=$(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone)
echo '<center><h1>This EC2 instance is located in Availability Zone: AZ</h1></center>' > /var/www/html/index.txt
sed "s/AZ/$AZ" /var/www/html/index.txt > /var/www/html/index.html
Enter fullscreen mode Exit fullscreen mode
  • Create an Auto Scaling Group (ASG): An ASG manages the desired number of instances, scaling them based on demand. Create an ASG named "my-auto-scaling-group" with a minimum size of 1, maximum size of 3, and desired capacity of 2 instances. Specify the subnets we want to use for instances:
aws autoscaling create-auto-scaling-group --auto-scaling-group-name my-auto-scaling-group --launch-template LaunchTemplateName=my-launch-template,Version=1 --min-size 1 --max-size 3 --desired-capacity 2 --vpc-zone-identifier "OUR_SUBNET_IDS" --region OUR_REGION
Enter fullscreen mode Exit fullscreen mode
  • Create a Target Group: A target group defines a set of instances that handle incoming traffic. Create a target group named "my-target-group" for HTTP traffic on port 80 in our default VPC:
aws elbv2 create-target-group --name my-target-group --protocol HTTP --port 80 --vpc-id OUR_VPC_ID --region OUR_REGION
Enter fullscreen mode Exit fullscreen mode
  • Create an Application Load Balancer (ALB): Now, create an ALB named "my-application-load-balancer" that is internet-facing, using the "my-target-group" target group, and associated with the previously created security group:
aws elbv2 create-load-balancer --name my-application-load-balancer --subnets OUR_SUBNET_IDS --security-groups OUR_SECURITY_GROUP_ID --scheme internet-facing --type application --region OUR_REGION
Enter fullscreen mode Exit fullscreen mode
  • Create a Listener: A listener configures the ALB to forward incoming requests to the target group. Create a listener for HTTP traffic on port 80, forwarding it to "my-target-group":
aws elbv2 create-listener --load-balancer-arn OUR_ALB_ARN --protocol HTTP --port 80 --default-actions Type=forward,TargetGroupArn=OUR_TARGET_GROUP_ARN --region OUR_REGION
Enter fullscreen mode Exit fullscreen mode
  • Attach the Auto Scaling Group to the Load Balancer: Finally, attach the ASG "my-auto-scaling-group" to the ALB using the "my-target-group" target group:
aws autoscaling attach-load-balancer-target-groups --auto-scaling-group-name my-auto-scaling-group --target-group-arns OUR_TARGET_GROUP_ARN --region OUR_REGION
Enter fullscreen mode Exit fullscreen mode

Conclusion:

ALB plays a vital role in ensuring our application can handle varying loads while maintaining high availability. By following this guide, you can create a robust infrastructure that scales seamlessly with demand. Happy load balancing!

Top comments (0)