ECS — run it on EC2 or Fargate?

Anurag Bapat
Simplilearn Engineering
4 min readFeb 17, 2021

Development teams are moving towards containerized applications deployed on the cloud. On a large scale there is a need to install, operate and scale the infrastructure. Amazon ECS solves this problem by allowing users to configure a cluster of compute resources to run their Docker containers.

But after moving to ECS, we encounter two distinctive types of environments on which the Docker containers can be deployed. We will first understand the high level concepts of this setup and discuss the factors required to choose the best fit for the application.

What is Amazon ECS?

Amazon Elastic Container Service (Amazon ECS) is a highly scalable, fast container management service that makes it easy to run, stop, and manage containers on a cluster.

It allows teams to deploy applications in the cloud without having to configure an environment and maintain servers to run these applications.

ECS is an ideal solution for deploying distributed microservices that require hundreds of containers. The ECS cluster can also be configured to manage scaling of containers based on load. Another use case is running batch workloads e.g. data processing/ML training models which can be triggered or scheduled using the ECS APIs.

Amazon ECS Cluster

Launch Types

ECS supports two launch types that can define how the compute resources are managed :

  • EC2 Launch Type
  • Fargate Launch Type
High level overview

EC2 Launch Type

The traditionally EC2 launch type utilizes self-managed EC2 instances which are registered to the ECS cluster. The user is responsible for managing and provisioning these “Container Instances”, on which containers will be deployed.

Fargate Launch Type

Fargate eliminates the need to manage servers. The user only needs to specify the resource needs of the container. Fargate manages the underlying infrastructure giving a “serverless” experience for container deployment.

Comparison — EC2 vs Fargate

EC2 Deployment vs Fargate Deployment

Server Management

Fargate allows users to focus on building and deploying the application and eliminates the need of managing EC2 instances. Software patches need to be installed manually on the EC2 hosts while Fargate automatically keeps the underlying infrastructure up-to-date.

But with the EC2 launch type, you can specify the instance types and gain more control on the hosts running the containers.

Auto-scaling

Auto-scaling is built into Fargate, the cluster scales based on usage. On the contrary, an auto-scaling group has to be manually configured for the EC2 launch type. Note that Fargate has an upper limit of 4vCPU and maximum 30GB per container (as of February 2021).

Costs

Fargate is priced on the duration of the task, similar to how Lambda billing works. Users only need to pay as per usage. The cost of EC2 launch type depends on the selection of instances and their usage.

Typically, Fargate costs much less

Operational Overhead

Fargate requires a low operational effort as there are fewer moving parts. On the contrary for the EC2 launch type, the user needs to manage EC2 instances and have system administrators for the upkeep and maintenance of the servers.

Vendor lock-in

Fargate is a AWS specific service and would tie up the application to a particular vendor. In case of the EC2 launch type, the user needs to only deploy the containers on any VM available on the alternate compute provider.

Design Considerations — How to choose?

Consistent Traffic

If your application has a consistent workload and the CPU/Memory requirements are quantified — running a cluster of EC2 instances covered by Reserved Instances/Savings plan or Spot instances will be more cost effective than using Fargate.

Heavy workloads requiring vCPU and memory

Fargate currently has an upper limit of 4 vCPU units / 30 GB memory on each task. If your workload requires more vCPU or memory then deploy a cluster of EC2 instances.

Compliance Requirements

Fargate abstracts the layer of managing infrastructure and provides a “serverless” experience of running containers. But if there are compliance requirements for running a certain type of instance, then prepare and harden EC2 instances which will be registered to the ECS cluster.

Test Environment

Fargate provides a cost-effective deployment for the test/development environments which have lower requirements and do not need to be in the running state at all times.

Batch processors/workers

Scheduled workloads or ad-hoc processors can be deployed on Fargate instead of incurring charges for a running EC2 instance and managing the start/stop of the server.

Conclusion

Making a choice truly depends on the nature of application and also prioritizing the features of one over the other. It might also lead to a infrastructure deployment which combines both the launch types.

AWS EC2 is the best fit when flexibility is the priority. The cost can be further controlled with reservations and responsive scaling policies.

AWS Fargate lowers the complexity of operations at the cost of customization, but creates a balance between low costs and ease of development.

--

--