> For the complete documentation index, see [llms.txt](https://anon-coders-notes.gitbook.io/techwriterdev/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://anon-coders-notes.gitbook.io/techwriterdev/cloud/aws/developerassociate/services/03_elastic_loadbalancing/01_elb.md).

# Elastic load balancer

## Icon

![ELB Icon](https://icon.icepanel.io/AWS/svg/Networking-Content-Delivery/Elastic-Load-Balancing.svg)

## [Introduction](https://github.com/jojijoy15/Notes/blob/main/Cloud/AWS/CloudPractitioner/03_Elastic_LoadBalancer.md)

* Elastic load balancer is a managed service which can spread load across multiple services.
* It is a **regional service**, making them highly available.
* Exposes a single point of access for DNS to your application.
* AWS takes care of the infra and upgrades of load balancers.
* Provides SSL termination for your websites.
* Offers health checks *(health endpoints)*, to make sure the requests are forwarded only to health instances.
* Can be setup as public or private load balancer depending on the scheme.
* Provides high availability across zones.
* Can enforce stickiness with cookies.

## Target groups

* In general, group of AWS resources which would receive traffic from load balancer.
* Allows grouping of
  * EC2 instances
  * ECS tasks
  * IP addresses *(must be private)*
  * Lambda function
  * Application load balancer
* Read more about it [here](https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-target-groups.html).

## Listener

* A listener is a process that checks for connection requests, using the protocol and port that you configure.
* **The rules that you define for a listener determine how the load balancer routes requests to its registered targets**.
* Read more about it [here](https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-listeners.html).

## Health Checks

* To enable load balancer to know if the instance is healthy and can forward traffic to it, health checks play crucial role.
* Health check is done using `/health` endpoint *(typically)* running on port `4567` using `http` protocol.
* They are done at target group level.
* Health checks need to be configured when creating a target group before registering targets in to the target group.

## Types of Load Balancer

### Classic Load Balancer (CLB)

* Deprecated and [retired](https://aws.amazon.com/blogs/aws/ec2-classic-is-retiring-heres-how-to-prepare/), old generation *(v1)*.
* Supports `HTTP`, `HTTPS`, `TCP`, `SSL`.
* Only supported one SSL certificate for `HTTPS` based listeners.

### Application Load Balancer (ALB)

* Newer generation, 2016.
* Works on layer 7 of OSI model, supports `HTTP/1.x`, `HTTP/2` `HTTPS`, `WebSocket`, `gRPC`.
* Supports redirect from `HTTP` to `HTTPS`.
* Target groups can be,
  * EC2 instances
  * ECS Tasks
  * Lambda Functions
  * Private IP Addresses
* Great fit for microservices and container-based application.
* Supports routing to different target groups based on,
  * Path in URL
  * Hostname in URL
  * Query strings and headers
* Port mapping feature allow to redirect to a dynamic port in ECS.
* The IP address of the client is available in the header `X-Forwarded-For`, the port of the client in header `X-Forwarded-Port`, the protocol in header `X-Forwarded-Proto`.
* Target groups can be EC2 instances, ECS Task, Lambda functions *(requests are translated to `JSON` event)*, Private IP Address.
* **Health checks are done at target group level**.
* ALB as has a fixed hostname of the form `xxx.region.elb.amazonaws.com`.
* Note that ALB has access logs to analyze latencies and client details like IP address patterns.

### Network Load Balancer (NLB)

* Newer generation, 2017.
* Not available in free-tier option.
* Target groups can be,
  * EC2 instances
  * Private IP Addresses
  * ALB.
* Layer 4 load balancer, supports `TCP`, `TLS`, `UDP`.
* High performance, with ultra-lower latency.
* Has one **static IP** per AZ, with ability to assign elastic IP to each AZ.
* Health check supports `TCP`, `HTTP` and `HTTPS`.

### Gateway Load Balancer (GWLB)

* Newer generation, introduced in 2020.
* Operates at network layer 3 i.e Network Layer.
* Target groups can be,
  * EC2 instances
  * Private IP Addresses
* Can be used to deploy, manage and scale a fleet of 3<sup>rd</sup> party virtual appliances which includes Firewalls, IDPS, Deep packet inspection etc.
* Uses `GENEVE` protocol at port `6081`.
* It combines function of transparent network gateway and load. balancer.

## References

* [Working of Load balancer](https://docs.aws.amazon.com/elasticloadbalancing/latest/userguide/how-elastic-load-balancing-works.html)
* [ALB v/s NLB](https://blog.cloudcraft.co/alb-vs-nlb-which-aws-load-balancer-fits-your-needs/)
