Understanding AWS Fargate: Serverless Compute for Containers
A guide to AWS Fargate, the serverless compute engine for containers. Learn how Fargate allows you to run containers on ECS and EKS without managing servers, and understand when to choose Fargate over EC2.
Containers have revolutionized how we build and deploy applications, but they haven't completely eliminated operational overhead. With traditional container orchestrators like Amazon ECS or EKS, you are still responsible for managing the underlying cluster of EC2 instances—patching them, scaling them, and ensuring they are secure.
AWS Fargate is a technology that removes this burden. Fargate is a serverless, pay-as-you-go compute engine that lets you run containers without having to manage servers or clusters. It's the bridge between the container world and the serverless world.
The Problem Fargate Solves: Cluster Management
When you run containers on AWS using Amazon ECS (Elastic Container Service) or EKS (Elastic Kubernetes Service), you have two choices for the launch type, which determines how your containers are run:
EC2 Launch Type: You create and manage a cluster of EC2 instances. Your container orchestrator (ECS or EKS) places your containers onto these instances. You are responsible for the EC2 instances.
Fargate Launch Type: You don't manage any EC2 instances. You just define your application, specify the CPU and memory it needs, and Fargate launches and runs the containers for you on a fleet of infrastructure that AWS manages.
With Fargate, you no longer have to think about servers. You don't have to provision, scale, or patch virtual machines. You just focus on your application.
How Fargate Works
When you use Fargate, you package your application in a container, specify the CPU and memory requirements, define your networking and IAM policies, and launch it. Fargate handles everything else.
Here's the basic workflow with Amazon ECS:
Create a Task Definition: This is a JSON file that describes your application. It includes things like the Docker image to use, the CPU and memory to allocate, and the ports to open.
Create a Cluster: A Fargate cluster is just a logical grouping for your services. There is no infrastructure to manage.
Create a Service: An ECS service is responsible for running and maintaining a specified number of instances of your task definition. If a task fails, the service will automatically launch a new one.
When you create the service, you specify the Fargate launch type. ECS then works with Fargate to provision the compute capacity and run your containers.
Fargate vs. EC2 Launch Type: When to Use Which?
Choosing between Fargate and the EC2 launch type is a trade-off between control and convenience.
Use AWS Fargate when:
- You want to minimize operational overhead: This is the primary reason to choose Fargate. You want to focus on your application, not on managing servers.
- Your application has spiky or unpredictable traffic: Fargate can scale quickly without you having to worry about adding more instances to your cluster.
- You have small to medium-sized workloads: Fargate is a great fit for most general-purpose applications.
Use the EC2 Launch Type when:
- You need maximum control: You need to run custom daemons on your instances, require specific GPU acceleration, or have other special requirements for the underlying host.
- You have very high, sustained workloads: For very large, stable workloads, you may be able to achieve better cost optimization by carefully managing a fleet of EC2 instances with Reserved Instances or Savings Plans.
- You need access to persistent storage on the host: Fargate tasks have ephemeral storage. For persistent storage, you need to use a service like Amazon EFS.
Fargate vs. Lambda
Fargate and AWS Lambda are both serverless compute services, but they are designed for different use cases.
AWS Lambda is a function-as-a-service (FaaS) platform. It's designed to run small, event-triggered functions. It's ideal for short-lived tasks and has a maximum execution time (currently 15 minutes).
AWS Fargate is a container-as-a-service platform. It's designed to run full containerized applications, including long-running services like web servers and APIs. You can run almost any application that can be packaged in a container.
Conclusion
AWS Fargate is a powerful technology that makes it easier than ever to run containers on AWS. By abstracting away the underlying infrastructure, it allows you to get the portability and packaging benefits of containers without the operational complexity of managing a cluster. For a huge range of applications, Fargate provides a sweet spot between the flexibility of containers and the simplicity of serverless.