How do I build a Lambda deployment package for Node.js?

4 minute read
0

I want to build a deployment package for an AWS Lambda function in Node.js, but I get a "Cannot find module" error during invocation.

Short description

The Cannot find module error usually occurs for one of three reasons:

  • The Lambda function's deployment package doesn't have the correct folder structure to allow the Lambda service to load the required modules and libraries.
  • The deployment package doesn't have the correct file permissions.
  • The deployment package doesn't contain the module being imported.

Note: Lambda requires global read permissions.

Resolution

To create a deployment package with function code in the root folder of the .zip file and then apply read and execute permissions for all files, do the following.

Note: If you receive errors when running AWS Command Line Interface (AWS CLI) commands, make sure that you’re using the most recent AWS CLI version.

For Linux and macOS

Install dependencies to the function project folder

Note: Most Node.js modules are platform-independent, but some are compiled against specific operating system environments. Lambda runs under a Linux environment. When installing modules with npm, it's a best practice to build the .zip file in a Linux environment to make sure that the correct platform dependencies are included.

1.    In the command line, change directories to your project folder. For example:

cd /project-folder-name

Important: Make sure to replace project-folder-name with the name of your actual project folder.

2.    Install your dependencies locally to your function project folder by running the following command for each module required for your function:

Note: Also include the handler source file.

npm install package-name

Important: Make sure to replace package-name with the actual package name. There's also a 250 MB limit on your function size for unzipped files. Include only the libraries that you need for your function to work.

For example, to install the AWS SDK for JavaScript modules to the root of a project folder, run the following command:

npm install aws-sdk

Note: Lambda includes the AWS SDK for JavaScript libraries as part of the base container configuration. To use a different version of the library, you can also include a local copy. For more information about the current Lambda environment and supported libraries, see Lambda runtimes.

Build the deployment package

In the command line, run the following command:

zip -r ../function-name.zip .

Note: Replace function-name with the file name that you want to give your deployment package. This puts all files in the project folder into a .zip file located in the parent folder.

Verify the deployment package

1.    In the command line, run the following command:

zipinfo ../function-name.zip

Note: Replace function-name with the actual file name of your deployment package. Or, you can run unzip -l on the .zip file to list its contents, but the output won't be as detailed.

2.    Check the output to verify that the function handler source file is located in the root of the .zip file.

3.    Check the output to verify that your files have global read permissions. For more information, see How do I troubleshoot "permission denied" or "unable to import module" errors when uploading a Lambda deployment package?

For Windows

Build the deployment package

1.    In File Explorer, open your project folder.

2.    Select all the project files, then right-click to open the context menu.

3.    Choose Send to, then choose Compressed (zipped) folder.

4.    Enter a name for the .zip file.

Upload and verify the deployment package

1.    In the Lambda console, choose your function.

2.    Under Function code, for Code entry type, choose Upload a .zip file.

3.    Under Function package, choose Upload.

4.    Choose the .zip file you created, and then choose Open.

5.    At the top of the console, choose Save.

Tip: You can also run update-function-code from the AWS CLI to upload your .zip file.

6.    After uploading is completed, choose Test.

Tip: You can also use 7-Zip from the command line to verify your deployment package's file permissions. Download it from the 7-Zip website.


Related information

Deploy Node.js Lambda functions with .zip file archives

Lambda deployment packages

Building Lambda functions with Node.js

AWS OFFICIAL
AWS OFFICIALUpdated 2 years ago