List of AWS Lambda Functions with Invocation Counts in the last 24 hours
Code
The following JavaScript code retrieves a list of all your AWS Lambda functions, as well as the count of invocations for each function over the last 24 hours.
// Load AWS SDK and create new service objects const cloudwatch = new AWS.CloudWatch({}); const lambda = new AWS.Lambda({}); // Specify the time range for the CloudWatch metrics const oneDayAgo = new Date(); oneDayAgo.setDate(oneDayAgo.getDate() - 1); // Call the Lambda listFunctions method const listFunctionsPromise = lambda.listFunctions().promise(); // Define function to retrieve CloudWatch metrics data const getMetricsData = async (FunctionName) => { const params = { EndTime: new Date(), StartTime: oneDayAgo, MetricName: 'Invocations', Namespace: 'AWS/Lambda', Period: 86400, Statistics: ['Sum'], Dimensions: [ { Name: 'FunctionName', Value: FunctionName } ] }; return cloudwatch.getMetricStatistics(params).promise(); }; // Use the promise .then() method to handle the response listFunctionsPromise.then(async (data) => { const functionsData = await Promise.all(data.Functions.map(func => getMetricsData(func.FunctionName))); const invocationCounts = functionsData.map((metricData, index) => ({ FunctionName: data.Functions[index].FunctionName, Invocations: metricData.Datapoints[0] ? metricData.Datapoints[0].Sum : 0 })); invocationCounts.sort((a, b) => b.Invocations - a.Invocations); console.log(invocationCounts.slice(0, 5)); });
Detailed Code Explanation
The aforementioned script initiates two AWS service objects, one for AWS CloudWatch and one for AWS Lambda. Then it defines a starting point from which to look at metrics data for the Lambda functions (exactly one day prior). A call to AWS.Lambda.listFunctions()
lists all of the functions and for each one, gets the invocation count data in the specified time range using AWS.CloudWatch.getMetricStatistics()
.
The collected data is then refined, sorted by the number of invocations in descending order and the top 5 functions with the most invocations in the last 24 hours are logged on the console.
Expected Output Format
The expected output will be a sorted array of objects. Each object represents a Lambda function and its corresponding invocation count in the last 24 hours. It will look something like this:
[ { "FunctionName": "function1", "Invocations": 1000 }, { "FunctionName": "function2", "Invocations": 750 }, ... ]
Considerations & Caveats
It's important to note that this script only lists functions in the default (or specified) region. If your AWS Lambda functions are spread over multiple regions, you need to instantiate the AWS.CloudWatch and AWS.Lambda with the relevant region codes and run the script for each region.
Required IAM Permissions and Example Policy
To execute this script, your IAM policy must have permissions to lambda:ListFunctions
(to list AWS Lambda functions) and cloudwatch:GetMetricStatistics
(to get Cloudwatch metrics data). An example of such a policy could be:
{ "Version": "2012-10-17", "Statement": [ { "Sid": "VisualEditor0", "Effect": "Allow", "Action": [ "lambda:ListFunctions", "cloudwatch:GetMetricStatistics" ], "Resource": "*" } ] }
FAQ
Q1: Can I modify the code to get invocation counts for more days?
Yes, you can modify the oneDayAgo.setDate(oneDayAgo.getDate() - 1);
line to go as many days back as you want by replacing 1
with the number of days.
Q2: The output does not include all the functions I have in my account. What could be the reason?
This script only shows the top 5 functions with the most invocations over the last 24 hours. If you want the script to show all your functions, you can remove or modify the invocationCounts.slice(0, 5)
line.
Q3: Can I use this script to monitor real-time invocations?
No, CloudWatch metrics are not real-time and may have a delay of several minutes.
Q4: Will this script cause any additional charges on my AWS account?
This script uses AWS CloudWatch and AWS Lambda. Both services have free usage tiers, however, if your account exceeds these limits, additional charges may apply. Please check AWS Pricing for exact details.