Setup Application Load Balancer and Point to ECS — Deploy to AWS ECS Fargate with Load Balancer (Last Part)

Sumedh Shakya
6 min readJul 10, 2022

--

In the previous articles, firstly (Part 1) we setup AWS RDS instance and build a backend application’s docker image, secondly (Part 2) we setup AWS ECR and pushed a docker image to it and thirdly (Part 3) we setup AWS ECS cluster, configured Task Definition and Container Definition, configured ECS Cluster Service using Fargate launch type and run the docker container. In this article, we will now be setting up AWS Application Load Balancer and point it to ECS.

What is Load Balancer?

Load balancer is a technology that acts as a reverse proxy and distributes network or application traffice across a number of server. It is designed to distribute the workload between different servers or applications. Its goal is to optimise overall infrastructure performance, efficiency and capacity. Load balancers are generally grouped into two categories: Layer 4 and Layer 7. layer 4 load balancers act upon data found in network and transport layer protocols (IP, TCP, UDP, FTP) whereas Layer 7 load balancers distribute requests based upon data found in application layer protocols such as HTTP.

Requests are received by both types of load balancers and they are distrubuted to a particular server based on a configured algorithm. Some standard algorithms are:

  • Round Robin
  • Weighted round robin
  • Least connections
  • Least response time

What is Application Load Balancer?

Application load balancer(ALB) is Layer 7 load balancer that works on layer 7 i.e. application layer in OSI model. It monitors application-level contents like HTTP, HTTPS and packet details( HTTP headers, cookies or data within application message itself), instead of port and IP and distributes requests based on it. Its unique feature is its ability to conduct health check of applications which helps to find out possible coding and HTTP errors.

ALB comprises rules and listeners. The listener takes or listens to client’s request and rules govern the routing of these requests. Here are 3 crucial components of the application load balancer:

  • Load Balancer: It splits traffic to servers available.
  • Listener: It listens to the incoming traffic by client and decides to apply a rule and send it to the target group accordingly.
  • Target group: It comprises several servers grouped logically called registerd targets. When application load balancer receives a request, it sends to one of registered targets based on the protocol and port number configured.
Fig: How ALB works (Source: AWS)

Setup Application Load Balancer

Let’s get started with setting up an application load balancer in AWS.

  1. Login to AWS console and search for EC2.
  2. Click on Load Balancers and click on Create Load Balancer.

3. Click Create on Application Load Balancer section.

4. Name your load balancer, Select Internet-facing Scheme and IPv4 IP address type.

5. In network mapping section, select VPC that you have selected/created while creating ECS cluster and map Availability Zones.

6. In security group, select/create a security group with Inbound rules enabling Port 80 from Anywhere Source. Here, I have created a security group named ecs-alb-loadbalancer-SG.

7. In listeners and routing section, select HTTP as protocol and input port 80. Then click on create target group and open on new tab.

8. In Basic configuration, choose IP address as target type and name target group and leave the rest as default.

9. In health checks, input your backend application health check API.

10. Click on Next button.

11. In register targets section, select the network on which you have created your ECS cluster. For now, remove IPv4 address and specify port on which your ECS Task is running. In my case, it’s 8000.

12. Click on Create target group.

13. Go to your Create Load Balancer page’s listeners and routing section, press refresh button and select your target group.

14. Click Create Load Balancer.

Point ALB to ECS

After you’ve successfully spinned up your load balancer, now point the application load balancer to ECS.

  1. Search ECS, click on your ECS cluster.
  2. An existing can not be updated and configure load balancing in it so, create a service for the cluster. Follow my previous article, to configure ECS service.
  3. Except in Step 2: Configure Network
    i. Select previously created Security Group if exists else create a new one with inbound rule open for port as defined in Container Definition while configuring Task Definition. In my case, it’s 8000.ii. In Load Balancing section, select Application Load Balancer as load balancer type and select load balancer name by clicking it from dropdown.

iii. In Container to load balance section, click Add to load balancer button.

iv. Select Production listener port from dropdown and select your target group from Target Group Name dropdown.

You can follow next steps as in previous article.

Here you’ve now successfully pointed Application Load Balancer to ECS. Once you’ve the tasks in RUNNING status, go to load balancer page in EC2 service. Select your load balancer, there you’ll find DNS name.

This DNS name is what you will add as A record while binding it to a domain/subdomain. Copy the DNS name and paste in your browser’s address bar. I will open up API documentation page with DNS name and request health-check API.

Congratulations, you’ve successfully deployed your backend application to AWS ECS using Fargate with Application Load Balancer.

If you have any suggestions for improvements or some better way to enhance, feel free to comment below or send me an email at sumedhshakya11@gmail.com

Thanks for reading! Hope this was helpful.

--

--