Using an Application Load Balancer with a Fargate Service

In the previous module, we created two tasks where both the tasks had different endpoints to access the application. We now want to have a single access point encapsulating the application running on multiple containers. Additionally, we want our load distributed across all the containers evenly. This is where Application Load Balancers are useful.

Replace your application’s existing Index.cshtml code with the following code. This will print the Hostname on the page.

@page
@model IndexModel
@{
    ViewData["Title"] = "Home page";
}

<div class="text-center">   
    <h2> Response from Hostname: @Environment.MachineName</h2>    
</div>

Make sure your application is compiling and you are able to launch it in debug mode locally in Visual Studio.

Next go to your Visual Studio web application project -> right click on the project in Solution Explorer and click Publish Container to AWS...

New ASP.NET Project

Select Deployment Target as Service on an ECS Cluster leaving everything else as it is and click the Next button.

Container as Service

Next on the Launch Configuration screen:

  • Select dojo-cluster. This cluster was created the previous module.
  • Use the security group created by the toolkit in the previous module. Click on Security Groups drop-down to select it.

Launch Config

Next on the Service Configuration screen:

  • We want to Create New service. Name it dojo-service-ALB.
  • We want this Service to launch and manage two tasks. Select 2 for Number of Tasks.
  • We want minimum two tasks all the time hence we will Minimum Healthy Percent to 100 percent.

Service Config

With above settings, click the Next button.

In previous Lab, we created two tasks where both the tasks had separate endpoints to access the application. With Application Load Balancer, we want a single point of access to multiple containers and we want load balancer to distribute user’s requests across multiple container instances evenly.

Next on the Application Load Balancer Configuration screen:

  • Select Configure Application Load Balancer checkbox.
  • Select Create New for Load Balancer. Give it a name dojo-ALB.
  • Keep Listener Port as 80.
  • Select Create New for Target Group and the value as dojo-target-group.

Service Config

Click Next, then Publish.

You can monitor the status of Load Balancer provisioning within Visual Studio.

ALB

This can take a couple of minutes. Once provisioning is completed, the Load Balancer Status would change to ACTIVE and you will see the URL of the load balancer.

ALB Provisioning Done

Click the URL and access the application. Since our service has two tasks, you should see requests are sent to these tasks in round-robin fashion.

Here is the first request.

Request One

Following is the second request. Notice the hostname is different for the second response because ALB will distribute the request on round-robin fashion between two container instances.

Request Two

Conclusion

Amazon ECS service can optionally be configured to use an Application Load Balancer to distribute the traffic evenly across the tasks in a service.