Skip to content

Instantly share code, notes, and snippets.

@cmawhorter
Created December 29, 2015 23:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmawhorter/cf96fcd9d8962bc019cf to your computer and use it in GitHub Desktop.
Save cmawhorter/cf96fcd9d8962bc019cf to your computer and use it in GitHub Desktop.
Quick and dirty local lambda development environment.
'use strict';
// To use - edit the options below and `node local-lambda.js`
// path to your lambda entry point
var app = require('./index.js');
// the handler you wish to test (you may only have one e.g. exports.handler)
var handler = app.handler;
// simulated max duration
var duration = 30000;
// the sample/mock event you'd like to use for testing
var mockEvent = require('./mock-event.json');
// TODO: CLI mockEvent to ease testing multiple environments
// TODO: implement maximum memory usage. i seem to remember process.memoryUsage() being unreliable https://nodejs.org/api/process.html#process_process_memoryusage
// ==========================
var end = new Date().getTime() + duration;
var context = {
awsRequestId: 'awsr-eque-stid-fake-9184', // e_e
getRemainingTimeInMillis: function() {
return Math.max(0, end - new Date().getTime());
},
succeed: function(obj) {
console.log('Succeeded', obj);
process.exit();
},
fail: function(err) {
console.log('Failed', err);
process.exit(1);
},
done: function(err, obj) {
if (err) {
context.fail(err);
}
else {
context.succeed(obj);
}
},
};
handler(mockEvent, context);
#!/usr/bin/env bash
cd $(dirname $0)
rm -f dist.zip
rm -fr node_modules
# lambda is currently 0.10. i recommend using nvm with 0.12 since
# i've found aws-sdk on node v0.10 problematic when running locally (for whatever reason)
# nvm use 0.12
# don't install devDeps
npm install --production
# optional. if some deps are es6 only, you can use this to transpile to something that'll work with lambda
# babel --plugins ../../scripts/build/node_modules/babel-plugin-transform-object-set-prototype-of-to-assign --presets ../../scripts/build/node_modules/babel-preset-es2015 node_modules/wreck -d node_modules/wreck
# sometimes that es6 transpiling isn't perfect
# node transpiling-patch.js
# finally create our lamabda bundle. only include what we explicitly want
zip -r dist.zip index.js config.js package.json node_modules/ lib/
// this is json but i wanted comments. the contents here would be
// named "mock-event.json" as described above
// this is pulled from lambda's canned testing events
{
"Records": [
{
"eventVersion": "2.0",
"eventTime": "1970-01-01T00:00:00.000Z",
"requestParameters": {
"sourceIPAddress": "127.0.0.1"
},
"s3": {
"configurationId": "testConfigRule",
"object": {
"eTag": "0123456789abcdef0123456789abcdef",
"sequencer": "0A1B2C3D4E5F678901",
"key": "HappyFace.jpg",
"size": 1024
},
"bucket": {
"arn": "arn:aws:s3:::mybucket",
"name": "sourcebucket",
"ownerIdentity": {
"principalId": "EXAMPLE"
}
},
"s3SchemaVersion": "1.0"
},
"responseElements": {
"x-amz-id-2": "EXAMPLE123/5678abcdefghijklambdaisawesome/mnopqrstuvwxyzABCDEFGH",
"x-amz-request-id": "EXAMPLE123456789"
},
"awsRegion": "us-east-1",
"eventName": "ObjectCreated:Put",
"userIdentity": {
"principalId": "EXAMPLE"
},
"eventSource": "aws:s3"
}
]
}
// example of a transpiling-patch.js; used in package.sh
// wreck and dependencies are all es6 and >= node 4.0 because of it
// whether or not we agree with that decision we must deal with it
// babel transpiling works for the most part except for one line. let's just str replace
var fs = require('fs');
var path = require('path');
var target = path.join(__dirname, 'node_modules/wreck/node_modules/boom/lib/index.js');
var data = fs.readFileSync(target).toString();
var patched = data.replace('}, null)', '}, {})');
fs.writeFileSync(target, patched);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment