When it comes to incorporating npm modules into your AWS Lambda functions, there are two primary approaches to consider:
Direct Inclusion: This method involves directly zipping the
node_modules
directory along with yourapp.js
handler file. It's a simple and straightforward approach, ideal for smaller projects.๐กHowever, it's important to note that AWS Lambda has a file size limit of50 MB
. If your npm modules exceed this limit, you'll need to explore an alternative approach.Lambda Layers: For larger projects or scenarios where you want to reuse the same third-party libraries across multiple Lambda functions, Lambda Layers come into play. These layers enable you to separate your npm modules from your function code, making it easier to manage and share dependencies.
In this blog, we will explore both of these methods, demonstrating how to work with npm modules in AWS Lambda, with and without Lambda Layers.
How to use npm modules Without Layers in AWS Lambda
To use npm modules in a Lambda function, you have to:
Have the following folder structure:
app.js package.json node_modules your_npm_modules
First, initialize the
package.json
file and install the necessary modules, in the directory where theapp.js
the file is located:npm init -y npm install axios
Let's fill out the code for the
app.js
file, which makes use of thenpm
module:const axios = require('axios'); exports.handler = async (event, context) => { try { const response = await axios.get('https://example.com/api/data'); // Replace with your API endpoint. const data = response.data; return { statusCode: 200, body: JSON.stringify(data), }; } catch (error) { return { statusCode: 500, body: JSON.stringify({ message: 'Error occurred' }), }; } };
Open your terminal in the directory where
app.js
andnode_modules
are and zip the folder's contents:zip -r lambda.zip .
๐กWe have to zip the contents of the directory, not the directory itself. When the archive is extracted lambda will look for aapp.js
file, not asome-directory/app.js
file.Update the code of the lambda function, by uploading the
lambda.zip
file. Open your terminal in the directory where thelambda.zip
file is located and run theaws lambda update-function-code
command:aws lambda update-function-code --function-name <YOUR_LAMBDA> --zip-file fileb://lambda.zip
๐กfileb://: This is used for binary files. The content of the file is treated as binary data. For Lambda function code, which is typically a binary ZIP archive, you should usefileb://
.Verify that the lambda function's handler is set to
app.handler
(anapp.js
file exporting a function namedhandler
).Invoke the lambda function and verify that it can use the npm module:
aws lambda invoke --function-name yourFunctionName --cli-binary-format raw-in-base64-out --payload '{"name": "mikaeel"}' response.json
๐กNow that we've zipped theapp.js
file and thenode_modules
directory our lambda function can use the npm modules in it.However, now we can't use the online editor. Our deployment package is too large, due to the size of the
npm_modules
folder.Also, what if we need a particular
npm
package in multiple functions? It doesn't seem very efficient to repeat this process for every function.This is when lambda layers come in.
How to use npm modules With Layers in AWS Lambda
To use npm modules, with layers, in a lambda function, you have to:
Have the following folder structure:
app.js layers axios nodejs package.json node_modules
๐กNote that thenodejs/node_modules
folder structure is what lambda expects when working with layers in Node.js - see source here.Open your terminal in the
layers/axios/nodejs
directory and install your npm modules:npm init -y npm install axios
Open your terminal in the
layers/axios
directory and zip the contents.๐กWhen the zip archive is extracted, thenodejs/node_modules
directory structure has to be directly accessible.# in the layers/axios directory zip -r axios-layer.zip .
To create or update the lambda layer, open your terminal in the
layers/axios
directory and run thepublish-layer-version
command:aws lambda publish-layer-version --layer-name axios-layer --description "add axios library" --zip-file fileb://axios-layer.zip --compatible-runtimes nodejs14.x nodejs16.x
Attach the layer to the lambda function. The easiest way to add a layer to a function is by using the AWS Lambda console.
a. In the
Code
tab scroll down to theLayers
section and clickAdd a layer
.b. Select
Custom layers
and pick your layer from the drop-down menu, selecting the latest version.The last step is to update the code of the lambda function. Enter the following code in your
app.js
file:const axios = require('axios'); exports.handler = async (event, context) => { try { const response = await axios.get('https://example.com/api/data'); // Replace with your API endpoint. const data = response.data; return { statusCode: 200, body: JSON.stringify(data), }; } catch (error) { return { statusCode: 500, body: JSON.stringify({ message: 'Error occurred' }), }; } };
The code simply imports the npm module from the layer and makes use of it.
Open your terminal in the directory where the
app.js
the file is located and zip the file.๐กNote that we are zipping theapp.js
file, not the folder that contains it.zip -r lambda.zip app.js
Finally, let's update the function's code. Open your terminal in the directory where the
lambda.zip
the file is located and run theupdate-lambda-code
command:aws lambda update-function-code --function-name yourFunctionName --zip-file fileb://lambda.zip
Verify that the lambda function's handler is set to
app.handler
in lambda console (anapp.js
file exporting a function namedhandler
).Invoke the lambda function and verify that it can use the npm module from the layer.
The
node_modules
directory is not visible in the online code editor and now we can view the code of our lambda function.๐กYou can reuse the lambda layer for multiple functions. You can even include multiplenpm
modules or even your helper functions that you reuse between your lambdas.