Uploading Node.js package to AWS Lambda

Quick tip: for those developing AWS Lambda applications using Node.js, if you’re uploading a zip package rather than editing inline, something you might get stuck on while trying to test your function is the below error:

Unable to import module 'index': Error at Function.Module._resolveFilename (module.js:325:15) at Function.Module._load (module.js:276:25) at Module.require (module.js:353:17) at require (internal/module.js:12:17)

First, make sure the name of your handler in the AWS console matches the name of your “main” JavaScript file (the one containing your exports.handler function). Screen Shot 2016-07-06 at 11.01.28 AM

If your file with the exports.handler function is named “index.js”, then in the AWS console, name it as “index.handler”.

Next, something that really tripped me up was not having this index.js file in the root of my .zip. This was what ultimately led to the Unable to import module 'index' error I kept getting. So make sure this file is in the root of the package.

 

coreyc

 

5 thoughts on “Uploading Node.js package to AWS Lambda

  1. I’m doing exactly as described in your blog but still getting the same issue –
    Unable to import module ‘index’: Error
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17).

    My zip file structure is –
    index.handler
    node_modules/async
    node_modules/gm

    1. The file should be called index.js and then the handler is the name of the function that gets exported. In other words:

      My zip file structure is –
      index.js
      node_modules/async
      node_modules/gm

      and

      exports.handler… in the index.js file.

Leave a Reply

Your email address will not be published. Required fields are marked *