Comparing Amazon ECS launch types: EC2 vs. Fargate

Home Blog Comparing Amazon ECS launch types: EC2 vs. Fargate

Amazon Elastic Container Service (ECS) is a fully managed container orchestration service that enables users to easily run, manage and scale containers on AWS. With ECS, you can deploy containers either on a cluster of Amazon EC2 instances or on AWS Fargate, a serverless computing engine for containers. In this article, we’ll look at how these two launch types compare and explore how to start using them.

What is Amazon ECS?

ECS is highly scalable and powerful for orchestrating containers that are run on Docker. You can use it to deploy containers from your local environment via Docker and provision access to those containers from anywhere, resulting in zero downtime and no complicated configurations. You can also scale numerous containers with just a single click, then collate logs about the container instance to Amazon CloudWatch.

ECS provides the underlying infrastructure to create task definitions for your tasks and services; however, you still have to select a launch type that will handle running, stopping, and deploying your tasks and services.

Amazon ECS Components

To further understand ECS, it’s essential to understand the components involved in deploying a service with ECS. These include:

  • Clusters
  • Task definitions
  • Tasks
  • Services
  • Containers and images
  • Container agents

Let’s look at each briefly to gain a better understanding of how Amazon ECS works.

Clusters

ECS clusters are logical units of core services or tasks. You can deploy containers using task definitions by first configuring a cluster. The type of cluster you configure determines the type of containers you can run in it.

You can deploy multiple clusters if you see the need. This means that for operations, you can create a Production cluster, a Testing cluster, and a Development cluster. Or you can have the clusters based on operating systems—e.g., a Linux cluster for Linux containers and a Windows cluster for Windows containers. You can use the ECS instance menu to scale your cluster up or down, or even delete it when you don’t need it anymore.

Task Definitions

Task definitions come in handy when you need to specify which Docker image should be used by a Docker container, the amount of memory to be provisioned for the container, the launch type to adopt, the network and security group configurations, the commands to run, etc. In essence, definitions control the deployment type. You can run a task definition as a task or service, depending on your preference.

Tasks

Task definitions can be used to define multiple containers. However, if you want to run and/or deploy a single container without advanced features like load balancing etc., you would use a task.

Services

Services, on the other hand, are used to run and/or deploy multiple containers that can have advanced features like load balancing. Services are created via task definitions; they run and maintain the needed number of tasks in parallel.

Containers and Images

Containers and images are synonymous with their counterparts in Docker. Your cluster is configured to run in a container environment, making the container the standard system with everything your application needs to run. Containers are created via templates called images.

Container Agents

Container agents are responsible for connecting container instances to your ECS cluster.

Amazon ECS Launch Types

Launch types, or instance launch types, are hardware environments based on the host computer where your application is deployed. The launch types are responsible for managing the compute resources dedicated to deploying your application. Amazon ECS supports two launch types for running containers, Amazon EC2 and AWS Fargate. 

EC2 Launch Type

The AWS EC2 launch type allows more control but requires more management. When using the EC2 launch type, you still need to specify the underlying infrastructure details, the launch instance, the network security setting, and the auto-scaling group if applicable. Users need to have a deep understanding of their application requirements to be able to determine the right instance type to use, and when to scale them. 

Let’s explore how to create a cluster using the EC2 launch type. Make sure you are set up for Amazon ECS before proceeding. Navigate to the new AWS ECS Console and select “Clusters” in the left navigation bar. Click “Create cluster” on the following page and choose the EC2 Linux + Networking cluster template. Click “Next”:

Figure 1: Selecting a cluster template

Figure 1: Selecting a cluster template

Now, add a name for your cluster. Under “Instance configuration,” select “On-Demand Instance.” You would need to select the t2.large instance type for EC2 whether you select Spot or On-Demand.

You can leave the default settings for the rest of the section, unless you require more than the default values. To define a key pair, click the “Key Pair” dropdown menu and select any of those listed.

Figure 2: Instance configuration for EC2 cluster

Figure 2: Instance configuration for EC2 cluster

Now, under “Networking,” select a VPC in the bar labeled “VPC.” Choose one or two subnets from the Subnets bar and leave everything as is.

For the container instance role, you can leave the default state. If you want to enable CloudWatch for the container, then tick on the “Enable Container Insights” button at the bottom of the page.

Click “Create” to create the cluster instance; it will take just a few minutes:

 

Figure 3: Completed EC2 cluster creation

Figure 3: Completed EC2 cluster creation

