Index handler is undefined or not exported
Asked Answered
B

7

49

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.

Backpedal answered 13/3, 2020 at 6:40 Comment(1)
For me, it was an issue with the function's case, I forgot to change one char to uppercasePhotocompose
F
141

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.

Faradic answered 13/3, 2020 at 12:49 Comment(4)
Command I used to fix the issue (run from within the directory): zip -r -D lambda.zip *Aldin
This worked for me, but I had a lot of symlinks in my bin directory all pointing back to image magick and without the -y flag it ended up being way too large for lambda. So yeah... include the -y flag.Lardy
Since zip doesn't work In windows, I use the commands mentioned in this answer, https://mcmap.net/q/167681/-how-to-zip-a-file-using-cmd-line, to zip and unzip the content. Sure, it has some limitations but is more than enough for zipping a lambda function.Chingchinghai
@Chingchinghai I would recommend using WSL for this sort of thing if you are on a Windows machine.Faradic
N
19

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

Nattie answered 13/5, 2021 at 22:19 Comment(0)
S
4

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

Selig answered 28/8, 2020 at 17:28 Comment(0)
M
4

If you're using typescript with CDK, make sure you are not exporting another function within your main function file.

Maundy answered 28/2, 2022 at 6:44 Comment(0)
L
3

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.

Limicoline answered 9/9, 2021 at 13:33 Comment(0)
P
0

I was using colima and after trying a bunch of things, I did:

colima delete
colima start

colima doc : link

In the lambda project's root directory, do these commands again and all should be ok

sam build
sam local invoke

At the very least, a hello-world project should work. You can use sam init to build a hello-world app and run a basic check.

Pyroxylin answered 15/8, 2023 at 14:53 Comment(0)
W
0

I also faced this error. In my case, my index.js was NOT inside a folder and was in the root of the zip file.

But the my runtime configuration was named index.handler when it should have been called lambda.handler.

So upon updating the name of the handler, I was able to resolve this issue.

enter image description here

Wafture answered 3/5 at 5:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.