AWS Lambda function returning Runtime.HandlerNotFound error
Asked Answered
A

9

22

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.

Asch answered 17/5, 2020 at 18:6 Comment(10)
What’s your file name?Upright
index.js - it is inside a sub-folder called functions I have just realised - would that make a difference?Asch
In that case if you set your lambda handler to be functions/index.handler, does that work?Omphale
nope, same issue. Also tried moving index.js out of functions and into the main file but that didnt work eitherAsch
Move it to the rootUpright
As in the root of the zip file? Tried that but it didnt workAsch
@Asch How did you zip the deployment package? Could you post the commands/instructions you used?Stendhal
@Stendhal I used the compress folder option on Mac, right click and compressAsch
@Asch Oh, that's the culprit. You should go into the folder, select all files, and then compress.Stendhal
Thanks for the suggestion - also dosent work.Asch
P
23

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 .
Pedrick answered 3/7, 2020 at 13:56 Comment(2)
I am on a Mac and used the finder to zip the files, and got this handler not found error message. Zipping via the console made it work, thanks!!Llewellyn
I have used this command for zipping my function. but still have issues...Disown
U
5

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

Upright answered 17/5, 2020 at 19:16 Comment(1)
I am uploading as a zip as theres quite a few Node modules I need (firebase, gcp, etc) so I cant view the file on the inline editor. In the zip file I uploaded, I have included it in the root folder and a subfolder and it didnt work in eitherAsch
H
5

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!

Hiss answered 10/7, 2020 at 12:14 Comment(0)
G
4

This was one error I also got while Trying to execute the NodeJs Code. The following are the points we need to take care:

  1. Lambda Handler must be "filename.handler"
  2. filename.js must Exist in the Root Directory of the Zip Folder that would be uploaded to Lambda Function.
  3. While creating the Zip Folder - Open the Folder and use "Select All" before using "Send To" Compressed.(The benefit is the filename.js would be reflected in the Root itself, rather than Creating Zip folder for the Application Project Folder). This has worked, for the Lambda function Execution for NodeJS Project.
Gainless answered 15/6, 2021 at 13:32 Comment(1)
Point 3 was the issue is my case. You saved me a lot time. ThanksLetters
E
3

zip -r lambda_one.zip . worked for me

Extant answered 5/7, 2020 at 11:58 Comment(0)
D
3

Some of the answers here are indeed correct, but if you're like me and trying to do this from Windows then it should be noted that $ zip -r lambda_one.zip . will not work from PowerShell as it will in Linux and macOS.

The workaround was simply to zip the folders + files manually in the file explorer:

  1. Select folders + files to be zipped
  2. Right click > Send to > Compressed (zipped) folder
  3. Rename it to whatever you need, then proceed with uploading it to Lambda when you create your function.
Dean answered 3/3, 2021 at 7:42 Comment(0)
B
1

Be careful for not being using a wrong Running engine like NodeJS but writing code in Python or something of that nature. Go to the Lambda function info link and select the correct Running Engine for your development.

Bluegill answered 16/2, 2022 at 12:4 Comment(0)
S
0

before the this line def handler(event, context):

if you have any imports, then remove those. It should work.

Sideboard answered 6/12, 2021 at 18:7 Comment(0)
T
0

Make sure to name your handler file as index.js other you will get this error

Tireless answered 27/3, 2022 at 6:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.