Open In App

Deploying NPM Modules in AWS Lambda

Last Updated : 27 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

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 -
Install Node.js and NPM
sudo yum install -y nodejs
sudo yum install -y nodejs

Verify the installation

node -v
npm -v
Verify the installation

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
Create Your Node.js Project

Initialize a New Node.js Project:

npm init -y
  • This creates a package.json file with default settings.
npm init -y

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
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 }),

};

}

};

Write Your Lambda Function Code

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
Package Your Lambda Function

Verify the Package:

Check the contents of the ZIP file to confirm it includes all necessary files:

unzip -l my-lambda-function.zip
Verify the Package

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

Deploy to AWS Lambda

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

Test Your Lambda Function

Step 8: Check Logs and Output

List Log Groups

aws logs describe-log-groups
aws logs describe-log-groups

List Log Streams

aws logs describe-log-streams --log-group-name /aws/lambda/my-lambda-function
List Log Streams

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.


Article Tags :

Similar Reads