AWS Lambda Pricing in Context - A Comparison to EC2
There are many compelling reasons to consider a Serverless / Lambda-based architecture for your next project: scalability, fault-tolerance, low maintenance cost, high flexibility. But one of the most often-cited is cost.
A typical AWS serverless stack has several cost components: Lambda, API Gateway, DynamoDB, and often S3 & CloudFront. In this post we’ll focus on Lambda.
Lambda pricing is as follows:
- $0.20 per 1 million requests
- $0.00001667 for every GB-second of compute, with every execution rounded up to the nearest 100ms
You can read all the gory details on the AWS Lambda pricing page.
But these numbers can be a little hard to wrap your head around… just how does this pricing compare to EC2? Of course, the answer is all about utilization.
If you have a somewhat low utilization application, it’s not even close: Lambda is dramatically cheaper.
I’ll illustrate that below with two simple examples to give you a sense of what “low utilization” means.
Low Utilization Pricing Comparison: Lambda vs. EC2
Lightweight and low-traffic website
- 10,000 hits/day
- perhaps 200 ms of execution time per hit at 256MB
- → 432,000 requests per month and 2160 GB-sec of compute per month
- → about $0.31/mo
… less than 1/10th the cost of even a t2.nano, the smallest EC2 instance!
Periodic Scheduled Job
- A scheduled job that runs every hour with 1GB RAM for 2 minutes
- → 86,400 GB-sec of compute per month and 720 requests per month
- → $1.44/mo
… still less than 1/3rd of a t2.nano!
(And a t2.nano only gives you 0.5GB of RAM, so this type of job wouldn’t even be possible on it.)
Bigger Workloads - Lambda Breakeven Analysis
The story is less obvious for bigger workloads. One approach here is to look at the break-even utilization rate:
How much compute would your EC2 instance have to do to be cheaper than Lambda for that same workload?
For comparison, we’ll use a typical workhorse instance, the m4.large instance type. It has 2 vCPU and 8 GB RAM and costs $0.12/hr or approx $86/month in the N. Virginia region.
Lambda Breakeven Analysis for an m4.large Instance
|Function Execution Memory & Time||Requests per Hour Required for Lambda Cost to Equal EC2 Cost||Requests per Second|
|100 ms @ 128 MB||295,000||81.9|
|200 ms @ 512 MB||64,000||17.8|
|200 ms @ 1 GB||34,000||9.4|
|1 sec @ 1 GB||7,100||2.0|
So what does this mean? If a typical transaction in your application takes 100 milliseconds to run and uses 128 MB of RAM, your m4.large instance (with 2 vCPU and 8 GB RAM) would need to be running 82 requests per second, every second of every day, before it is more cost effective than running the same workload on Lambda. Could a single m4.large even handle 82 requests per second? Well of course that depends greatly on the workload, but typically that would be quite a lot.
On the other end of the spectrum, if your typical transaction is long-running (1 second execution time) and needs a full 1 GB of RAM for each transaction, then it would take 2 requests per second, every second of every day, on an m4.large with two vCPUs and 8 GB of RAM total, before EC2 is more cost effective. Again, whether or not that is reasonable depends greatly on your individual workload, but it gives you a sense of how heavily loaded your EC2 instance would need to be before it is more cost effective than Lambda.
This is a simple analysis with a lot of caveats (storage costs for EC2 are ignored, reservation savings are ignored), but it is meant to illustrate the general point… it takes a LOT of compute volume, more than one would typically run on an instance with 2 vCPUs and 8 GB of RAM, before Lambda costs are similar to EC2 costs.
And of course, we’re completely ignoring total cost of ownership here. There is no autoscaling or clustering or load balancing to build & manage, no containers to create and ship, no security patching. So even if Lambda is more expensive than EC2, in some cases, there is still a strong case for using it.
Keep these two points in mind:
- For most periodic or very light workloads, Lambda is dramatically less expensive than even the smallest EC2 instances.
- Focus on the memory and execution time that a typical transaction in your app will need to relate a given instance size to the break-even Lambda cost.
So now get out there and create some Lambda functions! And hit us up on Twitter if you’ve got any good ideas on how to analyze Lambda pricing.
And while you’re here, check out AWS Data Ingestion Cost Comparison: Kinesis, AWS IOT, & S3.