DEV Community

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

Posted 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
  1. Initialization
npm init
Enter fullscreen mode Exit fullscreen mode
  1. Install Express
npm install express
Enter fullscreen mode Exit fullscreen mode

Implementing application

  • This is a simple 'Hello World' application here is the code
  • Creating the file
touch index.js
Enter fullscreen mode Exit fullscreen mode
  1. 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
  1. 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
  1. Create a Lambda function

Image description
Image description
Image description
Image description
Image description

  1. Set Environement Variable

Image description

  1. Now your App is ready

Image description

  • 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

Top comments (0)