Setting Up AWS ALB with ECS Fargate Cluster(2024)

Karthikeyan mg
5 min readDec 30, 2023

--

Create an Application Load Balancer

  • Open the load balancer page under the EC2 section.
  • Select “Application Load Balancer” as the load balancer type and click “Create.”
  • Provide a name for your load balancer.
  • Under Scheme, choose “Internet-facing.”
  • In the Network Mapping Section, select the VPC where your ECS Fargate Cluster is configured and choose only the public subnets.
  • Note: Ensure your load balancer has access to the internet; hence, select the public subnets created earlier.
  • In my case, I have selected the VPC where I have placed my ECS Cluster and have selected its Public Subnets
  • Select your preferred Security Group. For demonstration purposes, I’ve chosen the default Security Group, allowing all traffic across all ports. However, avoid replicating this setting in a production environment.

Listening and Routing

In this section,

  • Create Target Groups for your services.
  • Specify the ports on which the load balancer listens for requests.
  • Create rules based on received requests to redirect them.

Creating Target Groups

  • In the EC2 section, find “Target Groups” or search directly in the AWS Management Console.
  • Click “Create Target Group.”
Target Groups
  • Under Target Type, select “IP Addresses” (if not already selected).
  • Provide a name for your target group and select protocol “HTTP” with port 80.
  • Choose the VPC where your Load Balancer and ECS Clusters are located.
  • Add the Health Check path, ensuring your API has an endpoint returning HTTP status Code 200 for health checks.
  • Click “Next” to register your targets.
  • In the Network tab, choose the VPC where your load balancer and ECS Cluster are placed.
  • Specify the Port that your service associated with the target group listens to (e.g., port 3000).
  • Also, Click “Remove” for the option where it says “Enter an IPV4 address from a VPC subnet”. So it should look like below
  • Click “Create Target Group.”

Associate Target Group with Load Balancer

  • Associate the target group with your load balancer by clicking the “None Associated” option and choosing “Choose an existing load balancer.”

Verify the Association

  • Open your load balancer and click the “Listeners and Rules” tab to confirm the association.

The above image basically means that load balancer would forward 100% of the requests that it receives on HTTP port 81 to the target group “customer-test-sg”.

The above is how you would setup target group in case of one service. But when you have multiple services, things change

Setting Up Listeners and Rules for Multiple Microservices

  • Create target groups for each microservice and configure the load balancer to forward requests on port 80 to these target groups.
  • Add routing rules to the listener to specify how each endpoint should be handled.

Here the natural question would arise if these are 3 different microservices with 3 different routes, how would the load balancer know which route should it route the current request to? This is where Rules come in

Adding Routing Rules to Listener

  • By adding routing rules to our listener, We can specify precisely how each endpoint should be handled.
  • In my case, customer service contains /api/customers, products service contains /api/products and shopping service contains /api/shopping. I can route based on the endpoint
  • Select your listener and click “Manage Rule” and “Add Rule.”
  • Provide a name for your rule and click “Next.”
  • Specify the condition based on which you would like to route requests (e.g., endpoints containing /api/customer to TargetGroupCustomer).
  • Click Next and associate it with your target group.
  • In the next step, add a priority number (a smaller number indicates higher priority) and click “Create.”

Now, you have successfully configured your load balancer to act as a reverse proxy listening on PORT 80 and routing requests to different microservices based on endpoint paths.

Conclusion

Likewise, I have routed /api/products/* to TargetGroupProducts and /api/shopping/* to TargetGroupShopping using the same approach

This is how you can set your load balancer to act as reverse proxy listening on PORT 80 and routing requests to different microservices based on the endpoint path. Leave your questions and comments below

--

--