Skip to content

Instantly share code, notes, and snippets.

@slotrans
Created December 27, 2014 21:01
Show Gist options
  • Save slotrans/2583c8b2bbd204b0ca40 to your computer and use it in GitHub Desktop.
Save slotrans/2583c8b2bbd204b0ca40 to your computer and use it in GitHub Desktop.
Local test harness for AWS Lambda functions. I'm no JS programmer so this is probably horrible in some way or other, but it does appear to work. Note that this does NOT directly simulate the permissions of the function's execution role. It will run with whatever permissions belong to the AWS credentials you use, unless you run it on an EC2 insta…
var fs = require('fs');
// Lambda knows what region it's in but a local execution doesn't, so preload the SDK and set the region
// This will only work if the same variable name is used in the Lambda function file
var AWS = require('aws-sdk');
AWS.config.update({region: 'us-east-1'});
// validate arguments
if(process.argv.length < 4) {
console.error('Usage: node lambda_test_harness.js <function file> <input file>');
process.exit(1);
}
var context = {};
context.done = function(errorMessage, exitMessage) {
var exitCode = 0;
if(errorMessage) {
console.error(errorMessage);
exitCode = 1;
}
console.log(exitMessage);
process.exit(exitCode);
}
var exports = {};
var fileToInclude = process.argv[2];
eval(fs.readFileSync(fileToInclude).toString());
var inputFile = process.argv[3];
var inputEvent = JSON.parse(fs.readFileSync(inputFile).toString());
// assume you're using the conventional function name
exports.handler(inputEvent, context);
@daguej
Copy link

daguej commented Mar 13, 2015

Something a little more fully-featured: https://www.npmjs.com/package/locavore

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment