AWS Lambda has hidden costs?
Does it turn out to be really expensive once you are out on production?
I know AWS Lambda is cheap, but AWS API Gateway that it couples with, is expensive!
My inspiration to write this stems from the fact of the two extremes of opinions that I generally hear from people (developers / architects / CTO’s), these opinions can be paraphrased into these:
|Serverless drains out your money once in production, the per ms cost that AWS Lambda charges sounds good only on dev, however, on production it can shut down an entire company.||Serverless is so awesome, it costs me less than a dollar to run my entire product with no maintenance at all|
Fundamentally, whenever there is such an extreme divide of opinion the real answer is generally that it depends on the situation.
In this article, I will attempt to answer what the pricing pivots on, explain the pricing model for AWS Lambda, what you should look out for when it comes to pricing. After reading this article, you will be able to make a more educated decision on whether AWS Lambda (or serverless architecture in general) is right for you or not.
What you should consider in costs?
When you are estimating costs related to an architectural choice there are two important factors to consider:
- The raw hosting cost, calculate the estimated traffic, the highest possible and the lowest one, take into account data being transferred
- Calculate the money spent on maintaining those resources, an aspect generally left out of consideration because its hard to quantify. The nature of Serverless is that it either works or it doesn’t, while you may spend time in optimizing stuff just like for any other system, once its running you generally don’t need to care; this statement is from experience.
Types of incoming Traffic behaviors
Traffic behaviors is the fundamental where pricing for AWS Lambda would mostly pivot on, if you have a B2B site, chances are that the usages are sporadic, if you have a site that targets a specific country’s users, you probably get traffic only during the day. International B2B sites or B2C sites would normally have traffic all around the day on production.
How does all this matter?
Example 1 – A Global B2B site
This application is B2B, is globally accessed in all timezones and produces considerable business.
There were a total of 893,599 requests made over a one week period. The latency for the requests were something like this:
The application depends on a lot of third party sourced data, so don’t judge on the slowness.
Average Latency is ~600ms.
This application utilizes two m4.large instances which are always up spread about in two different availability zones in order to give adequate dependability.
Each EC2 has 100GB of EBS attached to it, while no disk operations take place we found that anything less than 100GB of space is a risk for various factors which are out of scope for this article.
Total EC2 cost: USD 130.28. If you want the grimy details on the break up of this cost, see this CSV.
At a high level, it includes costs of the two servers (linux) with 1 year no upfront reservation, two 100GB EBS volumes, one ELB (classic) and EBS snapshot consuming 10GB to maintain the AMI (server image) for the EC2 servers and a real 56.7GB of traffic processed by the ELB which costs less than a dollar.
Now, as an experiment, let’s assume this application is running on AWS Lambda and calculate its costs to compare.
- Assuming the application would more or less behave the same way on Lambda, because of the lower CPU quality in general on Lambda’s I will assume the worst case latency of 700ms instead of the 600ms average that we have with EC2.
- This application uses MySQL as the backend, for the purpose of the experiment, I will assume that is not changing since AWS Lambda can use that anyway. Read best practices around AWS Lambda if you want to know more about what all you should consider before programming for it.
- I will consider a Lambda of 256MB of memory to give it decent compute power as compute power is proportional to the RAM selected in Lambda (again, read the best practices article for more info on this).
- API gateway charges for data transfer out, not in, since I only have the data numbers for data coming in i.e. 56.7GB per month, I will assume it to be 45GB per month of out since I know the nature of the application is to give out more data than it takes in.
- I will assume no free tier for either EC2 nor will I consider the free 1 million calls for AWS Lambda. Mind you AWS Lambda offers the first 1 million 100ms slab for 128MB Lambda free for perpetuity, but for the cautious, since this can go anytime AWS has the mood for it, I will ignore it.
- I have ignore data transfers inside the AWS environment because firstly, it will be the same for both, secondly, the cost is trivial in this example.
|What the cost if for (as of 22/03/2018)||Cost (USD)|
|Price per 100ms of a 256MB Lambda||0.000000417|
|API gateway charges per million calls||3.50|
|API Gateway cost per GB for first 1TB out||0.09|
Total cost for running this app in AWS Lambda: (7 * 0.000000417 * 893,599 calls/mo) + (893,599 * (3.50 / 1000000)) + 45GB * 0.09 = USD 9.78
Now, if we consider the free tier of 160,000 seconds of free lambda executions for the 256MB version of Lambda which is perpetual today, the cost will come out to be USD 7.1 approx
We spent about 200 hours (very rough) of work doing stuff like chef, EC2 image management, tuning the servers etc which we didn’t need to spend if it were to be Serverless. I am not counting the hours of setting up the servers, automation scripts and build jobs because that would be same for both architectural systems.
Example 2 – A US only B2B application
This is B2B application which has lower traffic than the previous application and does business in US only.
There were a total of 12,009 requests in the week considered and latency pattern looked something like this:
Average Latency: ~87ms
All the assumptions and considerations in this example are pretty much the same with the following differences:
- Instead of two m4.large machines, this application utilizes two c5.Large ones.
- Average latency for Lambda function that I will consider is 200ms in this case.
- I haven’t considered the data transfers cost for both EC2 and Lambda in this case as they are in single digit cents for both systems.
Total cost with EC2: USD 117, download the detailed costing sheet here.
Total Cost with AWS Lambda (256MB) without considering the perpetual free requests: (2 * 0.000000417 * 12,009 calls/mo) + (12,009 * (3.50 / 1000000)) = USD 0.05
Considering free Lambda seconds: USD 0.04
Dev, QA, Stage environments
Each environment that you create also costs money, all of these environments are sporadically called and Serverless almost always saves money there.
The more traffic that your application receives the less sense Serverless makes, now while all of us want to prepare for a billion requests a month level of traffic we need to make sure we estimate sensibly.
Decided that you want to go for Serverless? Not all applications are suitable for it, its not just the cost for which you should choose it, make sure you consider all the points. Read this article on “Why Serverless” before you make that decision.
If you liked this article, you can choose to follow this blog/subscribe to email alerts (top of sidebar or below comments in mobile) so that you know when any future posts come about.