Skip to content

Instantly share code, notes, and snippets.

@petermorlion
Created July 16, 2019 06:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save petermorlion/f5aba23f417d25ae7eee33f0122827a1 to your computer and use it in GitHub Desktop.
Save petermorlion/f5aba23f417d25ae7eee33f0122827a1 to your computer and use it in GitHub Desktop.
Find AWS lambda cold start durations

Full code I used to get the amount and duration of AWS Lambda cold starts during 2 given months.

Also see the question on StackOverflow.

import AWS from "aws-sdk";
import fs from "fs";
import util from "util";
const appendFilePromise = util.promisify(fs.appendFile);
const logGroupName = `/aws/lambda/...`;
const cloudWatchLogs = new AWS.CloudWatchLogs({ region: "eu-west-1" });
async function main(month: string) {
let next = true;
let nextToken = "";
while (next) {
let params: any = {
logGroupName,
logStreamNamePrefix: `2019/${month}`,
descending: true
};
if (nextToken !== "") {
params.nextToken = nextToken;
}
try {
const response = await cloudWatchLogs.describeLogStreams(params).promise();
const logStreams = response.logStreams;
for (let logStream of logStreams) {
try {
const events = await cloudWatchLogs.getLogEvents({
logGroupName,
logStreamName: logStream.logStreamName,
startFromHead: true
}).promise();
for (let event of events.events) {
if (event.message.startsWith("REPORT")) {
const duration = /Duration:(.*)Billed/.exec(event.message);
if (duration) {
console.log(`${logStream.logStreamName} -- ${duration[1]}`);
await appendFilePromise(`2019-${month}.csv`, `${logStream.logStreamName},${duration[1]}\n`);
} else {
console.error("No duration found for : " + event.message);
}
break;
}
}
} catch (e) {
console.error(e);
}
}
if (response.nextToken) {
nextToken = response.nextToken;
next = true;
} else {
next = false;
}
} catch (e) {
console.error(e);
}
}
}
(async () => {
try {
await Promise.all([main("05"), main("06")])
} catch (e) {
console.log(e);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment