I use Gulp to compress a zip file and then upload it to AWS Lambda. The boot zip file is executed manually. Only the compression process is handled by Gulp.
Here is my gulpfile.js
var gulp = require('gulp'); var zip = require('gulp-zip'); var del = require('del'); var install = require('gulp-install'); var runSequence = require('run-sequence'); var awsLambda = require("node-aws-lambda"); gulp.task('clean', function() { return del(['./dist', './dist.zip']); }); gulp.task('js', function() { return gulp.src('index.js') .pipe(gulp.dest('dist/')); }); gulp.task('npm', function() { return gulp.src('./package.json') .pipe(gulp.dest('dist/')) .pipe(install({production: true})); }); gulp.task('zip', function() { return gulp.src(['dist/**/*', '!dist/package.json']) .pipe(zip('dist.zip')) .pipe(gulp.dest('./')); }); gulp.task('deploy', function(callback) { return runSequence( ['clean'], ['js', 'npm'], ['zip'], callback ); });
After starting the deployment task, a zip folder is created with the name dist.zip consisting of the index.js file and the node_modules folder. The node_modules folder contains only the lodash library.
This is index.js
var _ = require('lodash'); console.log('Loading function'); exports.handler = (event, context, callback) => { //console.log('Received event:', JSON.stringify(event, null, 2)); var b = _.chunk(['a', 'b', 'c', 'd', 'e'], 3); console.log(b); callback(null, event.key1); // Echo back the first key value //callback('Something went wrong'); };
After using the AWS lambda console to load the dist.zip folder. An error occurred indicating that the lodash library could not be found
{ "errorMessage": "Cannot find module 'lodash'", "errorType": "Error", "stackTrace": [ "Function.Module._load (module.js:276:25)", "Module.require (module.js:353:17)", "require (internal/module.js:12:17)", "Object.<anonymous> (/var/task/index.js:1:71)", "Module._compile (module.js:409:26)", "Object.Module._extensions..js (module.js:416:10)", "Module.load (module.js:343:32)", "Function.Module._load (module.js:300:12)", "Module.require (module.js:353:17)" ] }
But in the zip folder there is a node_modules directory containing lodash lib.
dist.zip |---node_modules |--- lodash |---index.js
When I zip the node_modules directory and the index.js file manually, it works fine.
Can anyone understand what errors are? Maybe during compression using Gulp it is incorrectly configured for the lib path?