Lambda vs Fargate vs EC2: How to Choose AWS Compute
Use Lambda for short event-driven tasks, Fargate for containers without servers, and EC2 when you need full control.
AWS gives you three main ways to run code. They trade control against operational work.
Think of it like this
EC2 is renting an apartment: you furnish and maintain it. Fargate is a serviced apartment: bring your suitcase (container) and move in. Lambda is a hotel room by the minute: show up, use it, leave.
Side-by-side
| Lambda | ECS on Fargate | EC2 | |
|---|---|---|---|
| You manage | Code only | Container image and task config | OS, patching, scaling, everything |
| Max run time | 15 minutes per invocation | Unlimited | Unlimited |
| Scales to zero | Yes, automatically | Possible, but you configure it | No |
| Startup | Milliseconds (warm) to ~1s (cold) | Tens of seconds per new task | Minutes per new instance |
| Billing | Per request + per ms of compute | Per second of vCPU and memory requested | Per second while running |
| GPUs, custom kernels | No | No | Yes |
| Best for | Event-driven tasks, APIs, glue code | Containerized web apps, workers | Long-running servers, special hardware, full control |
A three-question decision
- Does it run in response to events and finish within 15 minutes? Start with Lambda.
- Is it a containerized app or a long-running worker? Use ECS on Fargate.
- Do you need GPUs, a custom OS setup, or licensed software tied to hosts? Use EC2.
Cost rules of thumb
- Spiky or low traffic: Lambda usually wins, because idle time is free.
- Steady, high traffic around the clock: Fargate or EC2 often become cheaper per request.
- Interruptible batch work: Fargate Spot or EC2 Spot cut costs dramatically.
Watch out
Don't pick EC2 just because it's familiar. Every instance is a server you must patch, monitor, and replace. That ongoing work is a real cost, even if it doesn't show up on the bill.
Learn more: Lambda limits & when not to use it, Lambda vs Fargate vs EC2 in the ECS tutorial, and EC2 scaling & pricing.
Key takeaways
- Lambda: event-driven, under 15 minutes, scales to zero.
- Fargate: containers without managing servers.
- EC2: full control, GPUs, and long-running servers, at the cost of more operational work.