Getting a Handle on Spiraling AWS Lambda Cost in Seconds
Getting a Handle on Spiraling AWS Lambda Cost in Seconds
Amazon's AWS service is relatively cost-efficient...but only if you are clearly aware of what it will cost. Use these method to keep costs down.
Join the DZone community and get the full member experience.Join For Free
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!
Factors that Affect AWS Lambda Cost:
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
For 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, the number of invocations, and runtime 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. The first three approaches take minutes to hours depending on the number of functions, while the fourth approach takes ~15 seconds. Here's a walkthrough:
The Good Old Manual Approach:
(Takes minutes to hours depending on the number of functions.)
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, and saves hundreds of dollars per month. However, if you allocate much les, the function might drop before execution.
On that account, monitor Lambda records regularly. The challenge with this approach, however, is you have to spend a lot of time and effort in monitoring each function. The greater the number of functions, the more 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.
The Cost Explorer Approach:
(Takes few minutes, if all the Lambda functions are appropriately tagged.)
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.
CCCost of Lambda function that triggers RabbitMQ
Despite the cost details, the AWS console does not provide the visibility into the 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.
The Third-Party Tools Approach:
(Takes minutes to hours depending on the number of functions)
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.
Using a Visual Console:
(Takes ~ 15 seconds, irrespective of whether the functions are tagged or not!)
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.
The Wrap Up:
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.
Published at DZone with permission of Jayashree Hegde Adkoli , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.