Skip to content

Instantly share code, notes, and snippets.

@j3tm0t0
Last active August 29, 2015 14:10
Show Gist options
  • Save j3tm0t0/26f2e109212a2e22e25d to your computer and use it in GitHub Desktop.
Save j3tm0t0/26f2e109212a2e22e25d to your computer and use it in GitHub Desktop.
locally test lambda script
var fs = require('fs');
if(process.argv.length == 3)
{
if(process.argv[2].match(/\.json$/))
{
try{
var event = JSON.parse(fs.readFileSync(process.argv[2], 'utf8'));
} catch(e) {
console.log("could not parse JSON from file: "+process.argv[2]);
return 1;
}
}
else
{
try{
var event = JSON.parse(process.argv[2]);
} catch(e) {
console.log("could not parse parameter as JSON string. : "+process.argv[2]);
return 1;
}
}
}
else
{
console.log('usage: node invoke.js input.json');
console.log(" node invoke.js '{\"key1\":\"1\",\"key2\":\"2\",\"key3\":\"3\"}'");
return 0;
}
var Context=function(){
this.done=function(flag,msg){
if(flag == null)
{
console.log("SUCCESS",msg);
}
else
{
console.log("FAILURE",null,msg);
}
};
};
var app=require('./app.js');
app.handler(event,new Context());
~/work/lambda$ cat app.js
console.log('Loading event');
exports.handler = function(event, context) {
console.log('key1:' + event.key1);
console.log('key2:' + event.key2);
console.log('key3:' + event.key3);
setTimeout(function(){
context.done(null, 'Hello World'); // SUCCESS with message
},1000);
return;
};
~/work/lambda$ cat input.json
{
"key1":"1",
"key2":"2",
"key3":"3"
}
~/work/lambda$ node invoke.js input.json
Loading event
key1:1
key2:2
key3:3
SUCCESS Hello World
~/work/lambda$ node invoke.js '{"key1":"1","key2":"2","key3":"3"}'
Loading event
key1:1
key2:2
key3:3
SUCCESS Hello World
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment