I am trying to create a simple lambda function, and I'm running into an error.
My code is basically
console.log('Loading function'); exports.handler = function(event, context) { console.log('value1 =', event.key1); console.log('value2 =', event.key2); console.log('value3 =', event.key3); context.succeed(event.key1); // Echo back the first key value // context.fail('Something went wrong'); }
in a helloworld.js file. I zip that up and upload it as a zip file in the creating a lambda function section, and I keep getting this error:
{ "errorMessage": "Cannot find module 'index'", "errorType": "Error", "stackTrace": [ "Function.Module._resolveFilename (module.js:338:15)", "Function.Module._load (module.js:280:25)", "Module.require (module.js:364:17)", "require (module.js:380:17)" ] }
Does anyone have any ideas?
The . zip file contains your function's code and any dependencies used to run your function's code (if applicable) on Lambda. If your function depends only on standard libraries, or AWS SDK libraries, you don't need to include these libraries in your .
It should be stored in the same directory as your Lambda handler function. They should be bundled in a zip file and deployed to AWS. If you didn't deploy it that way then that file doesn't currently exist.
The name of your file needs to match the module name in the Handler configuration. In this case, your Handler should be set to helloworld.handler
, where helloworld
is the file that would be require()'d and handler
is the exported function. Then it should work with the same zip file.
Make sure your index.js is in the root of the zipfile and not in a subdirectory.
In my case I had the name of the module matching the name of the file and the exported handler, the real problem was macOS and the zip program which basically creates a folder inside the zip file so when uncompressed in AWS Lambda engine the index.js ends in a subdirectory.
Don't right click and compress the directory, instead select the files individual files like index.js, package.json and the node_modules directory and right-click to compress, you may end up with a file Archive.zip in the same directory. The name of the zip file is not going to be fancy but at least it will work when you submit it to AWS Lambda.
You could make the same mistake using the command line with zip -r function.zip function
which basically creates a zip file with a directory called function
in it, instead do:
$ zip function.zip index.js package.json node_modules adding: index.js (deflated 47%) adding: package.json (deflated 36%) adding: node_modules/ (stored 0%)
Using finder, if you double click the zip file and it uncompresses in a subdirectory then Lambda won't be able to see the file as index.js lives in that subdirectory.
Using the command line and zipinfo
:
$ zipinfo function.zip | grep index.js | more -rw-r--rw- 2.1 unx 1428 bX defN 27-Jul-16 12:21 function/index.js
Notice how index.js ended up inside the subdirectory function
, you screwed up.
$ zipinfo function.zip | grep index.js | more -rw-r--rw- 3.0 unx 1428 tx defN 27-Jul-16 12:21 index.js
Notice that index.js is not inside a subfolder, this zip file will work in AWS Lambda.
So I added a script to my package to zip the project files for me just by running npm run zip
{ "name": "function", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "zip": "zip function.zip package.json *.js node_modules" }, "dependencies": { "aws-sdk": "^2.4.10" } } $ npm run zip > [email protected] zip > zip function.zip package.json *.js node_modules adding: package.json (deflated 41%) adding: index.js (deflated 47%) adding: local.js (deflated 42%) adding: node_modules/ (stored 0%)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With