Home > Software design >  Can you access monthly used execution time in a lambda function?
Can you access monthly used execution time in a lambda function?

Time:09-14

I have an android app that uses AWS lambda as a backend to fetch data - which usually takes a few seconds of execution time per request.

I would like to be able to fetch data and cache it at the end of the month if there is still free execution time available, however, I could not find a way to access that information inside a lambda function. Is there a way to do this?

CodePudding user response:

if there is still free execution time available

You cannot directly do that, need to perform cloudwatch log aggregation as told by @Dan M, but the most ideal way would be to always set an alarm and notification once your overall execution time( across all functions ) exceeds your desired value

It is a very straightforward approach using cloudwatch alarms :-

  • You need to choose metric based on dimension which is called as across all functions to be able to view aggregated execution time for all functions in that region. Check how to view all functions
  • There are 2 types of metrics Invocation and performance metrics, you need to focus on performance metrics, for these metrics you need to consider their average statistic
  • Now create cloudwatch alarm for Duration metric ( under performance metric) and add sns rule when ever alarm breaches your desired value

According to Docs for duration metric

The amount of time that your function code spends processing an event. The billed duration for an invocation is the value of Duration rounded up to the nearest millisecond.

Docs for creating cloud watch alarm

CodePudding user response:

As soon as i know, lambda functions only got temporary storage and you will have to use another AWS service for persistent data storage like AWS S3 or AWS Elastic block store. So you have to save your data during code execution. A approach can be found in this article at the chapter "write to AWS S3":

https://www.startdataengineering.com/post/pull-data-from-api-using-lambda-s3/

CodePudding user response:

You need to have your Lambda function send logs to Cloudwatch. Then, whenever it executes, you get something like:

REPORT RequestId: c9999f19-0b99-4996-8dea-94c9999999de  Duration: 5495.59 ms    Billed Duration: 5496 ms    Memory Size: 128 MB Max Memory Used: 82 MB  Init Duration: 306.78 ms

You can then export the logs and using whatever tool you like get your monthly total.

  • Related