Lambda Billing Tracker
What is it?
Tracking your lambda usage can be hard, in fact it's impossible right now because AWS doesn't provide an API to get that from within the lambda function itself, your only bet is CloudWatch.
This library uses a timer to track how long your functions within the lambda are taking. This will get you a close guess at how long the lambda is running.
How accurate is it?
On a cold start this is less accurate considering this function doesn't actually track usage until the function is called by the Lambda container. Everything after is close enough that we can use this library reliably.
Installation
$ npm install --save @printtracker/lambda-usage-tracker
Clean Usage
Start by instantiating a new instance of $BillingService
import $BillingService from '@printtracker/lambda-usage-tracker';
let billingService = new $BillingService()
This service is ready to go! There are four methods you can use:
- start
- end
- getUsage
- measure
- log
They are methods on the $BillingService instance that can be called like this:
billingService.start()
Lambda Usage
Implementation in a Typescript Lambda handler.ts
file could look something like this:
import { Handler, Context } from 'aws-lambda';
import $BillingService from '@printtracker/lambda-usage-tracker';
import { HelloWorldService } from './services';
const helloWorld: Handler = async (event: any, context: Context) => {
let billingService = new $BillingService()
const data: [] = await HelloWorldService.helloWorld();
let response = {
statusCode: statusCode || 400,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': true,
},
body: {]
payload: billingService.measure(data),
usage: billingService.getUsage()
}
}
return response;
}
export { helloWorld };
Method Reference
start
Use this method to explicitly start the timer where you want. By default when you instantiate a new instance of $BillingService
the timer is started. To use the start
method to explicitly start the time, do this:
let billingService = new $BillingService(false)
billingService.start()
Notice that in order to not start the timer at class instantiation, we need to pass false
as a parameter into the new $BillingService
instance.
end
Use this method to explicitly state when the timer should end. No special usage is needed. This function does not return any data.
getUsage
Use this method to get the measurements the service saved. These measurements are returned in the form of the interface:
interface BaseBillingUsageInterface {
startTime: number;
endTime: number;
totalTime: number;
memorySize: number;
recordCount: number;
billedDuration?: number;
}
memorySize
looks to the process.env.memorySize
variable which you would need to create in a lambda environment. This variable assists in calulating the billedDuration
.
measure
This method takes an array as input and returns an untouched version of that array but sets the $BillingService instance variable, recordCount
to determine how many records in a JSON array were passed through the lambda. It returns the identical array so that you can use it inline similar to a pipeline process. Normally you would return your data like this:
let response = dataFromMyService;
Instead we do this:
let response = measure(dataFromMyService);
As long as the data type is an [] this works.