AWS · Comparison
Lambda vs Fargate vs EC2
Where should my code run: in functions, in serverless containers, or on virtual servers?
Decision summary
Move down this list only when you need to:
- Lambda: short, event-driven pieces of code; scales to zero; nothing to patch.
- Fargate: long-running services and jobs in containers, any language or runtime, still no servers.
- EC2: full control of the operating system, special hardware (GPUs, large memory), licensing, or the lowest cost for large, steady fleets with commitments.
Best fit for each option
Lambda
Event handlers, APIs with spiky traffic, glue code and scheduled tasks under 15 minutes.
Fargate
Containerised web services, workers and jobs that run longer, without managing servers.
EC2
Full control of the OS and runtime, special hardware, legacy and licensed software, and big steady fleets with Savings Plans.
Side by side
| Lambda | Fargate | EC2 | |
|---|---|---|---|
| Best for | Event-driven functions | Serverless containers | Virtual servers |
| Scaling | Per request, to zero | Per task; scale services by metrics | Auto Scaling groups of instances |
| Limits | 15-minute maximum per invocation; memory up to 10 GB | Task sizes up to 32 vCPU | Instance types up to very large sizes and GPUs |
| Operational complexity | Nothing to patch | Images to maintain; no hosts | Patch and manage the OS |
| Cost | Per request and per GB-second | Per vCPU and GB per second | Per instance-second; commitments and Spot lower it |
Limits and constraints
| Item | Value | Context | Source |
|---|---|---|---|
| Lambda function timeout | 900 seconds (15 minutes) Fixed limit |
Source checked |
Quotas and limits change. Check the linked source before designing around a number.
Cost trade-offs by scenario
| Scenario | What happens to cost |
|---|---|
| Spiky API with long idle periods | Lambda: pay nothing when idle. |
| Steady service at high utilisation all day | EC2 or Fargate with Savings Plans usually costs less than Lambda per request. |
Prices differ by Region and change over time; see the pricing pages linked under Sources.
When to avoid each option
Lambda
- Long-running processes, persistent connections, or steady high load where per-request pricing adds up.
Fargate
- GPU workloads, or very large steady fleets that are cheaper on packed EC2.
EC2
- Small, spiky workloads where you would pay for idle servers and patch them.
Example scenarios
| Situation | Choose | Why |
|---|---|---|
| Resize images when they land in S3. | Lambda | Short and event-driven. |
| A Java API serving steady traffic around the clock. | Fargate | A long-running service without managing servers. |
| A GPU-based machine learning inference fleet. | EC2 | GPUs and control over the host. |
Exam cues
- Runs longer than 15 minutes: not Lambda; use Fargate, Batch or EC2.
- Least operational overhead: Lambda, then Fargate.
- Full OS control or GPUs: EC2.
Service pages
- AWS Lambda · Run code in response to events without managing servers, paying only for the requests and the time your code runs.
- AWS Fargate · Serverless compute for containers: run ECS tasks or EKS pods without provisioning or managing servers, paying per vCPU and memory.
- Amazon EC2 · Resizable virtual servers in the cloud, with hundreds of instance types and several ways to pay: On-Demand, Savings Plans, Reserved and Spot.
Related
Courses and lessons
- AWS Lambda · AWS Solutions Architect – Associate (SAA-C03) · Members
- Containers: ECS, EKS, Fargate and ECR · AWS Solutions Architect – Associate (SAA-C03) · Members
- AWS Batch, Elastic Beanstalk and choosing a compute service · AWS Solutions Architect – Associate (SAA-C03) · Members
Services, comparisons and patterns
- AWS Lambda · Service
- AWS Fargate · Service
- Amazon EC2 · Service
- Amazon ECS · Service
- AWS Batch · Service
- Event-driven architecture · Architecture style
Sources
Independent study material, not affiliated with or endorsed by Amazon Web Services.