EC2 or AWS Fargate?

There are two major models for how to run your containers on AWS:

  • EC2 (Deploy and manage your own cluster of EC2 instances for running the containers)
  • AWS Fargate (Run containers directly, without any EC2 instances)

Both are completely valid techniques for operating your containers in a scalable and reliable fashion. Which one you pick primarily depends on which factors you want to optimize for.

Pricing

With the EC2 launch type billing is based on the cost of the underlying EC2 instances. This allows you to optimize price by taking advantage of billing models such as spot instances (bid a low price for an instance), or reserved instances (get a flat discount for committing to an instance for a certain time period). However, it is your responsibility to make sure that your containers are densely packed onto instances to get the best use out of them, otherwise you will be wasting money.

With the AWS Fargate launch type billing is based on how many CPU cores, and gigabytes of memory your task requires, per second. You only ever pay for what your task uses, no more paying for EC2 capacity that goes unused.

Use Cases

Large workload, optimized for price

If your workload has a consistent demand for many CPU cores and many gigabytes of memory, and you want to optimize for price you should consider running a cluster of reserved EC2 instances, or spot instances. You will be responsible for maintaining this cluster and optimizing it, but you will be able to take advantage of EC2 instance saving strategies such as spot instances or reserved instances.

Large workload, optimized for low overhead

Managing a large cluster of EC2 instances can be somewhat hard. You need to make sure they are all patched, secure, and updated to the latest version of Docker and the ECS agent. If you don’t want to deal with any of this overhead AWS Fargate can be a great choice. For example when the Spectre / Meltdown vulnerability was announced customers that were running on EC2 had to make sure they patched and upgraded, while customers running AWS Fargate were protected automatically behind the scenes by AWS engineers who patched the underlying infrastructure.

Small workload, with occasional bursts

If your workload is small with the occasional burst, such as a website that has traffic during the day but low traffic at night, then AWS Fargate is a fantastic choice. You can scale down to one tiny container at night, costing very little, but still scale up during the day, while only paying for the CPU cores, and gigabytes of memory that your task requires.

Tiny workload

For a small test environment AWS Fargate is a perfect fit. It’s generally wasteful to run a tiny test environment on an EC2 instance because the EC2 instance is too powerful, and you will have a hard time getting a good percentage of utilization.

Batch workloads

If your workload consists of periodic tasks, such as a cron job that runs once an hour, or occasional jobs that come from a queue then AWS Fargate is a perfect fit. Instead of paying for an EC2 instance, and having to start and stop it between uses you can just ask AWS Fargate to run your container when you need to, and stop paying when your container stops.