EC2 vs Lambda

When to run long-lived servers versus event-driven functions.

Overview

What is being compared

EC2 gives you a virtual machine that runs continuously and suits long-lived server processes. Lambda runs your code only when something triggers it and charges for each invocation's duration.

  • Runtime model — Always-on virtual machine — Runs only when triggered
  • Best-fit load — Predictable, steady throughput — Spiky or event-driven traffic
  • Control — Custom OS, kernel parameters, file system mounts — No servers to configure
  • Hardware — GPU and specialized hardware — No GPU option
  • Operations — You manage AMIs, patching and Auto Scaling Groups — Zero server management
  • Scaling — You configure it — Automatic, to thousands of concurrent executions
  • Pricing — Cost-optimized at scale with Reserved Instances — Pay per request, 1 million free requests per month
  • Integrations — Whatever you install — Native triggers from S3, DynamoDB, SQS and API Gateway

Who owns each job

Both run your code on AWS hardware. What changes is how much of the server is yours to run. Green is AWS, yellow is you.

> [!NOTE] EC2 bills for the hours the instance is up whether or not it is busy; Lambda bills only while your code runs.

> [!TIP] Both architectures side by side, a cold and a warm invocation, and the choice between them play step by step in Visual Explanation below.

Sign up free to read the full topic

More Comparisons topics

Start free on Cariara · Coding problems · System design studio