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);
});
Retrieving a Search Result (Artile Tile or Package)
When a user shares a result tile (e.g. an article or package)
we retrieve the result they saw from S3:
var params = {
userId: 'UniqueFingerprint',
sessionId: 12345,
itemId: ABC1234,
};
AwsHelper.getRecordFromS3(params, function (err, json) {
console.log(err, json);
});
Environment Variables
You will require an the following Environment Variables to push results to S3
and retrieve them later:
export WEBSOCKET_SERVER_URL=get_this_from_codeship
export AWS_S3_SEARCH_RESULT_BUCKET=get_this_from_codeship
export SEARCH_RESULT_TOPIC=arn:aws:sns:eu-west-1:123456789:my-awesome-topic
(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