AWS Lambda - How Performance Influences Lambda Cost

AWS Lambda - How Performance Influences Lambda Cost

ยท

5 min read

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.

  1. Architecture (x86 or Arm)

  2. Number of Requests per unit time (Time being in seconds, min, hours, days & month)

  3. Duration of each request (in ms)

  4. Amount of memory allocated to Lambda (MB)

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

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.

Image description Image description

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 to a company might seem negligible. 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 optimize the Lambda performance and make it more efficient.

Image description

The cost of on-demand Lambda is determined by the time it takes to execute initialization 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:

  • If you don't need to, don't overallocate memory for Lambda. If your testing shows that Lambda's maximum memory usage is 150MB, don't give it 3GB. Utilise the open source AWS Lambda Power Tuning tool to assist you in figuring out the best Lambda settings between performance and cost.

  • As previously stated, improving the efficiency of your Lambda code (within the event handler) will lower Lambda execution time and cost.

  • Choose the appropriate Lambda timeout value. Don't set a timeout of 30 seconds if 99 percent of your Lambda executions are completed in 100 ms or less. Choose a more appropriate timeout value.

  • Use a runtime that boots up faster. However, you can improve Lambda's performance by ensuring that the static initialisation ("INIT") code is efficient. The size of function packages, the amount of code and initialisation work, and the efficiency of libraries and other services in setting up connections and resources all affect the cold start. Make sure you only initialize what is required by lambda.

  • Choose the appropriate architecture. Arm architecture can help you save money on Lambdas. Running Lambda on ARM now costs $0.0000133334 per GB-second, while running lambda on x86 costs $0.0000166667 per GB-second. For the same number of requests, both have the same charges.

  • If Lambda is triggered by API gateway and produces data that doesn't change frequently, Cloudfront may be an alternative to have. This will raise the cost of Cloudfront, but it will lower the cost of your API gateway and Lambda. Architect your solution with cost, security & performance in mind.

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 ๐Ÿ™‚

Say Hello on: Linkedin | Twitter | Polywork

Blogging: Dev | Hashnode

Did you find this article valuable?

Support OzPerf by becoming a sponsor. Any amount is appreciated!

ย