DEV Community

Cover image for Deploy a Express JS app in Lambda AWS
Janith Disanayake
Janith Disanayake

Posted on • Edited on

Deploy a Express JS app in Lambda AWS

Introduction

Lambda is a AWS service that provides the server-less application that is easy to run application without maintaining server. It is not expensive and always free under 1000 000 requests. There is no need to use any other service in lambda you just can deploy with Function URL. In this demonstration I will provide the exact steps that you need to follow in order to deploy this application.


Steps 📚

Create a Node JS server

1) Create Directory for application



mkdir lambda-express
cd lambda-express


Enter fullscreen mode Exit fullscreen mode

2) Initialization



npm init


Enter fullscreen mode Exit fullscreen mode

3) Install Express



npm install express


Enter fullscreen mode Exit fullscreen mode

Implementing application

  • This is a simple 'Hello World' application here is the code

1) Creating the file



touch index.js


Enter fullscreen mode Exit fullscreen mode

2) Code of the application



const express = require('express')
const serverless = require('serverless-http')
const app = express()
const port = 3003

app.use(express.json());

app.get('/', (req, res) => {
    res.send('Hello World!');
});



if(process.env.ENVIRONMENT === "lambda") {
    module.exports.handler = serverless(app);
} else {
    app.listen(port, () => {
        console.log(`Example app listening on port ${port}`)
    });
}


Enter fullscreen mode Exit fullscreen mode

3) Test it is running on port 3003



node index.js


Enter fullscreen mode Exit fullscreen mode

Deploy on AWS 🚀

1) Create the .zip file



zip -r lambda.zip node_modules index.js package.json


Enter fullscreen mode Exit fullscreen mode
  • you can use this command to create the zip file or you can download this file -> lambda.zip

2) Create a Lambda function

Image description
Image description
Image description
Image description
Image description

3) Set Environment Variable

set env

4) Now your App is ready

final step

  • You can click Function URL and check the applications is working

Considerations ❗❗

  • This lambda is open to public by Using Public URL
  • It might lead to a DDOS attach by someone else
  • If you want you can manage lambda by using AWS API Gateway

Congratulations 🎉 Just now you deployed your app

click your Function URL and enjoy

happy coding !

Top comments (1)

Collapse
 
pruthuvide profile image
Pruthuvi de Silva

Great article, Janith! I really enjoyed how clearly you explained the steps to deploy an Express.js app in AWS Lambda. The way you broke down each step made it super easy to follow, even for someone new to serverless architectures. The tip on optimizing the app's size before deployment was especially helpful. Looking forward to more content like this.