I am trying to package code for AWS Lambda. Lambda has various restrictions, such as using Node 6.10, and not having a build step, like AWS EB does. I also am using NPM modules, so these will need to be bundled with the AWS Lambda handler.
Here is what I would like to do:
- Define and use NPM modules (pure JS modules only)
- Transpile all code (including NPM modules) to a JS version that Node 6.10 supports
- Statically link all NPM modules into one big JS file
- Upload that single file to AWS Lambda
For example, suppose I have an NPM module foo
(node_modules/foo/index.js
):
export default { x: 1 };
and I have my own code ('index.js'):
import foo from 'foo';
export const handler = (event, context, callback) => {
console.log(foo); // Will appear in CloudWatch logs
callback(null, 'OK');
};
The output would be something like this ('dist/bundle.js'):
var foo = { x: 1 };
exports.handler = function(event, context, callback) {
console.log(foo);
callback(null, 'OK');
};
I should be able to upload and run bundle.js
on AWS Lambda without further modification.
How can I achieve this using existing JS tools?