Anshul Walia
Anshul Walia

Reputation: 587

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.

Upvotes: 51

Views: 63161

Answers (8)

fenix.shadow
fenix.shadow

Reputation: 461

In my case, I forgot to export the function.

Instead of this:

const handler = async () => {

Should be this:

export const handler = async () => {

Upvotes: 0

Mayur Mahajan
Mayur Mahajan

Reputation: 124

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

Upvotes: 0

Lucius Kaye
Lucius Kaye

Reputation: 91

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.

Upvotes: 0

alayor
alayor

Reputation: 5045

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

Upvotes: 4

João Galli Jr.
João Galli Jr.

Reputation: 51

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.

Upvotes: 3

Pakira
Pakira

Reputation: 2021

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

Upvotes: 19

Leon Africa
Leon Africa

Reputation: 619

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

Upvotes: 4

Mark B
Mark B

Reputation: 201138

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.

Upvotes: 141

Related Questions