48

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.

Mohamad Mousheimish
  • 1,641
  • 3
  • 16
  • 48
Anshul Walia
  • 547
  • 1
  • 5
  • 9

6 Answers6

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.

Mark B
  • 183,023
  • 24
  • 297
  • 295
  • 5
    Command I used to fix the issue (run from within the directory): zip -r -D lambda.zip * – Haider Jun 11 '20 at 04:25
  • 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. – hobberwickey Oct 27 '20 at 21:58
  • Since zip doesn't work In windows, I use the commands mentioned in this answer, https://stackoverflow.com/a/64475079/8810941, to zip and unzip the content. Sure, it has some limitations but is more than enough for zipping a lambda function. – Pavindu Apr 21 '21 at 14:50
  • @Pavindu I would recommend using WSL for this sort of thing if you are on a Windows machine. – Mark B May 26 '22 at 13:39
17

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

Pakira
  • 1,951
  • 3
  • 25
  • 54
4

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

Leon Africa
  • 509
  • 6
  • 11
4

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

alayor
  • 4,537
  • 6
  • 27
  • 47
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.

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.

Lucius Kaye
  • 91
  • 3
  • 6