Choose between EC2, Lambda and Fargate using traffic shape, state and runtime limits, with worked cost break-evens instead of rules of thumb.
Picking the wrong AWS compute option rarely breaks a system outright. It quietly costs you money, adds cold-start latency, or leaves your team patching servers they never wanted. The choice also tends to stick, because moving a workload later means changing deployment, networking and monitoring.
The scenarios below are illustrative, not descriptions of any company's real architecture. Prices are us-east-1 list prices as last checked, so confirm them in the AWS Pricing Calculator before deciding.
Choose by workload shape. Use Lambda for short, event-driven work with uneven traffic, Fargate for containers that should run continuously without host management, and EC2 for stateful, specialised or very high-utilisation workloads. Most production systems end up using all three.
This tree is a starting point. Traffic level, state and cost can move you off it, as the sections below show.
You pay for different units. EC2 bills for instance time whether or not it is busy, Fargate bills for the vCPU and memory you reserve while a task runs, and Lambda bills for requests plus GB-seconds of execution.
| Service | You manage | You pay for |
|---|---|---|
| EC2 | OS, patching, scaling | Instance hours |
| Fargate | Image, CPU, memory | vCPU and GB per second |
| Lambda | Code and trigger | Requests and GB-seconds |
Lambda's list price is $0.20 per million requests plus $0.0000166667 per GB-second on x86 (arm64 is cheaper). Fargate on Linux x86 lists at $0.04048 per vCPU-hour and $0.004445 per GB-hour. Since August 2025 Lambda also bills the init phase of functions, so heavy start-up code now costs money as well as latency.
EC2 is right when the process is long-lived, stateful or needs specific hardware. Think of an in-memory order-matching service, a Redis or Kafka cluster, or GPU inference. These workloads cannot tolerate recycling or cold starts, and steady utilisation makes commitments pay off.
EC2 fits when:
The common mistake is defaulting to EC2 out of habit. An instance sitting at 3% CPU for 18 hours a day is mostly an expensive idle machine.
Lambda is right when work arrives in bursts, finishes quickly and is triggered by an event. File uploads, queue messages, webhooks, scheduled jobs and glue between AWS services all fit, and you pay nothing while idle.
Consider a streaming platform that must fan out millions of notifications and watch-history updates when a live match ends, then goes quiet. Each event takes a few hundred milliseconds. Lambda can add up to 1,000 concurrent executions every 10 seconds per function, subject to account quotas, so it absorbs the burst without capacity planning.
Lambda fits when:
It struggles when functions run constantly, need GPUs or hold long-lived connections.
Compute the per-invocation cost and compare it with the monthly cost of an always-on alternative. The calculation is simple enough to script:
## Lambda monthly cost, x86, us-east-1 list pricedef lambda_cost(req, ms, mem_gb): gb_s = req * (ms / 1000) * mem_gb return gb_s * 0.0000166667 + req / 1e6 * 0.20 print(lambda_cost(30_000_000, 300, 0.25))That prints about $43.50 for 1 million invocations a day (30 million a month) at 256 MB and 300 ms, before the free tier. A t3.small costs about $0.0208 per hour, roughly $15 a month, so at this volume Lambda is about three times more expensive, not cheaper.
The break-even is simple to state. One invocation costs about $0.00000145, so a $15 instance breaks even near 10 million invocations a month, or about 4 requests per second sustained. For high availability you need two instances, which moves the break-even to roughly 8 requests per second. Below that, Lambda is usually cheaper and simpler.
Fargate is right for containerised services that should run continuously without host management. There is no 15-minute limit, no OS patching, and you pay per second for the vCPU and memory you reserve.
A small always-on service with 0.5 vCPU and 1 GB costs about $18 a month per task, or about $36 for two tasks across zones. Fargate fits when:
Fargate Spot discounts interruption-tolerant tasks by up to about 70%. Fargate is a weaker fit for sudden spikes from zero, because a new task needs to provision and pull its image, often tens of seconds to a minute.
The cheapest option changes with traffic, so model at least three scenarios. The figures below use stated assumptions and rough list prices, so replace them with your own numbers.
| Scenario | Lambda | Container or EC2 |
|---|---|---|
| 2 req/s steady | About $23/mo | About $30-36/mo (two tasks) |
| 50 req/s steady | About $565/mo | About $90-145/mo |
| 1,000 req/s for 2 hrs weekly | About $135/mo | Cheaper if pre-scaled |
Assumptions: 512 MB and 500 ms per request on Lambda, and 4 vCPU-class tasks or two c6i.large instances for the 50 req/s row. The burst row is the instructive one. If you can schedule capacity before the spike, containers cost less. If the spike is unpredictable, Lambda's instant scaling is worth the premium.
Initialise expensive objects outside the handler, then pick the right mitigation for your runtime. Python and Node.js functions usually start in 100 to 500 ms, while large Java or .NET functions can take several seconds without help.
import boto3 ## runs once per execution environment, reused afterwardss3 = boto3.client("s3") def handler(event, context): return s3.head_object(Bucket="orders", Key=event["key"])Mitigation options, in order of effort:
Provisioned Concurrency bills for the provisioned capacity whether or not it serves traffic, so schedule it to business hours:
aws lambda put-provisioned-concurrency-config \ --function-name pay-api --qualifier live \ --provisioned-concurrent-executions 20 aws application-autoscaling register-scalable-target \ --service-namespace lambda \ --resource-id function:pay-api:live \ --scalable-dimension lambda:function:ProvisionedConcurrency \ --min-capacity 2 --max-capacity 40| Concern | EC2 | Fargate |
|---|---|---|
| Operations burden | High | Low |
| Cost at low traffic | Worst | Medium |
| Cost at steady high load | Best with commitments | Medium |
| Concern | Lambda | Note |
|---|---|---|
| Operations burden | Near zero | No servers |
| Cost at low traffic | Best | Zero when idle |
| Cost at steady high load | Worst | Per-request billing |
ECS on EC2 and EKS sit between these options. They give container workflows with host-level control, which suits GPU, daemon and high-utilisation workloads.
References and Further Reading
- AWS Lambda Pricing - Request, duration and Provisioned Concurrency rates
- AWS Fargate Pricing - Per-vCPU and per-GB rates
- Lambda SnapStart - Supported runtimes and behaviour
- AWS Savings Plans - Commitment options for EC2 and Fargate
For a 256 MB function running 300 ms, Lambda costs about $1.45 per million invocations at us-east-1 x86 list prices, so a t3.small at about $15 a month breaks even near 10 million invocations a month (about 4 requests per second sustained). Above that, containers or instances usually win, though high availability and operations time shift the real break-even.
Execution environments are ephemeral and can be recycled at any time, and each invocation is capped at 15 minutes. State that takes minutes to rebuild cannot live in a function, so keep it in a long-running process on EC2 or a container service.
Not on its own. A new task needs time to provision and pull its image, often tens of seconds to a minute, so sudden spikes outrun autoscaling. Lambda can add up to 1,000 concurrent executions every 10 seconds per function (subject to your account quota), which suits unpredictable bursts better.
Use SnapStart when your runtime supports it (Java, Python and .NET) and you want lower init time at no extra provisioning cost. Use Provisioned Concurrency when you need initialised environments ready at a predictable level, and model the cost, because you pay for the provisioned capacity whether it serves traffic or not.
When you need GPUs, specific instance families, privileged or daemon workloads, very high sustained utilisation where Savings Plans beat Fargate's per-vCPU price, or host-level tuning. Fargate trades those options for not patching or sizing hosts.
Discussion0