Lambda vs Fargate vs EC2
A simple decision guide for where to run your code.
1 min read
| Question | Lambda | ECS on Fargate | EC2 |
|---|---|---|---|
| Runs longer than 15 minutes? | No | Yes | Yes |
| Need a specific OS setup or GPU? | No | Limited | Yes |
| Want zero server management? | Yes | Yes (no hosts) | No |
| Scales to zero when idle? | Yes | Possible, but you manage it | No |
| Existing Docker app? | Possible (container images) | Ideal | Possible |
A quick rule of thumb
- Event-driven, short tasks? Start with Lambda.
- A containerized web app or long-running worker? Use ECS on Fargate.
- 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. 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