Skip to main content

Command Palette

Search for a command to run...

Automating AWS ALB using AWS CLI and Shell Scripting

Published
5 min read

Using shell script to launch a load balancer on AWS

Introduction

In this tutorial, we will explore how to create an Application Load Balancer (ALB) on AWS using the command-line interface (CLI). Load balancers play a crucial role in distributing incoming traffic across multiple instances, ensuring high availability and scalability for your applications. We will walk through the steps involved in setting up a custom Virtual Private Cloud (VPC), creating subnets, configuring security groups, creating target groups, and finally creating the ALB. So let's dive in!

Types of Load Balancers Offered by AWS

Before we dive into the code, let's briefly discuss the types of load balancers offered by AWS. AWS provides three types of load balancers:

  • Application Load Balancer (ALB): The second-generation load balancer that operates at the application layer (Layer 7) and provides advanced routing and content-based load balancing features. ALB is ideal for modern web applications and microservices architectures.

  • Network Load Balancer (NLB): The third-generation load balancer that works at the transport layer (Layer 4) and provides ultra-high performance and low latency. NLB is suitable for TCP and UDP traffic scenarios that require extreme scalability.

Creating a Custom VPC

The first step is to create a custom Virtual Private Cloud (VPC) using the AWS CLI. The script prompts you to enter a CIDR block for the VPC. The CIDR block defines the IP address range for your VPC. Once executed, the script will display the VPC ID of the newly created VPC.

Let's break down the code that creates the VPC:

echo "The VPC ID of the newly created VPC is:";
aws ec2 create-vpc --cidr-block $cidr_block --output text --query 'Vpc.VpcId'
  • The aws ec2 create-vpc command is used to create the VPC. We pass the --cidr-block option with the value of the CIDR block entered by the user.

  • The --output text --query 'Vpc.VpcId' parameters are used to retrieve and display only the VPC ID in the output.

Configuring Subnets and Internet Gateway

Next, we create two custom subnets within the VPC. The script prompts you to enter the CIDR blocks for the subnets. Additionally, an internet gateway is created and attached to the VPC to enable internet connectivity for the subnets.

The code snippet below illustrates the creation of subnets and an internet gateway:

echo "Creating Subnets...";
subnet_id1=$(aws ec2 create-subnet --vpc-id $vpc_id --cidr-block $subnet_cidr_block1 --output text --query 'Subnet.SubnetId');
subnet_id2=$(aws ec2 create-subnet --vpc-id $vpc_id --cidr-block $subnet_cidr_block2 --output text --query 'Subnet.SubnetId');

echo "Creating Internet Gateway...";
internet_gateway_id=$(aws ec2 create-internet-gateway --output text --query 'InternetGateway.InternetGatewayId');

aws ec2 attach-internet-gateway --internet-gateway-id $internet_gateway_id --vpc-id $vpc_id;
  • The aws ec2 create-subnet command is used to create the subnets. We pass the --vpc-id option with the value of the VPC ID and the --cidr-block option with the values of the CIDR blocks entered by the user.

  • The aws ec2 create-internet-gateway command is used to create the internet gateway.

  • The aws ec2 attach-internet-gateway command is used to attach the internet gateway to the VPC.

Creating Security Groups

Security groups control the inbound and outbound traffic for your instances. The script creates a custom security group and prompts you to enter the security group ID. You can then add inbound rules to allow TCP, SSH, and HTTP traffic.

The following code snippet demonstrates the creation of a security group and adding inbound rules:

echo "Creating Security Group...";
security_group_id=$(aws ec2 create-security-group --group-name $security_group_name --description "Load Balancer Security Group" --vpc-id $vpc_id --output text --query 'GroupId');

echo "Adding Inbound Rules...";
aws ec2 authorize-security-group-ingress --group-id $security_group_id --protocol tcp --port 80 --cidr 0.0.0.0/0;
aws ec2 authorize-security-group-ingress --group-id $security_group_id --protocol tcp --port 22 --cidr 0.0.0.0/0;
aws ec2 authorize-security-group-ingress --group-id $security_group_id --protocol tcp --port 443 --cidr 0.0.0.0/0;
  • The aws ec2 create-security-group command is used to create the security group. We pass the --group-name, --description, and --vpc-id options with the values entered by the user.

  • The aws ec2 authorize-security-group-ingress command is used to add inbound rules to the security group. We specify the --group-id option with the security group ID and the --protocol, --port, and --cidr options with the desired values for each rule.

Creating Target Groups

Target groups are used to route requests to registered targets (instances). The script prompts you to enter the target group name, port, and protocol. Once entered, the target group is created.

Here's the code snippet for creating the target group:

echo "Creating Target Group...";
target_group_arn=$(aws elbv2 create-target-group --name $target_group_name --protocol $target_group_protocol --port $target_group_port --vpc-id $vpc_id --output text --query 'TargetGroups[0].TargetGroupArn');
  • The aws elbv2 create-target-group command is used to create the target group. We pass the --name, --protocol, --port, and --vpc-id options with the values entered by the user.

  • The --output text --query 'TargetGroups[0].TargetGroupArn' parameters are used to retrieve and store the ARN (Amazon Resource Name) of the target group.

Creating the Application Load Balancer

Finally, we create the Application Load Balancer (ALB) by specifying the listener, target group, subnets, and security groups.

The code snippet below shows the creation of the ALB:

echo "Creating Application Load Balancer...";
load_balancer_arn=$(aws elbv2 create-load-balancer --name $load_balancer_name --subnets $subnet_id1 $subnet_id2 --security-groups $security_group_id --output text --query 'LoadBalancers[0].LoadBalancerArn');

echo "Creating Listener...";
aws elbv2 create-listener --load-balancer-arn $load_balancer_arn --protocol HTTP --port 80 --default-actions Type=forward,TargetGroupArn=$target_group_arn;

echo "Application Load Balancer created successfully!";
  • The aws elbv2 create-load-balancer command is used to create the ALB. We pass the --name, --subnets, and --security-groups options with the appropriate values.

  • The --output text --query 'LoadBalancers[0].LoadBalancerArn' parameters are used to retrieve and store the ARN of the ALB.

  • The aws elbv2 create-listener command is used to create the listener for the ALB. We specify the --load-balancer-arn, --protocol, --port, and --default-actions options with the appropriate values.

Running the shell script

To execute the script, make it executable by running the following command:

chmod +x create-alb.sh

Then, run the script using the command:

./create-alb.sh

Conclusion

In this tutorial, we have learned how to create an Application Load Balancer (ALB) on AWS using the command-line interface (CLI). We walked through the process of setting up a custom VPC, creating subnets, configuring security groups, creating target groups, and finally creating the ALB. Load balancers are essential for ensuring high availability and scalability for your applications, and understanding how to set them up using the AWS CLI is a valuable skill. By following the steps outlined in this tutorial, you can easily create an ALB to distribute traffic across your instances and enhance the performance and availability of your applications.