Lambda Cost Optimization, you can do it in several ways. I am listing down my top way for AWS Lambda Cost reduction.
For any successful project, planning is the key, and if you are working on lambda cost reduction this information will help.
The suggestion is a mix of best practices for cost reduction while using serverless architectures and some are technical suggestions.
1. Use Of Saving Plans
If you know your usage pattern and have some future projections in hand, using Savings Plans is a great option. It is a very simple trade-off Amazon offers against your upfront commitment to usage.
You can secure up to 15% discount rate for a one-year and up to 17% discount rate for a three-year term, this is offered against your commitment to usage.
The good news is it will cover your other compute services usage too, the discount is given in order of highest to lowest discount percentage.
You should consider committing to the saving plan option if your Lambda function usage is not varying too much.
If you see your AWS Cost Explorer is recommending purchasing a savings plan, look at your usage pattern to cover Lambda usage.
Look at AWS Cost Explorer Page to understand how compute savings plans work with Lambda
2. Implementing Cost Control Measures
You can have a defined budget at the beginning and have alerts to ensure you stay within your desired spending limits. AWS has tools & features that can help you to manage and control your Lambda costs effectively.
You can also set up an approval process before provisioning new resources for your project.
3. Right Size Your Functions, Look At Memory Settings:
Understand how your functions are using memory and what is the execution time to find the optimal memory allocation.
Your Lambda costs are calculated based on the memory used and execution time for your task, how many tasks you are running, or the number of requests.
Working on your functions to right-size them can help you to reduce charges without compromising performance.
For instance, if an application is CPU intensive, and you increase the memory up to a certen limit to reduce the execution time. This will save you the cost per execution. With the help of the AWS console, you can find out how much memory function is used.
For your Lambda function, currently you can allocate the memory between 128 MB and 10,240 MB.
4. Code Optimization – Write efficiently
The optimized code will run faster, since the code execution time is directly proportional to how much you’ll charged against your lambda cost.
The question is how to do code optimization, there is no single-liner answer to this, you can use Amazon CodeGuru Profiler, what it will do is based on the log it will show you the most expensive line of code, and your optimization job becomes simple. It will also give you some recommendations on how to improve.
5. Data Transfer – In and Out
Though it is not a direct function, you will be charged at the standard EC2 rate for data transferred, so it is very important to know how data flows in and out of your Lambda function.
In the same AWS Region, for data transferred between S3 buckets, DynamoDB database, SQS messaging, Kinesis, Amazon SNS notifications, and AWS Lambda functions there is no charge. But if the data is traveling across the AWS Region, you will be charged for this data transfer.
You can optimize data transfer by caching, doing compression, and doing good data handling.
6. Lambda timeout – Important to Set
There are situations when you call something outside Lambda (external services) out of your Lambda function and you do not receive a response. Your Lambda function will run for the maximum default value of 15 minutes before timeout. In this situation, you will be charged for 15 minutes of execution.
But if you handed this by providing your timeout value of less than 15 minutes, you will save money. Lambda cost per ms, so any time saved is equal to money saved.
7. Reduce Lambda-Related CloudWatch Costs
You don’t need everything logged and stored forever. Use the AWS CloudWatch retention policy for Lambda log groups and define what you want to store for how long.
If not done properly this may sometimes cost you more than your usage at a point.
How To Calculate Lambda Cost
Understanding AWS Lambda Pricing is very important so that you know your avenues to save your money.
Every Lambda function needs some time to run, now while running it will also need memory allocation. These two factors are considered for your computing charges. In increments of 100 milliseconds of your run time you will charged, this is how AWS Lambda bills compute.
This adds to the amount of GB memory used by the Lambda function to run. So, finding the right proposition of vCPU and GB RAM used is very important for lambda cost optimization.
The request is equal to the number of times you trigger the Lambda function. So, AWS Lambda cost per function it is triggered and considered for cost calculation. You will be charged per 1 million requests.
So, while coding you need to avoid/minimize unnecessary Lambda function invocations. This you can do by implementing event-driven architecture and reducing/controlling lambda cost per invocation.
Data Transfer Across Services
AWS charges for data transfer into and out of Lambda functions, especially when data transfer between different AWS regions and availability zones. So understanding these charges will help you to plan your data transfer strategy to reduce overall Lambda cost.
So, plan who and when your AWS Lambda functions will interact with other AWS services or external resources.
Using compute allocated (vCPU), Memory allocated (GB), Function run time (ms), Number of requests and data transfer (GB) you will be able to calculate AWS Lambda cost.
How To Automate Lambda Cost Optimization
You can use the below methods to automate your Amazon Lambda cost
- Use AWS Cost Explorer
- Using Billing Alerts
- Using Cost Optimization Tools
Lambda Cost Estimation
This is the sample Lambda Cost Estimation, without free tier numbers. The region selected is US East (N. Virginia).
Number of requests – 10000 per second
Duration of each request (in ms) – 5
Amount of memory allocated – 512 MB
Amount of ephemeral storage allocated – 512 MB
AWS provides a pricing calculator to estimate the cloud cost.
Video Credit Amazon Web Services
FAQ: Lambda Cost
Is Lambda cost efficient?
Yes, you only pay for the resources used during the time, it is good for event-driven or short-duration task execution using serverless architectures.
How do aI save on Lambda costs?
Use Of Saving Plans
Implement Cost Control Measures
Right Size Your Functions & Resources
What is the advantage of Lambda?
You do not manage infrastructure, so you save your time on this task and focus more on app development.
Now you know you can start optimizing your Amazon lambda cost from the stage of planning and during operation too.
You can start by removing unnecessary idle waiting time during external interaction or synchronous workflows. Take advantage of event-driven architecture. Avoid implementing synchronous workflows.
Do not forget to use available tools, use AWS Lambda Cost Explorer to start with. Let us know your thoughts on lambda cost optimization and your experience in the comment section below.