Created
April 27, 2020 10:08
-
-
Save nicokruger/4622ca6224d6067ea1f0eb5e12345358 to your computer and use it in GitHub Desktop.
Work out approximate cost per lambda across region over time period using Cloudwatch Log Insights
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const aws = require('aws-sdk'); | |
aws.config.region = 'eu-west-1'; | |
const cloudwatchlogs = new aws.CloudWatchLogs(); | |
const moment = require('moment'); | |
const fs = require('fs'); | |
const filename = `results-${new Date().getTime()}.csv`; | |
fs.appendFileSync(filename, ['logGroup','dollars','rcnt','numberdollars','total'].join(",") + "\n"); | |
const endTime = moment().toDate().getTime(); | |
const startTime = moment().subtract(1, 'day').toDate().getTime(); | |
const queryString = ` | |
filter @type = "REPORT" | |
| fields 1 as req, @timestamp as Timestamp, @requestId as RequestID, @logStream as LogStream, @billedDuration as BilledDurationInMS, @memorySize/1000000 as MemorySetInMB, @billedDuration/1000*MemorySetInMB/1024 as BilledDurationInGBSeconds | |
| stats sum(BilledDurationInGBSeconds) * 0.0000166667 as dollars, | |
sum(req)* 1 as rcnt, | |
sum(req) / 1000000 * 0.2 as numberdollars, | |
dollars + numberdollars as total | |
` | |
async function getPricing(logGroup) { | |
const data = await getQueryResults(logGroup); | |
if (!data.length) { | |
return [0,0,0,0] | |
} | |
return data[0].map( (lol) => { | |
return lol.value; | |
}); | |
} | |
async function getQueryResults(logGroup) { | |
var params = { | |
endTime, | |
startTime, | |
queryString, | |
limit: 1000, | |
logGroupName: logGroup | |
}; | |
const query = await cloudwatchlogs.startQuery(params).promise(); | |
let ret = false; | |
let data; | |
while (!ret) { | |
console.log(logGroup, 'sleeping'); | |
await sleepPromise(5000); | |
data = await cloudwatchlogs.getQueryResults({ | |
queryId: query.queryId | |
}).promise(); | |
if (data.status === 'Complete') { | |
ret = true; | |
data = data.results | |
} | |
} | |
return data; | |
} | |
async function priceLogGroups(logGroups) { | |
await Promise.all(logGroups.map( async (logGroup) => { | |
const data = await getPricing(logGroup); | |
fs.appendFileSync(filename, [logGroup].concat(data).join(",") + "\n"); | |
})) | |
} | |
async function priceLambda() { | |
let done = false; | |
const params = { | |
limit: 9, | |
logGroupNamePrefix: '/aws/lambda/' | |
} | |
while (!done) { | |
const data = await cloudwatchlogs.describeLogGroups(params).promise(); | |
const logGroupNames = data.logGroups.map( ({logGroupName}) => { | |
return logGroupName; | |
}); | |
await priceLogGroups(logGroupNames); | |
const nextToken = data.nextToken; | |
if (!nextToken || !logGroupNames.length) { | |
done = true; | |
} else { | |
params.nextToken = nextToken; | |
} | |
} | |
} | |
function sleepPromise(t) { | |
return new Promise((resolve) => { | |
setTimeout(resolve, t); | |
}); | |
} | |
priceLambda() | |
.then( () => { | |
console.log('done'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment