Is AWS Lambda expensive to run?

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.

Check out my answers for what is serverless and where you should use it and best practices around AWS Lambda if that’s what you are looking for.

What you should consider in costs?

When you are estimating costs related to an architectural choice there are two important factors to consider:

  1. The raw hosting cost, calculate the estimated traffic, the highest possible and the lowest one, take into account data being transferred
  2. 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.

Assumptions

  1. 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.
  2. 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.
  3. 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).
  4. 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.
  5. 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.
  6. 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.
Requests in a week
There were a total of 12,009 requests in the week considered and latency pattern looked something like this:
Latencies in the one week period

Average Latency: ~87ms

All the assumptions and considerations in this example are pretty much the same with the following differences:

  1. Instead of two m4.large machines, this application utilizes two c5.Large ones.
  2. Average latency for Lambda function that I will consider is 200ms in this case.
  3. 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.

Conclusion

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.

10 thoughts on “Is AWS Lambda expensive to run?

  1. In example one server example.. i dont see you data out charge. This is often associsted with what is going through your internet gateway.. that should change the amount in the srrver example and align it to equivalency to the serverless example

    Like

    • I didn’t consider the internet gateway charges for both cases which would be the same and would be in the tune of single digit USD’s. From a comparison point of view, I think that’s fine. Did I miss something that you are trying to point out?

      Like

  2. For your calculations you have chosen lambda times of 700ms and 200ms. If the cost for lambda is per 100ms why do the formulas use 700ms and 200ms? It seems this puts that part of each formula off by a factor of 100 which is very significant.

    These parts of the formulas should either divide by 100 to get the number of 100ms intervals or just use 7 and 2.

    Maybe I’m missing something?

    Like

  3. Are you considering the fact that serveless save lot of time and money out of the box. Sure you can save more money if, you hire the right people and build the right product,. How much do you have to spend to find the right people to build a bullet proof system vs let the experts do their thing even if you pay a little more money

    Like

    • I agree with you 100%, I touched this briefly here: “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.”. The reason why I didn’t talk about it in detail as it becomes a point for a lot of people to not give any thought whatsoever to a new system of developing software. In this article, I simply wanted people who have an opinion without data to start making decisions based on the same. I don’t just want to be another blog writer who forms blanket opinions in people, my objective is to only impart all that is required to make the decision themselves keeping in mind the basic fundamental that every decision and every system is subjective to the problem in hand.

      Like

  4. I’m a bit confused. From the beginning it seemed I would learn when it would be cheaper to run serverless and when it would not. But in both examples serverless was clearly the cheapest.

    Or am I missing something?

    Like

  5. Pingback: Express Serverless Platform on AWS - LunchBadger

Leave a comment