Deploy Node.js Lambda functions with .zip file archives - AWS Lambda

Deploy Node.js Lambda functions with .zip file archives

To create a container image to deploy your function code, see Using container images with Lambda.

A .zip file archive is a deployment package that contains your function code and dependencies. You must create a .zip file archive if you use the Lambda API to manage functions, or if you need to include libraries and dependencies other than the AWS SDK. You can upload the package directly to Lambda, or you can use an Amazon Simple Storage Service (Amazon S3) bucket, and then upload it to Lambda. If the deployment package is larger than 50 MB, you must use Amazon S3.

If you use the Lambda console editor to author your function, the console manages the deployment package. You can use this method as long as you don't need to add any libraries. You can also use it to update a function that already has libraries in the deployment package, as long as the total size doesn't exceed 3 MB.

Note

To keep your deployment package size small, package your function's dependencies in layers. Layers enable you to manage your dependencies independently, can be used by multiple functions, and can be shared with other accounts. For more information, see AWS Lambda layers.

Updating a function with no dependencies

To update a function by using the Lambda API, use the UpdateFunctionCode operation. Create an archive that contains your function code, and upload it using the AWS Command Line Interface (AWS CLI).

To update a Node.js function with no dependencies

  1. Create a .zip file archive.

    ~/my-function$ zip function.zip index.js
  2. To upload the package, use the update-function-code command.

    ~/my-function$ aws lambda update-function-code --function-name my-function --zip-file fileb://function.zip { "FunctionName": "my-function", "FunctionArn": "arn:aws:lambda:us-west-2:123456789012:function:my-function", "Runtime": "nodejs12.x", "Role": "arn:aws:iam::123456789012:role/lambda-role", "Handler": "index.handler", "CodeSha256": "Qf0hMc1I2di6YFMi9aXm3JtGTmcDbjniEuiYonYptAk=", "Version": "$LATEST", "TracingConfig": { "Mode": "Active" }, "RevisionId": "983ed1e3-ca8e-434b-8dc1-7d72ebadd83d", ... }

Updating a function with additional dependencies

If your function depends on libraries other than the AWS SDK for JavaScript, use npm to include them in your deployment package. Ensure that the Node.js version in your local environment matches the Node.js version of your function. If any of the libraries use native code, use an Amazon Linux environment to create the deployment package.

You can add the SDK for JavaScript to the deployment package if you need a newer version than the one included on the runtime, or to ensure that the version doesn't change in the future.

To update a Node.js function with dependencies

  1. Open a command line terminal or shell. Ensure that the Node.js version in your local environment matches the Node.js version of your function.

  2. Install libraries in the node_modules directory using the npm install command.

    ~/my-function$ npm install aws-xray-sdk

    This creates a folder structure that's similar to the following:

    ~/my-function ├── index.js └── node_modules ├── async ├── async-listener ├── atomic-batcher ├── aws-sdk ├── aws-xray-sdk ├── aws-xray-sdk-core
  3. Create a .zip file that contains the contents of your project folder.

    ~/my-function$ zip -r function.zip .
  4. Upload the package using the update-function-code command.

    ~/my-function$ aws lambda update-function-code --function-name my-function --zip-file fileb://function.zip { "FunctionName": "my-function", "FunctionArn": "arn:aws:lambda:us-east-2:123456789012:function:my-function", "Runtime": "nodejs12.x", "Role": "arn:aws:iam::123456789012:role/lambda-role", "Handler": "index.handler", "CodeSha256": "Qf0hMc1I2di6YFMi9aXm3JtGTmcDbjniEuiYonYptAk=", "Version": "$LATEST", "TracingConfig": { "Mode": "Active" }, "RevisionId": "983ed1e3-ca8e-434b-8dc1-7d72ebadd83d", ... }

In addition to code and libraries, your deployment package can also contain executable files and other resources. For more information, see the following on the AWS Compute Blog: