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

LambdaECS on FargateEC2
You manageCode onlyContainer image and task configOS, patching, scaling, everything
Max run time15 minutes per invocationUnlimitedUnlimited
Scales to zeroYes, automaticallyPossible, but you configure itNo
StartupMilliseconds (warm) to ~1s (cold)Tens of seconds per new taskMinutes per new instance
BillingPer request + per ms of computePer second of vCPU and memory requestedPer second while running
GPUs, custom kernelsNoNoYes
Best forEvent-driven tasks, APIs, glue codeContainerized web apps, workersLong-running servers, special hardware, full control

A three-question decision

  1. Does it run in response to events and finish within 15 minutes? Start with Lambda.
  2. Is it a containerized app or a long-running worker? Use ECS on Fargate.
  3. 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.

Learn the details