Kien shows you how to
build a Lambda deployment package
for Node.js

kien_lambda_node

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

You might need to create a deployment package that includes the function code located in the root of the zip file with read and execute permissions for all files.

A common error for creating a Lambda function in Node.js is "cannot find module." This is usually due to incorrect folder structure or file permissions with the deployment package zip file. Global read permissions are required by Lambda.

Install all dependency modules local to the function project

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

To change directories to your project folder:

cd /my_project_folder

To install your dependencies locally to your function project folder:

npm install aws-sdk

This installs the aws-sdk JavaScript modules to the root of your project folder. Repeat this for all modules required for your Lambda function.

Note: There is a 250-MB limit on your function size for unzipped files. Only include the libraries you need for your function to work. AWS Lambda includes the AWS JavaScript SDK libraries as part of the base container configuration. To use a different version of the library, you can also include a local copy.

Manually build a deployment package

After all dependent modules are downloaded to the project folder, you can zip up the project folder for deployment to Lambda.

In the project folder, run this command:

zip -r ../my_function.zip .

This zips all files in the publish folder into a zip file located in the parent folder.

Verify a deployment package

To check your deployment package, you must use a zip tool to verify the folder structure of the zip file.

Linux and macOS:

zipinfo ../my_function.zip

Notice that the function handler source file is located in the root of the zip file.

In this example, only the file owner has read permissions.

$ zipinfo test.zip
Archive:  test.zip
Zip file size: 473 bytes, number of entries: 2
-r--------  3.0 unx        0 bx stor 17-Aug-10 09:37 exlib.py
-r--------  3.0 unx      234 tx defN 17-Aug-10 09:37 index.py
2 files, 234 bytes uncompressed, 163 bytes compressed:  30.3%

Lambda requires global read permissions in order to run the function correctly.

$ zipinfo test.zip
Archive:  test.zip
Zip file size: 473 bytes, number of entries: 2
-r--r--r--  3.0 unx        0 bx stor 17-Aug-10 09:37 exlib.py
-r--r--r--  3.0 unx      234 tx defN 17-Aug-10 09:37 index.py
2 files, 234 bytes uncompressed, 163 bytes compressed:  30.3%

Permissions can be corrected with these commands:

$ chmod 644 $(find /tmp/package_contents -type f)
$ chmod 755 $(find /tmp/package_contents -type d)

Windows:

From your project folder, select your files, open the context (right-click) menu, choose Send to, choose Compressed (zipped) folder, and then type a name for your function folder.

Open the AWS Lambda Console, choose your function, in Code entry type, choose Upload a .ZIP file, and then in Function package, choose Upload.

Navigate to your project folder, choose your zip file for your function, choose Save, and then choose Test.

You can also upload your zip file from the AWS CLI using the update-function-code command.


Did this page help you? Yes | No

Back to the AWS Support Knowledge Center

Need help? Visit the AWS Support Center

Published: 2018-06-29