Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Lambda function returning Runtime.HandlerNotFound error

I am new to AWS Lambdas - I am trying to write a lambda function to retrieve some data from firebase. I have called this function exports.handler and uploaded it to the Lambda with the node modules as a Zip file. However, when I try to run it, it returns the following error:

{
  "errorType": "Runtime.HandlerNotFound",
  "errorMessage": "index.handler is undefined or not exported",
  "trace": [
    "Runtime.HandlerNotFound: index.handler is undefined or not exported",
    "    at Object.module.exports.load (/var/runtime/UserFunction.js:144:11)",
    "    at Object.<anonymous> (/var/runtime/index.js:43:30)",
    "    at Module._compile (internal/modules/cjs/loader.js:1156:30)",
    "    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1176:10)",
    "    at Module.load (internal/modules/cjs/loader.js:1000:32)",
    "    at Function.Module._load (internal/modules/cjs/loader.js:899

Here is my function:

module.exports.handler = async (event) =>{
    ref.on("value", function(snapshot) {
        snapshot.forEach(function(childSnapshot) {
            responses.push({
                date: childSnapshot.val().Date,
                name:childSnapshot.val().name,
                response: childSnapshot.val().response
            })
        });
        printObjects(responses);
        console.log(json(responsesByPerson));
    })
})

I have looked at the other answers to questions similar to mine and implemented some of those solutions but none have worked.

like image 737
Yohan Avatar asked May 17 '20 18:05

Yohan


4 Answers

When we zip a directory the shell directory is also added to the zip while the Lambda expects it to be without the shell directory. Use the below command to create the zip correctly.

$ cd my_lambda_fun
$ zip -r lambda_one.zip .
like image 53
Mahesh M Avatar answered Oct 23 '22 01:10

Mahesh M


zip -r lambda_one.zip . worked for me

like image 20
Rick Avatar answered Oct 23 '22 03:10

Rick


You have to make sure that you don't zip your project but the contents of your project.

For eg, if your index.js is directly inside my-project, you should $> cd my-project and then create a zip with the contents. This zip can then be uploaded to AWS Lambda.

I hope this helps!

like image 37
saurabh Avatar answered Oct 23 '22 02:10

saurabh


Ensure index.js is in the root of the Lambda function directory

like image 26
Chris Williams Avatar answered Oct 23 '22 02:10

Chris Williams