I am building a cdk stack including a bunch of lambda functions. My goal here is to update all the node to es6 so I can use tree shaking, and use Webapck to hugely optimise the individual lambda bundles. I’m also updating the AWS-SDK to version 3 so I can take advantage of the leaner imports of individual packages and use bare bones clients etc. Basically anything I can think of to trim any fat off the code so speed it up and save a few $$$.
The folder structure of the relevant parts of the project:
monkey-cloud/
├──lambda
│ ├── src/
│ │ ├── release/
│ │ │ ├── image-get.js
│ │ │ ├── image-put.js
│ │ ├── critical/
│ │ │ ├── data_iot.js
│ │ │ ├── data-get.js
│ │ │ ├── device-put.js
│ │ │ ├── device-get.js
│ ├── includes/
│ │ ├── dynamodb/
│ │ │ ├── monkey_dynamodb.js
│ │ ├── s3/
│ │ │ ├── monkey_s3.js
│ ├── dist/
├── webpack.config.js
├── package.json
What I have done so far is install webpack & a bunch of other stuff:
npm install --save-dev webpack webpack-cli babel-loader @babel/core @babel/preset-env
Then I’ve created a webpack.config.js at the root of the project:
const path = require('path');
const glob = require('glob');
// Automatically find all Lambda entry points
const entry = glob.sync('./lambda/src/**/*.js').reduce((acc, file) => {
const name = path.relative('./lambda/src', file).replace(/.js$/, '');
acc[name] = file;
return acc;
}, {});
module.exports = {
entry,
output: {
filename: '[name]/index.js',
path: path.resolve(__dirname, 'lambda/dist'),
libraryTarget: 'commonjs2',
},
mode: 'production',
target: 'node',
module: {
rules: [
{
test: /.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env'],
},
},
},
],
},
optimization: {
usedExports: true,
},
};
I’ve added a simpole script into the package.json
"//-full-build": "webpack && node ./lambda/lambda-zip.js",
"build": "webpack"
And last piece of the puzzle, I’ve added a little node.js script to to some build stuff (lambda-zip.js) – essentially to clean up the outputs into a format that is ready to use by cdk when I actually deploy the project. N.B. I’ve temporarily disabled that step above.
To test the progress so far, I can just run npm run build
. Ideally, this should bundle up my lambdas into a nice clean tree of zipped files to shove into my cdk lambdas as code.
It’s not working so smoothly for me… When I run webpack, I end up with a lot of unexpected outputs. I’ve simplified the project to have only 1 lambda in it (critical/data-iot.js). When I just run webpack and nothing else, it does create the critical/data-iot.js in the dist folder. However, it builds a whole BUNCH of other folders called 109, 108 etc… And each of those has an index.js in them. It looks like it’s trying to build a bunch of lazy loaded assets…
Questions
-
How do I zip up this carnage into a single zip file for the cdk lambda?
-
When I add in the other hundred lambda functions, this is going to get slightly out of control!? Is there some way of forcing webpack to generate a single file for each lambda? And obviously in a sensible predetermined location. Otherwise it’s going to be a nightmare to automate the zipping up process.