A Complete Guide to AWS Elastic Load Balancer using Nodejs
Tags: aws, webdev, showdev, devops
Published: 2019-12-22 16:19:50

---
title: A Complete Guide to AWS Elastic Load Balancer using Nodejs
published: true
description: A Complete Guide to AWS Elastic Load Balancer using Nodejs
tags: aws,webdev,showdev,devops
series : AWS
canonical_url: https://cloudnweb.dev/2019/12/a-complete-guide-to-aws-elastic-load-balancer-using-nodejs/
---

<!-- wp:paragraph -->
<p>In this article, we will see what is elastic load balancer and how it is used in the application development.A Complete Guide to AWS Elastic Load Balancer using Nodejs.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Recent Article</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p><a href="https://cloudnweb.dev/2019/12/introduction-to-aws-sns-using-nodejs/">Introduction to AWS SNS using Nodejs</a></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><a href="https://cloudnweb.dev/2019/11/how-to-run-mongodb-as-a-docker-container-in-development/">How to Run MongoDB as a Docker Container in Development</a></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><a href="https://cloudnweb.dev/2019/12/how-to-install-nodejs-in-aws-ec2-in-a-proper-way/">How to Install Nodejs in AWS EC2 in a proper way</a></p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>What is Elastic Load Balancer</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Elastic load balancer is a cloud service that automatically distribute the incoming application traffic into multiple instances. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Let's understand this with an example,</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Let's say that the application is running in an instance with certain amount of users.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>what happens if there is sudden spike in the user load. how can you manage the load in your application server.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>So, solution is to balance the load across different instances of the server. but, how do you manually distribute the load to multiple instances. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>There comes the elastic load balancer. it will distribute the load for you without any manual work.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":1930,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://cloudnweb.dev/wp-content/uploads/2019/12/elb.png" alt="" class="wp-image-1930"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Here, elastic load balancer distribute the load to multiple instances. each client is distributes to different instances.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Key Concepts of ELB</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Firstly, There are some key concepts of ELB that you need to understand before getting hands-on on the Load balancer.</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li>Elastic Load balancer spreads the load across multiple instances</li><li>ELB exposes the single point of access for your application. user will have have the url of ELB.</li><li>it can seamlessly handle failures of down stream instances.</li><li>ELB does the  regular health checks on your application instances, ELB terminates the instance from target group, if it is not healthy.</li><li>Elastic Load Balancer can provide https(SSL) for your application.</li><li>ELB can provide stickiness with cookies, which means a particular can always be routed to a particular instance. </li><li>Load Balancer provide high availability across zones.</li><li>it can separate public traffic from private traffic.</li></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":3} -->
<h3>Types of ELB</h3>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li>Classic Load Balancer</li><li>Application Load Balancer</li><li>Network Load Balancer</li></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>Classic Load balancer is the oldest and kind of deprecated load balancer. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Mainly, Application load balancer(ALB) is the common load balancer nowadays.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>ALB handles the traffic of HTTP/HTTPS and websocket protocols.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Network Load balancer(NLB) handles the (Layer 4) TCP traffic. NLB is super high performance among all the load balancers.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Application Load Balancer</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Application Load Balancer(ALB) distributes the load across the application instances. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Like i said before, ALB supports the HTTP/HTTPS and websocket protocols.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>ALB can enable the stickiness of client to server. stickiness can be enabled at the target group level. </p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":4} -->
<h4>what is Target Group</h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Internally, load balancer contains the listener which will check for some rule. when the rule is met, it will forward the request to specified target group. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>listener rule can be a specific port or something. listener checks if the client request on the specific port(3000). after that, listener forwards the request to the respective target groups.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>target group contains the multiple target(EC2 instances).each target group route the request to one or more registered target.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Application Load Balancer(Contd)</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>mainly, application server don't see the ip of the client that is requesting the resources. all the ec2 instance will get only the ip of elastic load balancer.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>However, client ip can be accessed from the header <strong>X-Forwarded-For</strong>.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Network Load Balancer</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Network Load Balancer(NLB) handles the (Layer 4) TCP Traffic. NLB is a super high performance with very low latency of ~100ms.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>NLB can handle million request per second.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Enough of the theory part, let's learn how to configure the load balancer for a Nodejs Application.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Implementing ELB for Nodejs App</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>I assume that you have basic knowledge on setting up an EC2 in AWS.  you can refer <a href="https://cloudnweb.dev/2019/12/how-to-install-nodejs-in-aws-ec2-in-a-proper-way/">this article</a> to learn more about EC2 and Nodejs Setup.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Source code for the node application can be found <a href="https://github.com/ganeshmani/node-elb-basic">here</a>.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Once you create an EC2 instance, now it's time to setup the load balancer for EC2 instance.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":1949,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://cloudnweb.dev/wp-content/uploads/2019/12/Screenshot-2019-12-21-at-9.35.12-AM-1024x537.png" alt="" class="wp-image-1949"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Click on <strong>Create Load Balancer</strong> button</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":1950,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://cloudnweb.dev/wp-content/uploads/2019/12/Screenshot-2019-12-21-at-9.35.33-AM-1024x389.png" alt="" class="wp-image-1950"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Like we said before, it contains three types of Load Balancers, select Application Load Balancer here,</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":1951,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://cloudnweb.dev/wp-content/uploads/2019/12/Screenshot-2019-12-21-at-9.35.54-AM-1024x525.png" alt="" class="wp-image-1951"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Here, we need to give name for the load balancer and scheme. scheme can be internet-facing and internal. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>internet-facing means it can be accessed by client. internal can be accessed only by other instances.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>After that, add the listener for the load balancers. it can listen for port 80 or some custom port. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Here our load balancer runs on default http port 80. so, add the listener for HTTP Protocol with port 80.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":1952,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://cloudnweb.dev/wp-content/uploads/2019/12/Screenshot-2019-12-21-at-9.37.10-AM-1024x528.png" alt="" class="wp-image-1952"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>After that, configure the security group for load balancer. security group basically sets the inbound and outbound traffic ports for the load balancer. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Here, allow port 3000 which means the load balancer port 3000 will be outbound port to EC2 instance.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":1955,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://cloudnweb.dev/wp-content/uploads/2019/12/Screenshot-2019-12-21-at-9.37.25-AM-1024x526.png" alt="" class="wp-image-1955"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Now, it's time to configure the routing to the specific target group. create a new target group with target type as instance and port 3000.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Also, you can define different route for health check or base route to check the health of an instance.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Above all, we can also configure the advanced health check which have number of checks and time interval between health check etc.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Now, it is time to register the targets to the target groups.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":1959,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://cloudnweb.dev/wp-content/uploads/2019/12/Screenshot-2019-12-21-at-9.41.00-AM-1024x492.png" alt="" class="wp-image-1959"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Add the EC2 instance to the registered group and review the complete configuration once.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Once you complete the configuration and create the load balancer.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":1960,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://cloudnweb.dev/wp-content/uploads/2019/12/Screenshot-2019-12-21-at-9.41.11-AM-1024x521.png" alt="" class="wp-image-1960"/></figure>
<!-- /wp:image -->

<!-- wp:image {"id":1961,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://cloudnweb.dev/wp-content/uploads/2019/12/Screenshot-2019-12-21-at-9.51.18-AM-1024x535.png" alt="" class="wp-image-1961"/></figure>
<!-- /wp:image -->

<!-- wp:heading {"level":3} -->
<h3>Summary</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>This article explains the Elastic Load balancer in detail.  To Sum up, let's see some of the important points  of ELB. A Complete Guide to AWS Elastic Load Balancer using Nodejs.</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li>There are three types of LB. Application,Network and Classic</li><li>Load Balancer checks the health of an instance periodically.</li><li>LB can enable the stickiness in the target groups.</li><li>ALB supports HTTP,HTTPS and websockets protocol.</li><li>NLB is for TCP Protocol and it is very fast.</li><li>application instance don't see the client ip address. although, you can access the IP in <strong>X-Forwarded-For </strong>header.</li></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p></p>
<!-- /wp:paragraph -->