If you click “View Cluster,” you should see the details about the cluster running. Clicking the “ECS Instance” status bar will show you the active instance. Remember to turn the cluster off to avoid being billed for it:

Figure 4: ECS EC2 Cluster status: Active

Figure 4: ECS EC2 Cluster status: Active

Fargate Launch Type

The AWS Fargate launch type is the serverless service for running containerized application workloads. You don’t need to worry about the underlying infrastructure; creating, configuring, and scaling clusters; or container host. These configurations are made and managed by AWS, but you have to specify which operating system to use, the CPU and memory capacity needed to package your application, the network security setting, and IAM policies. In terms of cost, you only have to pay for the virtual CPU and memory resources that are consumed in running your tasks.

The AWS Fargate architecture follows the flow in the image below:

Figure 5: The AWS Fargate launch type architecture

Creating an ECS cluster using the Fargate launch type is similar to the EC2 launch type. 

First, visit the AWS ECS management console, then click “Create new cluster.” The difference is that you’re leaving the Infrastructure section as-is, without ticking the Amazon EC2 instances checkbox if you’re using the New ECS Experience, or selecting the Networking only option if you’re using the normal experience:

Figure 6: Creating a new cluster with AWS Fargate launch type

If you view the cluster in AWS CloudFormation, you will see that it has been created:

Figure 7: AWS Fargate cluster completed

Notice that you don’t have to bother with all the networking, patching, and memory allotment; AWS handles everything. If you click through to the “Infrastructure” section, you will see the capacity provider and type is Fargate:

Figure 8: Cluster was provisioned by AWS Fargate

Use Cases for ECS Launch Types

Now, let’s compare the EC2 and Fargate launch types, focusing on when to use each.

When to Use AWS Fargate

AWS Fargate is the recommended deployment launch type in AWS, especially for those just getting started with containers and lack the expertise to manage cloud infrastructure. It abstracts away many complications and decision processes, making it faster and easier compared to EC2. As a result, if you have an unusually light workload—like a test environment—then you should run it on Fargate. Likewise, if your workload is only run in batches, like a cron job, you should also use Fargate.

The benefits of using AWS Fargate for deployments include:

  • Run containers without the overhead of managing and scaling servers and clusters 
  • AWS handles patching and maintenance, as well as security and health of instances
  • Operational efficiency and less over-provisioning of compute as Fargate scales to meet resource requirements of containers 
  • Only charged when container workloads are active, regardless of how long servers are running. 

AWS Fargate does have limitations:

  • The convenience provided by abstracting away management operations makes it more expensive than EC2 clusters. Some reports claim that Fargate is three times more expensive, below a certain threshold.
  • You cannot run DaemonSets and privileged pods, making you unable to run certain types of services on your cluster, and significantly impact the functionality and performance of your application.
  • You cannot specify a particular instance type
  • It has no support for GPU and EBS volumes. If your application requires GPU acceleration and/or the need for a persistent block storage with EBS volume, you will not be able to use Fargate.
  • It does not support classic load balancing
  • It does not support all task definition parameters available on Amazon ECS. Without this feature, you will not be able to customize the environment in which you want to run your task or transfer tasks across different environments.

When to Use AWS EC2

AWS EC2 gives you the ability to control and manage the ECS cluster. It is only recommended when you need more control and the advanced features that it provides. If you wish to provision multiple CPU cores and large memory capacities—with the budget set for these features—then you should consider running the cluster on the AWS EC2 launch type. If you want to run a machine learning model, EC2 is the right choice.

AWS EC2 has the following advantages over Fargate:

  • It is relatively cheaper for running fully utilized clusters, especially if your task consumes a set or predictable amount of vCPU and memory. You can even get started for free with the AWS Free Tier. Furthermore, the spot instances on EC2 are cheaper than the spot instances of Fargate.
  • EC2 clusters are more secure; you don’t need to worry about running sensitive clusters along with arbitrary workloads.
  • EC2 supports GPU acceleration.

The limitations of EC2 include:

  • You manage the cluster yourself, meaning that you must tackle the infrastructure management.
  • You are charged for both used and unused capacities, although there are cost-optimization strategies of which you can take advantage.

Conclusion

The Amazon ECS service launch types have specific ideal use cases, depending on your needs. We explored when best to adopt each, and how to create clusters using your preferred option. We also discussed the use of EC2 for general applications, and Fargate for serverless applications.

If you run a microservice application, adopting an observability tool is important in order to monitor the health and activity in your containerized environment. Lumigo is a great choice for doing so; it is simple to set up and helps everyone that is part of the software development lifecycle by adding observability to their systems.