AWS Compute Comparison

EC2 vs Lambda: When to Use Each AWS Compute Service

A first-principles guide to choosing between EC2 and Lambda — covering cost crossover, architectural trade-offs, and the scenarios where each service wins.

Choosing between EC2 and Lambda is not a question of which service is better. It is a question of which execution model fits your workload. Getting this wrong leads to either over-engineered serverless architectures that fight Lambda’s constraints, or underutilized EC2 instances paying for idle capacity around the clock.

This comparison is written for CTOs and cloud architects who need a clear, quantified answer — not a feature checklist.

The Core Distinction: Execution Model

The fundamental difference between EC2 and Lambda is not the service itself but the billing and execution model.

EC2 provisions capacity. You pay for the instance whether it is processing requests or sitting idle. You control the OS, runtime, memory, CPU, and storage. The instance persists until you terminate it.

Lambda executes code. You pay only for the milliseconds your function runs. AWS manages the underlying infrastructure entirely. The execution environment is ephemeral — it may be reused across invocations or discarded after a single call.

This distinction has cascading implications for cost, architecture, operations, and developer experience.

Cost Comparison

Lambda pricing has three components: request count ($0.20 per 1 million requests), duration ($0.0000166667 per GB-second), and optional provisioned concurrency. EC2 pricing depends on instance type, commitment level, and operating system.

ScenarioLambda Cost / MonthEC2 EquivalentEC2 Cost / Month
5M requests, 500ms avg, 512 MB~$21t3.micro (on-demand)~$8
50M requests, 500ms avg, 512 MB~$210t3.small (on-demand)~$15
500M requests, 500ms avg, 1 GB~$4,200t3.large (1-yr reserved)~$50
Continuous 24/7 processing, 4 GB~$14,400r6g.large (1-yr reserved)~$150

The cost story is clear: Lambda wins decisively at low-to-moderate volumes where idle time would otherwise dominate EC2 costs. At high, continuous volumes, EC2 reserved instances are dramatically cheaper.

The crossover point for most workloads falls around 1 million compute-seconds per month — roughly 11.5 continuous days of single-CPU execution. If your Lambda functions aggregate to more than that, run the numbers against a reserved EC2 instance.

One cost frequently overlooked: operational overhead. EC2 requires OS patching, AMI management, auto-scaling configuration, and capacity planning. Lambda eliminates all of that. For small engineering teams, the labor savings often justify Lambda even above the theoretical EC2 cost threshold.

Hard Limits: Where Lambda Cannot Go

Lambda has architectural constraints that make it unsuitable for certain workloads. These are not soft limits you can tune — they are hard ceilings.

ConstraintLambda LimitEC2 Equivalent
Maximum execution duration15 minutesNo limit
Maximum memory10 GBInstances up to 24 TB (u-24tb1)
Maximum vCPUs (proportional to memory)~6 vCPUs (at 10 GB)Up to 448 vCPUs (u-24tb1)
Ephemeral storage (/tmp)512 MB – 10 GBInstance store up to tens of TB
Persistent local filesystemNot supportedFull OS filesystem
Default concurrent executions1,000 per accountNo concurrency ceiling
Cold start latency100 ms – 3 s (runtime-dependent)None (persistent process)

When EC2 is the only viable option:

Architectural Patterns by Use Case

APIs and Web Services

Lambda is the default choice for new REST and GraphQL APIs via API Gateway or ALB. Cold start latency is manageable with provisioned concurrency for latency-sensitive paths. Lambda scales automatically to tens of thousands of concurrent requests without pre-provisioning.

Use EC2 (or ECS/Fargate) when the API has persistent connection requirements (WebSockets, gRPC streaming), requires session-level state, or has latency SLAs under 50 ms that cannot tolerate cold starts even with provisioned concurrency.

Batch Processing

Lambda handles short-to-medium batch jobs well: file processing triggered by S3 events, SQS queue consumers, and scheduled aggregation jobs under 15 minutes. For long-running batch, AWS Batch on EC2 or Fargate is purpose-built and significantly cheaper at scale than Lambda.

Event-Driven Pipelines

Lambda is purpose-built for event-driven architectures. Native integrations with S3, SQS, SNS, DynamoDB Streams, Kinesis, and EventBridge mean you can wire up complex data pipelines with minimal code. EC2 can consume these same events but requires you to run and manage the polling process yourself.

Machine Learning Inference

Lambda supports inference for small, fast models (scikit-learn, lightweight ONNX, AWS Lambda layers with ML runtimes). For larger models (LLMs, large PyTorch models), EC2 with GPU or AWS Inferentia instances is required. SageMaker Inference endpoints are also worth considering — they abstract instance management while supporting model sizes Lambda cannot handle.

Decision Framework

