Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Lambda Function is returning "Cannot find module 'index'" yet the handler in the config is set to index

As my title explains I am getting the following error:

 {
  "errorMessage": "Cannot find module 'index'",
  "errorType": "Error",
  "stackTrace": [
    "Function.Module._resolveFilename (module.js:338:15)",
    "Function.Module._load (module.js:280:25)",
    "Module.require (module.js:364:17)",
    "require (module.js:380:17)"
  ]
}

I have tried both solutions provided in creating-a-lambda-function-in-aws-from-zip-file and simple-node-js-example-in-aws-lambda

My config currently looks like:enter image description here

and my file structure is: enter image description here

and my index.js handler function looks like :

exports.handler = function(event, context) {

What else could be causing this issue aside from what was stated in those two answers above? I have tried both solutions and I have also allocated more memory to the function just incase thats why it couldn't run.

EDIT - For the sake of trying, I created an even simpler version of my original code and it looked like this:

var Q = require('q');
var AWS = require('aws-sdk');
var validate = require('lambduh-validate');
var Lambda = new AWS.Lambda();
var S3 = new AWS.S3();




theHandler = function (event, context) {

  console.log =('nothing');

}

exports.handler = theHandler();

And yet still does not work with the same error?

like image 569
Andrew Font Avatar asked Jul 27 '15 22:07

Andrew Font


People also ask

Why can't I Run my AWS Lambda code in Python?

I receive the "Unable to import module" error when I try to run my AWS Lambda code in Python. You typically receive this error when your Lambda environment can't find the specified library in the Python code. This is because Lambda isn't prepackaged with all Python libraries.

Why can't I find the specified library in my Lambda code?

You typically receive this error when your Lambda environment can't find the specified library in the Python code. This is because Lambda isn't prepackaged with all Python libraries.

How do I use Lambda with AWS publish-layer-version?

Create an AWS Identity and Access Management (IAM) role with permissions to call the publish-layer-version API. Then, attach the IAM role to the EC2 instance. Note: Your EC2 instance now has permissions to upload Lambda layers for the publish-layer-version API call.


3 Answers

Try zipping and uploading the contents of the folder lambda-create-timelapse. Not the folder itself.

like image 141
tahsintahsin Avatar answered Oct 20 '22 05:10

tahsintahsin


If this was unclear for anyone else, here are the steps:

Step 1 Navigate to the folder of your project, and open that folder so that you are inside the folder: Folder

Step 2 Select all of the images you want to upload into to Lambda: Folder with selected files

Step 3 Right-click and compress the files you have selected: Right-click menu with compress option highlighted


This will give you a .zip file, which is the file you need to upload to Lambda:

Archive.zip output


There are a lot of ways to automate this, but this is the manual procedure.

like image 21
Tyler Avatar answered Oct 20 '22 04:10

Tyler


I ran into this problem a few times myself, and this indeed has to do with zipping the folder instead of just the contents like you're supposed to.

For those working from the terminal...

While INSIDE of the directory where the .js files are sitting, run the following:

zip -r ../zipname.zip *

The * is instructing the client to zip all the contents within this folder, ../zipname.zip is telling it to name the file zipname.zip and place it right outside of this current directory.

like image 11
Michael Du Avatar answered Oct 20 '22 06:10

Michael Du