Created
December 27, 2014 21:01
-
-
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…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Something a little more fully-featured: https://www.npmjs.com/package/locavore