Containers

Using AWS Application Load Balancer path-based routing to combine Amazon ECS launch types

AWS container services offer broad choice and flexibility of tools to run containers. This provides customers with the flexibility they need to select the right platform for their workloads. Amazon Elastic Container Service (Amazon ECS) is a fully managed container orchestration service that enables you to deploy, manage, and scale containerized applications. Customers often ask for help when choosing between the AWS Fargate vs EC2 launch types for the hosting layer of Amazon ECS. Comparing all features in detail and working backwards from application requirements will help to choose the right one for specific needs. While it’s reasonable to do an in-depth analysis for each specific use case, this could be a time-consuming and repetitive task for rapidly changing environments. Ideally, those efforts shouldn’t surpass the main purpose of building modern, scalable, and agile applications. This isn’t a one-way decision where you are stuck with either one of the services or the other. You can always use a mix of different options and combine a rich set of tools that will serve our business objectives.

Amazon ECS supports three different launch types as Fargate, EC2, and External. The Fargate launch type can be used to run your containerized applications without the need to provision and manage the backend infrastructure. It is the serverless way to host your Amazon ECS workloads. The EC2 launch type is used to run your containerized applications on Amazon Elastic Compute Cloud (EC2) instances. We’ve also recently announced the External launch type for Amazon ECS Anywhere. It allows customers to run their containers on their own on-premises servers. In this post, we will first focus on differences between Fargate and EC2 launch types in order to have a high-level overview on where each launch type could be a better fit. Then we will see how path-based routing on an Application Load Balancer enables customers to use both options together.

The EC2 launch type lets you to run containers on a fleet of Amazon Elastic Compute Cloud (EC2) that you register to your Amazon ECS cluster and manage yourself. It supports using different EC2 instance types and customized AMIs for the configuration of your infrastructure. This helps customers with their specific compliance and governance requirements as well as broader customization needs (CPU, memory, GPU etc.). You are responsible for patches, security updates, networking, and environment security of the EC2 fleet. In addition to service auto scaling for the desired count of tasks, customers are in charge of EC2 Auto Scaling Groups (ASG) for the desired number of instances. ECS Cluster Auto Scaling (CAS) is used to manage the infrastructure the tasks in your clusters run on. Amazon ECS on EC2 capacity providers is a capability for ECS to manage the scaling of EC2 Auto Scaling Groups (ASG) automatically. Amazon ECS with EC2 pricing is based on the cost of the underlying fleet of EC2 instances. It is your responsibility to make sure that your containers are densely packed onto instances to make the most usage out of them.

On the other hand, the Fargate launch type lets you leave the heavy lifting of provisioning and managing the backend infrastructure to AWS. It provides a serverless approach so you are no longer responsible for managing, patching, securing, or updating an EC2 fleet. Customers will only focus on service auto scaling while cluster auto scaling is seamlessly managed by Fargate. Fargate capacity providers enable you to use both Fargate and Fargate Spot capacity with your Amazon ECS tasks. Amazon ECS with Fargate pricing is based on how many CPU cores and gigabytes of memory your task requires, per second. You only pay for what your running tasks use and don’t have to deal with any unused EC2 capacity. All those features help customers to reduce the time and effort they spend on maintenance and management.

There are also cases where customers might need to combine two launch types in order to meet their specific requirements. For example, in the same environment there may be applications with highly unpredictable resource utilization as well as others that require direct control and customization of the underlying host. In another example, you might want to leave heavy lifting of maintaining the backend to AWS while also utilizing some of your previously purchased existing reserved instances in the same ECS cluster. For such cases, Application Load Balancer (ALB) helps you to easily mix and match Fargate and EC2 launch types. ALB provides advanced routing and visibility features targeted at application architectures, including microservices and containers. You can create a listener with rules that forward requests to target groups based on URL. Path-based routing enables customers to use microservices running on Fargate in conjunction with other microservices running on EC2.

Architecture:

In our hypothetical case, our webpage has two microservices with different purposes. The first one is built for an unpredictable workload on the landing page of our web application. After a campaign period, we’re expecting an occasional burst and traffic spikes. We don’t have enough data to estimate resource utilization. Besides, a high focus on application and UI leaves limited time for manually maintaining and autoscaling of the cluster. In our design, we will route specific requests for this page to an ECS target group with the Fargate launch type.

The second application is only for subscribed members of our webpage. We’re keeping track of resource utilization per user and expecting predictable workloads at higher scales. This data helps us for not leaving any idle EC2 capacity. We need to install a proprietary software to the host that requires GPU for high quality streaming. In our design, we will route specific requests for this page to an ECS target group with the EC2 launch type.

Prerequisites:

For this tutorial, you should have the following prerequisites:

  • An AWS account, AWS CLI, and ECS CLI
  • Familiarity with foundational AWS services as well as a strong understanding of container technologies and Amazon ECS.

Tutorial:

1. If image doesn’t work, use this alternative link

  • Leave the settings as default
  • Acknowledge that AWS CloudFormation might create IAM resources

This CloudFormation Stack will provision:

  • A VPC with two public subnets and two security groups.
  • IAM roles to be used by the ECS cluster, EC2 instance, and ECS tasks.
  • ECS cluster that will support both EC2 and Fargate launch types
  • EC2 Auto Scaling Groups (ASG) with a desired capacity of one. It is integrated with an ECS capacity provider to enable automated ECS cluster auto scaling.
  • Application Load Balancer with URL path parameters and target groups:
    • /ec2/* path will be routed to a target group with the EC2 launch type
    • /fargate/* path will be routed to a target group with the Fargate launch type

Output for the URL of the Application Load Balancer and paths for target groups:

2. Download and register task definitions:

  • Download the sample EC2 task definition, update the JSON with your <AWS_ID>
    • We’re using DockerHub as repository.
    • For the EC2 launch type the allowable network mode depends on the underlying EC2 instance’s operating system. If Linux is the operating system, ‘awsvpc’, ‘bridge’, ‘host’, and ‘none’ mode can be used. If Windows is the operating system, only the NAT mode is allowed
    • We’re using bridge mode and 0:80 to enable dynamic port mapping
  • Download the sample Fargate task definition, update the JSON with your <AWS_ID>
    • We’re using DockerHub as repository.
    • For the Fargate launch type, the ‘awsvpc’ is the only network mode supported.
    • We’re using port 80 for the elastic network interface (ENI) provided.
  • Register both task definitions through the ECS CLI:
$ aws ecs register-task-definition --cli-input-json file://TD-nodejs-EC2.json
$ aws ecs register-task-definition --cli-input-json file://TD-nodejs-Fargate.json

3. Create services

  • Navigate to Amazon ECS in the AWS Management Console and select ECS cluster with the name “aws-alb-for-ecs-launch-types” and click Create under the Services tab.

  • Configure the service for the EC2 launch type
    • Capacity Provider Strategy: Cluster Default Strategy
      • We are using the capacity provider created by CloudFormation stack for this service. This will enable the integration between EC2 Auto Scaling Groups (ASG) and ECS cluster auto scaling.
    • Task Definition: TD-nodejs-EC2
    • Service Name: ecs-ec2-service
    • Number of Tasks: 2
    • Leave other settings as default and click Next
    • Load Balancer Type: Application Load Balancer
    • Service IAM Role: ECS-Service-Role
    • Container to load balance: nodejs-container-EC2
    • Select the target group provisioned by CloudFormation stack

    • Leave other settings as default then click Next and
    • ecs-ec2-service will create two tasks that are running on an EC2 Instance.
  • Configure the service for the Fargate launch type
    • Click Switch to launch type
      • We’re not using any Fargate capacity providers for this service.
    • Launch Type: Fargate
    • Task Definition: TaskDefinition-Fargate-ALB
    • Service Name: ecs-fargate-service
    • Number of Tasks: 2
    • Leave other settings as default and click next
    • VPC: ECSDemoVPC
    • Subnets: ECSDemoPublicSubnetOne, ECSDemoPublicSubnetTwo
    • Security Groups: ECS-DEMO-Int-SG
    • Load Balancer Type: Application Load Balancer
    • Container to load balance: nodejs-container-EC2

    • Select the target group provisioned by CloudFormation stack
    • Leave other settings as default then click Next and Create.
    • ecs-fargate-service will create two tasks that are running serverlessly.

4. Visit the URL of the Application Load Balancer:

  • ` http://alb-for-ecs-cluster-1813987762.eu-west-1.elb.amazonaws.com/ec2/*` will be routed to containers running on EC2.

  • alb-for-ecs-cluster-613105032.eu-west-1.elb.amazonaws.com/fargate/*` will be routed to containers running on Fargate through elastic network interfaces.

 

Cleanup

To avoid incurring future charges, delete the resources.

  • Delete the ECS services created: ecs-fargate-service and ecs-ec2-service
  • Delete the CloudFormation stack: aws-alb-for-ecs-launch-types
  • Deregister task definitions created: TD-nodejs-EC2 and TD-nodejs-Fargate

Conclusion

In this blog, after walking through a high-level comparison of the Fargate and EC2 launch types, we used Application Load Balancer (ALB) to combine both in the same cluster. While taking into account all the differences between two options, don’t forget that two launch types can also be used together. AWS offers the flexibility to choose right platform for different workloads. The rich set of tools and services we provide enable customers to build modern, scalable and agile applications that will serve your business objectives.

Burak Unuvar

Burak Unuvar

Burak is a Senior Solutions Architect at AWS based in Amsterdam, Netherlands. After working closely with startups and global ISVs for quite some time, most recently he's serving for the AWS partner ecosystem in EMEA.

Ceren Tahtasiz

Ceren Tahtasiz

Ceren Tahtasiz is a Startup Solutions Architect based in London. She helps startups grow by understanding their goals and challenges, and guiding them on how they can get started on AWS. She’s passionate about enabling customers to launch cloud-native products that are resilient and scalable. Her core focus areas are serverless technologies and sustainability in cloud.