Home

Awesome

AWS Lambda Shim

CircleCI npm

A Better Lambda API

This library provides an abstraction between the AWS Lambda events and classic node.js calling conventions.

This project is sponsored by the Functional Fleet Serverless Platform. We are currently accepting beta users for our platform, featuring a free git repo, hosted deploy queue, and centralized monitoring.

Why

AWS Lambda has a somewhat unintuitive API structure that is sometimes inconsistent with node.js standards. This library bridges the gap, allowing you to write more familiar node.js style code.

Wrap

Wrapping your function standardizes the calling conventions across all versions of Node.js and all AWS Lambda event sources. This includes:

Example:

var shim = require('@ffleet/shim');
var myLib = require('./lib');

// callback is not needed with Promise
function myHandler(event) {
	// myLib.handle returns a promise
	return myLib.handle(event);
}

exports.myHandler = shim.wrap(myHandler);

Libraries

The specific API shims below provide context specific APIs for certain Lambda events.

HTTP

Node.js HTTP servers are built around request and response objects passing through route handlers. This has been embraced by the common node.js HTTP libraries as well (Express, Hapi, Restify, etc.).

The http shim creates a native node.js HTTP server and proxies the AWS Lambda event.

For example, using a native handler:

const shim = require('@ffleet/shim');

function myNativeHttpHandler(req, res) {
	res.end('Hello, World!');
}

exports.myNativeHttpHandler = shim.http(myNativeHttpHandler);

Or, you can wrap an entire Express server:

const shim = require('@ffleet/shim');
const express = require('express');
const app = express();

app.get('/', function(req, res, next) {
	res.send('Hello, World!');
});

exports.myExpressHttpHandler = shim.http(app);

Cron

Cron functions are executed by AWS CloudWatch Events. Wrapping with the cron shim unpacks the event data and provides a Date object of when the cron event was generated.

const shim = require('@ffleet/shim');

function myCron(date, callback) {
	console.log('cron event generated at ' + date.toISOString());
}
exports.myCron = shim.cron(myCron);

Logs

AWS CloudWatch Log data is provided in a base64 encoded and gzip packed data string. The logs shim will decode the log data and reconstitute the represented JSON object, as:

{
	"messageType": "DATA_MESSAGE",
	"owner": "123456789123",
	"logGroup": "testLogGroup",
	"logStream": "testLogStream",
	"subscriptionFilters": ["testFilter"],
	"logEvents": [{
		"id": "eventId1",
		"timestamp": 1440442987000,
		"message": "[ERROR] First test message"
	}, {
		"id": "eventId2",
		"timestamp": 1440442987001,
		"message": "[ERROR] Second test message"
	}]
}

Example:

const shim = require('@ffleet/shim');

function myLogHandler(logObject, callback) {
	logObject.logEvents.forEach(logEvent => console.log('log', logEvent));
}
exports.myLogHandler = shim.logs(myLogHandler);

License

MIT License

Copyright (c) 2018 Functional Fleet

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.