AWS Lambda - How Performance Influences Lambda Cost
In the last couple of months, I've been contacted by performance engineers wanting assistance in understanding Lambda performance and cost. In this article I explain how Lambda performance influences cost. It also covers how to calculate on-demand Lambda costs. And I provide a few recommendations on how to keep your Lambda costs low while also improve it's performance. Once you've mastered this skill, you can go on to a more sophisticated case that includes Lambda provisioned concurrency.
If you're not familiar with AWS Lambda, it's a serverless, event-driven computing service that lets you run code for virtually any type of application or backend service without having to provision or manage servers.
As a result, Lambda's cost as a service is influenced by the following variables: The emphasis of this article will be on on-demand Lambda. And not Lambda@Edge or Lambda with provisioned capacity.
Only the last three variables are required for Lambda@Edge cost estimation. For Lambda with provisioned capacity, you also need to consider the cost of concurrency and the time concurrency is enabled.
So, how does performance impact Lambda cost?
As previously stated, one of the criteria that impacts the cost is the Lambda execution time (duration) for each request. A faster Lambda execution will result in a reduced Lambda cost if everything else remains the same. Here's an illustration of what I'm referring to. Assume Lambda is running on Arm Architecture, with a monthly total of 5,000,000 Lambda requests, an average Lambda execution time of 100ms, and 512MB of RAM allocated. You will be charged as follows with these values:
Monthly Compute Charges:
=>Amount of memory allocated (in GB) = 512MB = 0.5GB
=>Total compute (in seconds) = 5,000,000 requests *100ms * 0.001 ms to sec conversion factor = 500,000
=> Total compute (GB-s): 0.5GB * 500,000sec = 250,000
=>For free tier, you get 400,000 GB-s free. Total compute = 250,000 - 400,000 = -150,000
=>Max (-150000.00 GB-s, 0 ) = 0.00 total billable GB-s
Monthly Compute Charges = 0.00 GB-s x $0.0000133334 (Arm GB-s cost) = $0.00
Monthly Request Charges:
=>For free tier you get 1,000,000 free requests.
=>Total monthly billable requests = 5,000,000 total requests - 1,000,000 free tier requests = 4,000,000
=> Max (4000000 monthly billable requests, 0 ) = 4,000,000.00 total monthly billable requests
Monthly request charges = 4,000,000 * $0.2/Million = 4,000,000 * 0.0000002 = $0.8
Recommended by LinkedIn
Total Lambda cost/month = Compute cost + Request cost = $0.0 + $0.8 = $0.8
Please keep in mind that Lambda pricing is based on the AWS Sydney Region. Depending on the region chosen, your calculation may yield a different cost to mine.
Now let's imagine your Lambda execution time increases from 100ms to 1000ms. If all other variables stayed the same, the overall monthly cost would be $28.8. If Lambda execution increases to 30,000 ms, than your monthly cost will be $996.
The following graph depicts the monthly cost with varying Lambda execution times while all other variables remain the same.
The annual Lambda cost is depicted in the graph below. The overall cost for the year for the 30,000 ms scenario is $11,946. The $11,946/ year Lambda cost for some companies might seem negligible and for others not. However, if you factor in the cost of other cloud services, your annual cloud cost could quickly become significant. There is a cost incentive to optimise the Lambda performance and make it more efficient.
The cost of on-demand Lambda is determined by the time it takes to execute initialisation and event handler code. If the same Lambda execution environment is used for subsequent requests, then the cost is primarily due to the event handler code.
Using the AWS Lambda Price calculator you can do the same thing with different configurations. What if the time it takes for Lambda to execute and the amount of requests it receives varies? What will the total Lambda price then? In such cases, the Lambda Price calculator can be extremely useful, especially when combined with performance testing and operational requirements. It might offer you an early sense of how much your monthly Lambda costs will be.
Finally general points to keep Lambda cost down:
There are also other charges related with Lambda that you should be aware of, such as data transfer, VPC/VPC peering, and so on, which may appear on your monthly bill.
Finally, not only does Lambda efficiency affect performance, but it also affects cost. Do keep an eye on your Lambdas.
---------------------------------------------------------------------------------------------------------------
Thanks for reading!
If you enjoyed this article feel free to share on social media 🙂
Github repo: hseera
Very useful Harinder
Harinder Seera, This is a great post! It illustrates the natural alignment with performance engineering and cloud costs. The ironic piece is the same items which drive cloud costs higher also make response times slower and scalability poorer because of the issue of resource scarcity at high load. Recently there was another article which would pair extremely well with this, one on language efficiency. Pair the right algorithm in lambda with a highly efficient language, you you have performance gold! https://medium.com/codex/what-are-the-greenest-programming-languages-e738774b1957