Deploying NPM Modules in AWS Lambda
Last Updated :
27 Aug, 2024
AWS Lambda definitely does change the way in which developers build and deploy their applications. it provides a serverless computing environment in which code can be executed in response to specific events without the need to manage the environment. When working with AWS Lambda, especially with Node.js, it's common to depend on external libraries or NPM modules to add functionality to your application.
But, as mundane as that might seem, making sure your Lambda function is packaged with the right dependencies is really important. Packaging and deploying NPM modules the right way in an AWS Lambda function will make sure that our code has access to all the libraries it needs in order to execute. This basically consists of setting up your Node.js environment, installing all required modules, packaging your application code along with its dependencies, and then deploying it into AWS Lambda.
In this guide, we will walk you through the steps to effectively package and deploy NPM modules in AWS Lambda, allowing you to harness the full power of your Node.js applications within a serverless environment. This guide is going to help you—be it a single library adding up functionality or taking control over an intricate bundle of dependencies—get all the necessary knowledge to start your Lambda functions with all the NPM modules.
Primary Terminologies
- AWS Lambda: AWS Lambda is a serverless compute service that allows running your code without provisioning or managing servers, it will execute the code in response to events—for example, data changes, system state alterations, or user activity actions.
- Node.js: Node.js is a runtime for executing JavaScript that's built on Chrome's V8 engine, it allows you to do event-driven programming in a non-blocking and concurrent manner.
- NPM (Node Package Manager): NPM is the default package manager for Node.js. It has a vast repository of available packages or modules that developers can use to extend functions in their own applications developed using Node.js, this also helps manage and organize dependencies in a project
- NPM Modules: NPM modules are reusable pieces of code that are designed in such a way that they can be easily used and included in any Node.js application, they could perform specific tasks, like handling HTTP requests, operating with databases, or authentication management
- Package.json: A kind of manifest file that is a descriptor of the project metadata in an NPM-based Node.js project. This includes information regarding the project, such as its name, version, dependencies, scripts, and more. This is extremely important for installing and managing NPM modules.
Step-by-Step Process for Packaging and Deploying NPM Modules in AWS Lambda
Step 1: Install Node.js and NPM
Here’s how to install Node.js 14.x and NPM:
curl -sL https://rpm.nodesource.com/setup_14.x | sudo bash -
sudo yum install -y nodejs
Verify the installation
node -v
npm -v
Step 2: Create Your Node.js Project
Create a New Directory:
Create a directory for your Lambda function and navigate into it:
mkdir my-lambda-function
cd my-lambda-function
Initialize a New Node.js Project:
npm init -y
- This creates a package.json file with default settings.
Step 3: Install NPM Modules
Install Required NPM Modules:
- Use NPM to install any libraries your Lambda function requires. For example:
npm install axios lodash
Step 4: Write Your Lambda Function Code
Create a Lambda Function File:
Create a file named index.js (or another name if you prefer) and write your function code. Example:
const axios = require('axios');
exports.handler = async (event) => {
try {
const response = await axios.get('https://api.example.com/data');
return {
statusCode: 200,
body: JSON.stringify(response.data),
};
} catch (error) {
return {
statusCode: 500,
body: JSON.stringify({ error: error.message }),
};
}
};
Step 5: Package Your Lambda Function
Create a Deployment Package:
- Zip your Lambda function code and dependencies:
zip -r my-lambda-function.zip index.js node_modules
Verify the Package:
Check the contents of the ZIP file to confirm it includes all necessary files:
unzip -l my-lambda-function.zip
We can download zip file to our local desktop by using following command
scp -i stockhol.pem ec2-user@<public-ip>:/home/ec2-user/my-lambda-function/my-lambda-function.zip .
Step 6: Deploy to AWS Lambda
Upload the Deployment Package Using AWS CLI:
- You can create a new Lambda function or update an existing one. Replace the placeholders with your actual values:
Create a New Lambda Function:
#bash
aws lambda create-function \
--function-name my-lambda-function \
--runtime nodejs18.x \
--role arn:aws:iam::001919753054:role/k8 \
--handler index.handler \
--zip-file fileb://my-lambda-function.zip \
--region eu-north-1
Step 7: Test Your Lambda Function
Invoke the Function Manually:
Test your function with a sample event:
aws lambda invoke \
--function-name my-lambda-function \
--payload '{"key": "value"}' \
response.json
Step 8: Check Logs and Output
List Log Groups
aws logs describe-log-groups
List Log Streams
aws logs describe-log-streams --log-group-name /aws/lambda/my-lambda-function
Conclusion
With this, you can easily package and deploy NPM modules in AWS Lambda and, at the same time, utilize the huge ecosystem that Node.js offers in a serverless environment. By understanding important terminologies and following the correct steps, developers can easily create scalable and maintainable Lambda functions. To further simplify the process of deploying Node.js applications and enhance the functionality of Lambda functions, reusable NPM modules are used within the system, proper package setup—dependencies, handler functions—will guarantee seamless execution and top performance. Make use of these skills to unlock the entire power of AWS Lambda for your serverless application.
Similar Reads
How to Load NPM Modules in AWS Lambda? AWS Lambda is a serverless computing service within the event-driven model that Amazon Web Services provides; it is purposed for the execution of code without server provisioning or management. It automatically scales your application to handle from a few requests per day to thousands. This integrab
6 min read
Blue/Green Deployment in AWS Lambda Blue Green Deployment is just like we deploy two versions of our application, one is the stable version, and another is a new feature or bug fix let's say, forwarding a certain percentage of traffic to the second version as well in production to ensure that everything is working fine. The Blue envir
7 min read
Layers in AWS Lambda AWS Lambda refers to the compute service without servers that allows you to execute codes without needing to setup or maintain the digital machines. One of the great feature it has is Lambda Layers which enables people to package and distribute libraries, custom runtime, as well as other dependencie
7 min read
How to Deploy Django Application in AWS Lambda? Pre-requisite: AWS , Python Django is a Python web framework that makes it easy to build web applications quickly and securely. It has a large and helpful community that provides support and contributes to its development. AWS Lambda is a serverless computing platform that runs your code in Docker c
7 min read
AWS Lambda Function Handler in Node.js AWS Lambda is a powerful, effective service offered by Amazon Web Services that enables code to be run in a serverless style. Essentially, this means you don't have to manage underlying infrastructure, worry about scaling, patching, and managing servers; all of this gets done automatically by Lambda
9 min read
How to Test AWS Lambda Locally AWS Lambda is a high-powered, serverless computing service that enables developers to run code without provisioning or managing servers. This service automatically scales, manages infrastructure, and charges only for the compute time consumed. However, developing and testing Lambda functions directl
8 min read