aws-lambda-helper
Collection of helper methods for lambda
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');
}
Pushing Data Back to Client(s) Via WebSocket Server
var params = {
id: sessionId,
searchId: 12345,
userId: 'UniqueFingerprint',
items: [
]
};
AwsHelper.pushResultToClient(params, function (err, res) {
console.log(err, res);
});
Environment Variables
You will require an Environment Variable for the WEBSOCKET_SERVER_URL
and AWS_S3_SEARCH_RESULT_BUCKET
(see below for complete list of required Environment Variables)
if you get stuck get the Environment Variables from CodeShip:
https://codeship.com/projects/143221/configure_environment