AWS Lambda function might look low-priced while getting started. Even though developers can limit its max memory size and max function execution time, there are chances of costs spiraling out of control due to several reasons. It could be because of a DDoS attack, excess memory allocation to each function, or a bug in the code that accidentally invokes a function innumerable times!
In this post, we walk you through ways to get a handle on the cost of Lambda functions, and how a visual console can help you get clarity in ~15 seconds (Jump to Point#4 to watch it in action now!).
At the top level, it looks like AWS bills you only for the time your Lambda functions are running. In actuality, it depends on:
– The number of executions/requests
– The time range for which a function is running (rounded up to the nearest 100 ms)
– The memory size of the function
Example: if a function uses 512 MB and is called 5 million times in a month (lasting one second for each invocation), the monthly cost for this Lambda alone would be $36.8072. Consider the number of dollars you would spend on all the functions put together!
Ideally, you have to keep a healthy balance between memory allocation, number of invocations and run time of the function to avoid the risk of overpaying. Monitoring lambda functions for optimality must be a part of your regular audits. Otherwise, the spend might reach up to hundreds of dollars.
Here are a few ways to monitor these functions for cost optimization:
Every time a Lambda executes, a record prints:
Duration: 1000 ms Billed Duration: 500 ms Memory Size: 512 MB Max Memory Used: 25 MB
The above record clearly shows that the function is using just 25 MB out of 512 MB. In this scenario, a function with 128 MB of memory works fine too as well as save hundreds of dollars per month. However, if you allocate much lesser, the function might drop before execution.
On that account, monitor Lambda records regularly. The challenge, however, with this approach is you have to spend a lot of time and effort in monitoring each function. More the functions, more amount of time and effort you have to invest in knowing their performance.
That’s why several cloud engineers across the globe use AWS calculator or test the function before launching, with different memory allocations, execution time and run time and then calculate the cost.
Using AWS Cost Explorer, you can closely monitor the pricing for the week, month, or quarter, slicing and dicing it further by Service, Region, AZ, Usage Type, Tag name, etc.
Cost of Lambda function that triggers RabbitMQ
Despite the cost details, the AWS console does not provide the visibility into actual problem area. You can just figure out whether the Lambda function costs have spiralled out of control, when an issue occurs.
If you have tagged your Lambdas properly, you can check out each of these tagged function’s pricing for the week in the console and discover the culprit function to take further action. If you have not, it’s next to impossible to find that culprit function. So several developers use CloudWatch Alarms to get a notification when a function exceeds the set limits.
There are several third party tools available in the market, like this cool tool on GitHub that calculates AWS Lambda costs in near real-time. Even with such tools, you have to key in all the details to get cost details manually. You have to sieve through data to make sense of the entire picture.
One of the novel ways to monitor Lambda cost by memory, runtime , execution time, code size, subnet, and more is to use a visual console.
Like TotalCloud’s state-of-the-art interface featuring built-in filters. These filters help group resources by several parameters like memory utilization, runtime, execution, etc. at multiple levels. The console’s Cost Analyzer analyzes spend on top of these grouped resources, in 3D space. Above all, the interface helps you get a viewable context of Lambda usage and get a handle on spending, in real-time, in just a few seconds. Watch the video below to know more.
If you observe the table below, the time and effort it takes to monitor the performance and cost of Lambda:
– The Manual Way: minutes to hours depending on the number of functions
– The Cost Explorer Way: few minutes, if and only if you tag all the Lambda functions properly
– The TotalCloud Way: ~ 15 seconds, irrespective of whether you tag the functions or not! Plus, you get the cost and performance perspective at a glance, on a single pane of glass.
AWS Lambda function’s pay-per-use billing model, turning on only during execution definitely saves on computing resources and costs. That said, it is equally important to keep a close watch on sub-optimal functions, while also track its cost, in real-time.