How to Optimize Cost of AWS Lambda

DZone 's Guide to

How to Optimize Cost of AWS Lambda

AWS Lambda is an excellent cloud compute tool, but every second and request adds up.

· Cloud Zone ·
Free Resource

In the monolithic architecture, analyzing and forecasting the expenses of running an application is pretty straightforward. However, after moving to microservices and serverless architecture, the cost complexity has increased. 

With the increase in cost complexity, various factors need to be considered. Even though AWS Lambda pricing is pretty straightforward, the cost of running a serverless application is complex and involves many challenges. In this article, we'll explore those challenges along with how you can optimize bill of running a serverless application. 

Before you jump into the cost optimization strategies, here are the three questions that you need to analyze: 

#1. What is the contribution of AWS Lambda pricing to your total bill? Serverless applications consist of databases, storage, network costs, APIs, and data processing systems to name a few. Considering the percentage AWS Lambda cost to the overall application cost, you will have to see whether it is worth running cost optimization cycles or not.

#2. What is your app’s requirement in terms of the performance? Before iterating functions memory allocation for the sake of lower cost, consider this: memory allocation has a significant impact on the cold start and runtime, both of which are directly proportional to the cost. Hence, if your app requires low latency performance, then opting for degraded performance at lower cost won’t be a recommended solution.

#3. Which and how many of the functions of your app are frequently used? Not every one of your functions will be invoked at the same frequency. Hence, focusing on cost optimization for functions with high frequency is recommended, for example, functions having hundreds of thousands or millions number of invocations.

Strategies for AWS Lambda Cost Optimization

There are multiple combinations of usage and configuration that affects the overall pricing of Lambda functions. This can be problematic sometimes!

For example, if you’re working for a small startup, you definitely don’t want to spend $500 over a single Lambda function. Let alone having hundreds of them. Here are few strategies to help you keep your Lambda cost in control:

1. Optimizing Function Frequency

There are various factors that affect the invocation frequency of Lambda function. This is based on the triggers. Closely monitor your triggers and see if you can do to reduce the number of invocations over the time.

For example, suppose your Lambda function is being triggered by the Kinesis Stream. Since the batch size is quite small, the invocation frequency is pretty high. In such cases, what you can do is opt for higher batch size so that your Lambda function is invoked less frequently.

2. Writing Efficient Code

The function that executes in half the time is a function that will cost you half the money. One must note that the execution duration is directly proportional to the amount of money you’ll be charged. It is critical to keep an eye on the Duration metric inside CloudWatch. And it’d be common sense to modify and iterate your function if it is taking a suspiciously long time to execute.

For this purpose, AWS X-Ray can help you monitor function from end to end.

3. Monitoring Data Transfer

While talking about Lambda charges, it often gets out of our sight that you’ll also be charged for the data transfer at standard EC2 data transfer rates. Hence, it is the default to keep an eye on the amount of data you’re transferring out to the internet and other regions of AWS.

While talking about internal data transfer, there isn’t much you can do about it as you can’t control the amount of data your Lambda will transfer. Likewise, there is no metric inside CloudWatch which can help you in monitoring the data transfer rate.

In such cases, here’s what you can do:

  • Keenly monitor your AWS Cost & Usage Report. Filter it by Resource (your Lambda function) and find different values in the Transfer Type column. Further, get the usage amount. This might come off as slightly time-consuming.
  • Log the size of data transfer operations in your Lambda code and then configure a CloudWatch Metric Filter as a reference that becomes a CloudWatch Metric.

4. Developing In-House Tools

If all of the above options aren’t suitable for you and you need a concrete solution that actually works, you can opt for developing in-house tools. Here are some of the examples:

  • Real-time Lambda Price Calculator: This is a quick and automated way to monitor estimated monthly cost in real-time of AWS Lambda based on your current usage. For more information, you can look here.
  • Memory Metric Filter: This is a simple CloudWatch Logs Metric Filter that can help you in monitoring by extracting memory size and allocated memory from the execution records. Launch Stack here and get started.


That being said, no one size fits all! However, for some organizations, serverless is helping them in cutting down the cost with a huge margin while others are probably looking for more comprehensive options considering the hidden costs of serverless. To avoid surprises, it is better to keep the eye on the bill and optimize on the go. If you have incorporated a strategy not mentioned here, I'd love to hear from you. Hit me up on Twitter @RohitAkiwatkar.

aws lambda, aws lambda function, cloud, cost optimization, serverless, serverless computing

Published at DZone with permission of Rohit Akiwatkar . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}