aws-lambda-helper
Collection of helper methods for lambda
![codecov.io](https://codecov.io/github/numo-labs/aws-lambda-helper/coverage.svg?branch=master)
Installation
$ npm install aws-lambda-helper --save
Usage
var AwsHelper = require('aws-lambda-helper');
exports.handler = function(event, context) {
...
AwsHelper.init(context);
...
}
Invoke a Lamda function
var AwsHelper = require('aws-lambda-helper');
exports.handler = function(event, context){
AwsHelper.init(context);
console.log(AwsHelper.env);
console.log(AwsHelper.region);
console.log(AwsHelper.account);
var params = {
FunctionName: 'MyAmazingLambda',
Payload: { 'hello': 'world' },
Qualifier: ''
};
AwsHelper.Lambda.invoke(params, function (err, data) {
AwsHelper.failOnError(err, event, context);
context.succeed(data);
});
}
fn: failOnError
This function will intercept the error if exists and then will call 'context.fail'.
The purpose of this function is to avoid the need of checking for errors so that in callback you could just focus on the succesfull procedure.
It can be used as follows:
AwsHelper.Lambda.invoke(params, function (err, data) {
AwsHelper.failOnError(err, event, context);
context.succeed(data);
});
Logging JSON messages
var AwsHelper = require('aws-lambda-helper');
exports.handler = function(event, context){
AwsHelper.init(context, event);
var log = AwsHelper.Logger('example');
log.info();
log.info('hi');
log.info('hi %s', bob, anotherVar);
log.info({foo: 'bar'}, 'hi');
log.info(err);
log.info(err, 'more on this: %s', more);
log.info({foo: 'bar', err: err}, 'some msg about this error');
}