Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Index handler is undefined or not exported

I have a lambda function which was working fine but I wanted to import a package so I created a directory with index.js and installed my npm package.

Then created a zip of this folder and uploaded it using

aws lambda update-function-code --function-name smrtfac-test --zip-file fileb://lambda.zip

But now I am getting this error

index.handler is undefined or not exported

What could be the reason for it? my index.js and node_modules are in the same directory.

like image 996
Anshul Walia Avatar asked Mar 13 '20 06:03

Anshul Walia


People also ask

How do I specify a handler in AWS Lambda?

This function handler name reflects the function name ( lambda_handler ) and the file where the handler code is stored ( lambda_function.py ). To change the function handler name in the Lambda console, on the Runtime settings pane, choose Edit.

What is callback in Lambda function?

The third argument, callback , is a function that you can call in non-async handlers to send a response. The callback function takes two arguments: an Error and a response. When you call it, Lambda waits for the event loop to be empty and then returns the response or error to the invoker.

Can Lambda continue after returning response?

No, this isn't possible.

Why is my index handler undefined AWS Lambda?

If you get an “index.handler is undefined” error on AWS lambda, it could be one of a couple things – essentially their code is doing “require (‘index’).handler (event, context, callback)” and erroring out: Your index.js script has incorrectly named exports

Why can't I find the index handler in lambda?

The index.js (which contains the handler function that iterates over the different modules) is never uploaded to the cloud. When executing the Lambda it can't find index.handler since it never got uploaded.

Why is the name of the handler method in my function's configuration?

The name of the handler method in your function's handler configuration doesn't match your code. Each runtime defines a naming convention for handlers, such as filename. methodname. The handler is the method in your function's code that the runtime runs when your function is invoked.

Why does the index file have to be at the root?

The index file needs to be at the root to be able to be read and accessed by lambda. Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question.


4 Answers

This usually occurs when you zip up the directory, instead of zipping up the contents of the directory. When you open your zip file to browse the content, the index.js file should be in the root of the zip file, not in a folder.

like image 159
Mark B Avatar answered Oct 18 '22 05:10

Mark B


You could also change the Handler section as below if your index.js is not directly under root folder as below

enter image description here

like image 33
Pakira Avatar answered Oct 18 '22 07:10

Pakira


Consider using Lambda Layers for node modules: https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html#configuration-layers-path

like image 4
Leon Africa Avatar answered Oct 18 '22 06:10

Leon Africa


This is because you are probably submitting the project inside a directory. You just zip all the files directly instead of zipping them into a directory. The index file needs to be at the root to be able to be read and accessed by lambda.

like image 3
João Galli Jr. Avatar answered Oct 18 '22 07:10

João Galli Jr.