Use this checklist to make the call:

QuestionLambdaEC2
Execution duration under 15 minutes?YesNo
Memory requirement under 10 GB?YesNo
Stateless or externalized state?YesNo
Triggered by events (S3, SQS, API, schedule)?YesNo
Continuous 24/7 processing?NoYes
Requires OS-level access?NoYes
Licensed software (Oracle, SQL Server)?NoYes
GPU or accelerated compute needed?NoYes
Cost-optimized at millions of requests/day?DependsDepends

If you answered “Yes” to the first four questions and “No” to the bottom five, Lambda is the right tool. If any of the bottom five conditions apply, EC2 (or Fargate for containerized workloads) is the more appropriate choice.

Migration Path: EC2 to Lambda

The most successful EC2-to-Lambda migrations follow a pattern:

  1. Audit for stateless candidates — identify EC2 workloads with short, bounded execution times and no local filesystem dependencies.
  2. Externalize state — move session data to DynamoDB or ElastiCache, move file I/O to S3.
  3. Break long jobs — decompose long-running processes into Lambda-sized chunks orchestrated by Step Functions.
  4. Containerize first — if refactoring is expensive, containerize the existing workload and deploy it as a Lambda container image (up to 10 GB) as an intermediate step.
  5. Validate cold starts — measure p99 latency in production and add provisioned concurrency if needed.

Most teams find that 60–70% of their EC2 workloads can be migrated to Lambda. The remaining 30–40% genuinely belong on EC2 or Fargate.

Getting the Architecture Right

The EC2-vs-Lambda decision is rarely a one-time choice. Production environments typically run both: Lambda for event-driven and API workloads, EC2 or Fargate for stateful services and long-running batch.

If you are evaluating a migration or designing a new architecture and want an objective assessment of where Lambda fits in your specific workload mix, our AWS architects are available to help. We have migrated dozens of EC2-heavy architectures to hybrid Lambda/EC2 designs and can give you an honest cost and complexity estimate before you commit to a direction.

Frequently Asked Questions

Is Lambda cheaper than EC2?

Lambda is cheaper at low-to-moderate compute volumes. The crossover point is approximately 1 million compute-seconds per month, which maps to around 11.5 days of continuous single-vCPU execution. Below that threshold, Lambda eliminates idle-time costs entirely. Above it — particularly for workloads running continuously — a reserved EC2 instance with a 1-year commitment is typically 50-70% cheaper than equivalent Lambda execution hours. The comparison must also include the cost of the developer time and infrastructure management Lambda eliminates.

When should I use EC2 instead of Lambda?

Use EC2 when your workload runs continuously, requires more than 10 GB of memory or 6 vCPUs, exceeds Lambda's 15-minute execution timeout, needs direct access to the instance OS or filesystem, or requires stateful long-running processes. EC2 is also the right choice for licensed software that charges per-host (Oracle, SQL Server), GPU-accelerated workloads (ML training, video encoding), and scenarios where cold start latency is unacceptable and cannot be mitigated with provisioned concurrency.

What are Lambda's limitations vs EC2?

Lambda has hard limits that EC2 does not: 15-minute maximum execution timeout, 10 GB maximum memory (with proportional vCPU allocation), 512 MB to 10 GB ephemeral /tmp storage, no persistent local filesystem, 1,000 concurrent executions per account by default (can be raised), and a deployment package size limit of 250 MB unzipped (or 10 GB as a container image). Lambda also has cold start latency — typically 100–500 ms for runtimes like Node.js or Python, and 1–3 seconds for Java or .NET — which matters for latency-sensitive APIs.

Can Lambda replace EC2?

Lambda can replace EC2 for event-driven, stateless, short-duration workloads — APIs, webhooks, data transformation, async processing, scheduled jobs under 15 minutes. It cannot replace EC2 for long-running processes, stateful applications, workloads with high memory requirements, or software that requires OS-level access. A common architecture is to use Lambda for the majority of application logic and EC2 (or Fargate) for the subset of workloads that exceed Lambda's constraints.

How do I migrate from EC2 to Lambda?

Start by identifying stateless, short-duration workloads on EC2 — these are the best Lambda candidates. Common first migrations include scheduled cron jobs, image resizing pipelines, webhook receivers, and lightweight APIs. Refactor to remove local filesystem dependencies (use S3 or EFS instead), externalize state (DynamoDB, ElastiCache), and break long-running processes into step-function-orchestrated Lambda chains. Use AWS Lambda Powertools to add structured logging and tracing. Expect the migration to expose hidden assumptions about state and execution duration baked into the original EC2 code.

Need Help Choosing the Right Cloud Platform?

Our AWS-certified architects help you evaluate cloud platforms based on your specific requirements, workloads, and business goals.