Packaging Code For Aws Lambda
Solution 1:
You can use serverless with serverless-webpack
Then you deploy your bundle with serverless deploy
Solution 2:
It turns out that this is possible, but it requires some tricky configuration to achieve. I have created a boiler-plate repo for others to use.
Here are the important bits...
First, you need a .babelrc
that targets Node.js 6.10
:
{"presets":[["env",{"targets":{"node":"6.10"},"loose":false,"spec":true}]]}
Next, you need to configure Webpack to generate a commonjs
library targetting node
:
const path = require('path');
const webpack = require('webpack');
const debug = process.env.NODE_ENV !== 'production';
module.exports = {
context: __dirname,
entry: [ 'babel-polyfill', './index.js' ],
output: {
path: path.join(__dirname, 'out'),
filename: 'index.js',
libraryTarget: 'commonjs'
},
devtool: debug ? 'source-map' : false,
module: {
rules: [
{
test: /\.js$/,
use: {
loader: 'babel-loader',
options: {
babelrc: true,
compact: !debug
}
}
}
],
},
target: 'node',
plugins: [
new webpack.DefinePlugin({ 'global.GENTLY': false })
]
};
Note that you do not want to ignore the node_modules
folder, since that would prevent static-linking.
The babel-polyfill
plugin is also crucial if you want to use modern JS features.
Your actual handler code should have a named export
that matches what you have set in the AWS console:
exportconsthandler = (event, context, callback) => callback(null, 'OK');
Do not do it like this!
// Bad! exportdefault {
handler: (event, context, callback) =>callback(null, 'OK'),
};
When packaging the code, make sure you add index.js
to the top level of the zip:
zip -j bundle.zip ./out/index.js
Post a Comment for "Packaging Code For Aws Lambda"