Hey Everyone. Are you looking for a way to deploy your Node Express API on AWS Lambda? This article is for you. In this article, I will show you how to launch your Node Express API in a lambda function.
One thing you should know about Express servers is that theyâre not built to run on serverless. Also, Lambda is a serverless function, so we will have to modify the Express server a little to get this work done. But itâs pretty straight forward so what we are gonna have to do via using the serverless-http
So let’s go to do that.
Prerequisites
- AWS Account: Ensure you have an AWS account with the permissions to create Lambda functions.
- Node.js and npm: Install Node.js and npm on your local machine.
Step 1: Set Up Your Node Express App
Create a new Node.js Express application or use an existing one. Organize your project with the following structure:
- my-express-app
- node_modules
- index.js
- package.json
- package-lock.json
You should ensure your index.js
contain your Express server logic like the below way.
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => {
res.json({ message: 'Hello from Express on AWS Lambda!' });
});
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
Step 2: Install Serverless Plugins
In your project directory, install the necessary Serverless plugins:
npm install serverless-http
Step 3: Update Your Node Express API for Lambda
Modify your index.js
to use the serverless-http
Package for Lambda compatibility:
const express = require('express');
const serverless = require('serverless-http');
const app = express();
app.get('/', (req, res) => {
res.json({ message: 'Hello from Express on AWS Lambda!' });
});
module.exports.handler = serverless(app);
Step 4: Package Your API and Create Lambda Function
Before deploying your app, package it into a zip folder by selecting all the files and folders(node_modules, index.js, package.json, package-lock.json) inside the my-express-app folder. You can use any compressing tool and zip the files to the app.zip
Create Lambda Function on AWS
- Open the AWS Management Console and navigate to the Lambda service.
- Click on the âCreate functionâ button.
3. Choose âAuthor from scratchâ and fill in the basic details for your function.
4. Configure the runtime to Node.js 18.x
5. Click on the âCreate functionâ button.
6. In the âFunction codeâ section, select âUpload a .zip fileâ in the âUpload Fromâ dropdown.
7. Click the âUploadâ button and select the app.zip
file you created earlier.
Step 5: Create a Test Event for Lambda Function
- In the Lambda function console, navigate to the âTestâ button.
2. Click on the âConfigure test eventsâ button.
3. In the âEvent nameâ field, give your test event a meaningful name, such as âTestEventâ.
4. After clicking the Test, you can see the test event name, response, function logs and request ID.
Step 6: Set Up Function URL for Lambda
- In the Lambda function console, go to the âConfigurationâ tab.
- Go to the function URL in the side menu. And click on the Create function URL button in the section.
3. Select None in the Auth Type and Click on the save.
4. After that, you can get the function URL in the function URL section.
5. You can test your node express API using the function URL.
Congratulations! đ You have successfully deployed your Node.js Express app inside the Lambda function. đ
Conclusion
In conclusion, deploying a Node.js Express server on AWS Lambda opens up exciting possibilities for building scalable and cost-effective serverless applications. While Express servers arenât inherently designed for serverless environments, leveraging the packages serverless-http
makes the process remarkably accessible.
Throughout this article, weâve covered essential steps, from setting up your Express app and installing necessary plugins to packaging the app into a zip folder, creating a Lambda function, and setting up a test event. Providing a secure and scalable entry point for your Express server.
Remember, the seamless integration of Express with AWS Lambda empowers developers to harness the benefits of serverless computing without sacrificing the familiar and powerful Express framework. By following these steps, youâve not only deployed your Node.js Express server on AWS Lambda but also embraced the versatility and efficiency of serverless application development.
As you embark on your serverless journey, explore additional AWS services, monitor and optimize your functions, and keep refining your serverless applications for optimal performance. Cheers to building robust, serverless solutions with Node.js Express on AWS Lambda! đđ
originally published at https://medium.com/aws-in-plain-english/deploying-a-node-express-api-on-aws-lambda-c9730a17f932 on December 27, 2023.
2 Comments
Thanks for Sharing
Thanks Rashmika