Facing issues loading zip code in aws lambda

I am new to aws and just started working with aws lambda, following some youtube tutorials, and was able to successfully write aws lambda functions in the web editor itself.

But I tried downloading the zip file from my local system, in which I wrote node.js code that uses the "fs" and "fill-pdf" modules. But when I tried to run the code, it gave me an error.

"error": module not found "/ var / task / index". I searched over the internet and found some links like: https://github.com/lob/lambda-pdftk-example I tried this, but it also shows the same error.

Here is my code:

var index = require('index'); var fillPdf = require("fill-pdf"); var fs = require('fs'); var formDate = { 'Employee Name': 'MyName', 'Company Name': 'ComapnyName' }; var pdfTemplatePath = "my.pdf"; fillPdf.generatePdf(formDate, pdfTemplatePath, function(err, output) { if ( !err ) { fs.writeFile('message.pdf', output, function (err) { if (err) throw err; console.log('It\ saved! in same location.'); }); } }); 

The fact is that I do not know what is the cause of this error. Thanks for any help.

+8
aws-lambda
source share
4 answers

Make sure that you do not zip the folder, but its contents. Make sure your zip contains index.js at the root level

+26
source share

The error may occur due to the following:

 1. Properly zip the folder wait for it zipping process completion and then upload. 2. First run the main.js file locally like using node main.js and check are there any errors showing in the terminal window, if it does then fix them and then upload. 3. Also there must be handler file that lambda needs, which is must so if you have the handler.js file then when in aws lambda you create a lambda function and check the configuration setting there then do update the name of the handler file name with yours like by default it is index.js may be you would have lambda.js do change it with lambda name (example lambda.handler) 
+2
source share

Delete the line var index = require('index'); because it is not used in your code. I'm not sure why it cannot find the module after installing it, but in your current example you don't need it.

0
source share

This error occurs, it means that your zip code is not in the valid form that aws requires.

If you double-click on zip, you will find your folder inside this code file, but lambda wants it to display direct code files when double-clicking on zip.

To achieve this:

 open terminal cd your-lambda-folder zip -r index.zip * 

then load index.zip in lambda

0
source share

All Articles