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:

  1. Lambda: short, event-driven pieces of code; scales to zero; nothing to patch.
  2. Fargate: long-running services and jobs in containers, any language or runtime, still no servers.
  3. 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

 LambdaFargateEC2
Best forEvent-driven functionsServerless containersVirtual servers
ScalingPer request, to zeroPer task; scale services by metricsAuto Scaling groups of instances
Limits15-minute maximum per invocation; memory up to 10 GBTask sizes up to 32 vCPUInstance types up to very large sizes and GPUs
Operational complexityNothing to patchImages to maintain; no hostsPatch and manage the OS
CostPer request and per GB-secondPer vCPU and GB per secondPer instance-second; commitments and Spot lower it

Limits and constraints

ItemValueContextSource
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

ScenarioWhat happens to cost
Spiky API with long idle periodsLambda: pay nothing when idle.
Steady service at high utilisation all dayEC2 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

SituationChooseWhy
Resize images when they land in S3.LambdaShort and event-driven.
A Java API serving steady traffic around the clock.FargateA long-running service without managing servers.
A GPU-based machine learning inference fleet.EC2GPUs 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

Related

Courses and lessons

Services, comparisons and patterns

Sources

Independent study material, not affiliated with or endorsed by Amazon Web Services.

Last reviewed · By the ByteCamel team · How we write content

Was this page helpful?

Report a mistake