Lambda vs Fargate vs EC2

A simple decision guide for where to run your code.

1 min read
QuestionLambdaECS on FargateEC2
Runs longer than 15 minutes?NoYesYes
Need a specific OS setup or GPU?NoLimitedYes
Want zero server management?YesYes (no hosts)No
Scales to zero when idle?YesPossible, but you manage itNo
Existing Docker app?Possible (container images)IdealPossible

A quick rule of thumb

  1. Event-driven, short tasks? Start with Lambda.
  2. A containerized web app or long-running worker? Use ECS on Fargate.
  3. Need full control (custom kernels, GPUs, licensed software)? Use EC2, or ECS on EC2.

Best practices for ECS

  • Run at least 2 tasks in a service, across multiple Availability Zones.
  • Add health checks so ECS replaces broken tasks automatically.
  • Turn on service auto scaling based on CPU, memory, or requests per target.
  • Use Fargate Spot for interruptible workloads to save money.
  • Keep images small (slim base images, multi-stage builds), so they start faster and have fewer vulnerabilities. ECR can scan images for known vulnerabilities.

Key takeaways

  • Lambda for short event-driven work, Fargate for containers, EC2 for full control.
  • Run 2+ tasks across AZs with health checks and auto scaling.
  • Keep images small and scanned.

Check your understanding

Pick an answer to see the explanation.

  1. 1. You have a Dockerized API that handles steady traffic all day and you don't want to manage servers. Best fit?

Next in “Run a classic web app”
You finished ECS & Fargate. Continue with the Route 53 tutorial