Application Load Balancer Using AWS CLI

Varun Ladha
4 min readMay 5, 2021

Introduction:

Cloud Computing is the on-demand delivery of computer power, database, storage and application, and other IT resources through a cloud service platform via the internet with pay-as-you-go pricing.

Load balancing is a vital component of any cloud environment. It plays an important role in maintaining the availability of your cloud-based applications to customers, business partners, and end-users. It is important in cloud computing to maintain scalability and availability.

AWS Elastic Load Balancing offers three types of load balancers that all feature the high availability, automatic scaling, and robust security necessary to make your applications fault-tolerant.

The three types of load balancers are:

1. Application Load Balancer operates at Application Layer (7th layer). It is ideal to route HTTPS and HTTPS request to targets — EC2 instances, containers, IP addresses and Lambda functions.

2. Network Load Balancer operates at the Network layer(4thlayer). It is ideal for TCP and UDP traffic routing connections to Amazon EC2 instances, microservices, and containers — within Amazon Virtual Private Cloud (Amazon VPC) based on IP protocol data. Network Load Balancer is optimized to handle sudden and volatile traffic patterns while using a single static IP address per Availability Zone.

3. Classic Load Balancer provides basic load balancing across multiple Amazon EC2 instances and operates at both the request level and connection level. Classic Load Balancer is intended for applications that were built within the EC2-Classic network.

We can use AWS Elastic Load Balancer using AWS Management Console as well as AWS CLI.

AWS Management Console bring the whole Cloud to your desktop or mobile screens.

AWS Command Line Interface (CLI) is a unified tool to manage your AWS services. Using it, we can control multiple AWS services from the command line and automate them through scripts.

Here we have used AWS CLI for creating load balancer.

Steps to follow:

1.Create a free tier account.

2.Create a IAM User using AWS IAM.

a. First select add user and give user name.

b. Select Programmatic access and management console access both.

c. Select custom password and give password.

d. Set permissions. There are three options:

i. You can add user to an existing group or can create a new group and can attach policies as per requirement.

ii. You can copy permission given to the older user for the new user.

iii. You can attach existing policy directly to the user

e. Give tags(optional), Review and Download the csv file and store it in a secure place.

Here I have created group Adminusers and nawab user is its member.

3.Type pip install awscli command in command line to download it.

4.Type aws configure.

a. Enter Access Key from the csv file you downloaded.

b. Secret access key from the csv file you downloaded.

c. Enter your region name in which you will create VPC.

d. Enter json in output format.

5. Create your own VPC or can use the default VPC.Here I have created my VarunVPC.

6. You have to make two public subnets in your VPC in different Availability zone(compulsory).

7. Launch one EC 2 instance in one subnet and other EC 2 instance in other subnet.

8. Create security groups for both the instances and add HTTP and SSH inbound rules. Ensure that the security groups for these instances allow access on the listener port and the health check port.

9. Use the following command aws elbv2 help to verify that you are running a version of the AWS CLI that supports Application Load Balancers.

10.Create an Application Load Balancer by giving name of load balancer(it cannot contain _)and subnet id of two subnet in different Availability zone,

aws elbv2 create-load-balancer — name varun-load-balancer — subnets subnet-0fd63d83e2100272c subnet-091b320adbbdcc170 — security-groups sg-0e7cf6e882453be81

11.Create target group giving target group name,protocol,port and vpc id

aws elbv2 create-target-group — name varun-targets — protocol HTTP — port 80 — vpc-id vpc-01554eba4663d7a87

12.Use the register-targets command to register your instances with your target group.Use the instance id of both the EC 2 instance:

aws elbv2 register-targets — target-group-arn arn:aws:elasticloadbalancing:us-west-2:136983541552:targetgroup/varun-targets/078a41e9ec1881f9 — targets Id=i-066cc641c89d3e711 Id=i-0dc590dabbc78c0ab

13. Use the create listener command to create a listener for your load balancer with a default rule that forwards requests to your target group:

aws elbv2 create-listener — load-balancer-arn arn:aws:elasticloadbalancing:us-west-2:136983541552:loadbalancer/app/varun-load-balancer/f19dfb49e137500b — protocol HTTP — port 80 — default-actions Type=forward,TargetGroupArn=arn:aws:elasticloadbalancing:us-west-2:136983541552:targetgroup/varun-targets/078a41e9ec1881f9

14. We can check the health of the instances.

aws elbv2 describe-target-health — target-group-arn arn:aws:elasticloadbalancing:us-west-2:136983541552:targetgroup/varun-targets/078a41e9ec1881f9

15.Delete the Load Balancer giving its arn.

aws elbv2 delete-load-balancer — load-balancer-arn arn:aws:elasticloadbalancing:us-west-2:136983541552:loadbalancer/app/varun-load-balancer/f19dfb49e137500b

16.Delete the Target groups.

aws elbv2 delete-target-group — target-group-arn arn:aws:elasticloadbalancing:us-west-2:136983541552:targetgroup/varun-targets/078a41e9ec1881f9

Conclusion: Hence we learned how to launch Application Load Balance using AWS CLI.

--

--