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.
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); | |
| } | |
| })(); |