DEV Community

arvind singharpuria
arvind singharpuria

Posted on

Express.js deployment in AWS Lambda

We are using this express code snippet:

'use strict'

const express = require('express')
const app = express()

app.get('/', (req, res) => res.send('Hello world!'))

const port = process.env.PORT || 3000
app.listen(port, () =>
  console.log(`Server is listening on port ${port}.`)
)
Enter fullscreen mode Exit fullscreen mode

If you save that code snippet as app.js in a new folder, you are just three steps away from having a simple Express app:

  1. Create a new Node.js project. To do this, run the npm init -y command in your terminal. Just make sure you navigated to the folder that contains app.js first.
  2. Install the Express module from NPM by running the npm install express --save command from terminal.
  3. Run the node app.js command, and you should see “Server is listening on port 3000.” as a response.

Your express app is ready now. Go to http://localhost:3000

Application deployment

We are going to deply or application in aws lambda.

Now we need to do some changes in code to make it production ready. You need to export your app instead of starting the server using app.listen. Your app.js should look like the following code listing:

'use strict'

const express = require('express')

const app = express()
app.get('/', (req, res) => res.send('Hello world!'))

module.exports = app
Enter fullscreen mode Exit fullscreen mode

That would break a local Express server, but you can add app.local.js file with the following content:

'use strict'

const app = require('./app')
const port = process.env.PORT || 3000
app.listen(port, () =>
  console.log(`Server is listening on port ${port}.`)
)
Enter fullscreen mode Exit fullscreen mode

And then run the local server using the following command:

node app.local.js
Enter fullscreen mode Exit fullscreen mode

Now make a AWS Lambda wrapper for your Express app. With Claudia, you can do so by running this code in your terminal:

claudia generate-serverless-express-proxy --express-module app
Enter fullscreen mode Exit fullscreen mode

This step generated a file named lambda.js, with the following content:

'use strict'
const awsServerlessExpress = require('aws-serverless-express')
const app = require('./app')
const binaryMimeTypes = [
  'application/octet-stream',
  'font/eot',
  'font/opentype',
  'font/otf',
  'image/jpeg',
  'image/png',
  'image/svg+xml'
]
const server = awsServerlessExpress
  .createServer(app, null, binaryMimeTypes)
exports.handler = (event, context) =>
  awsServerlessExpress.proxy(server, event, context
)
Enter fullscreen mode Exit fullscreen mode

Now you only need to deploy your Express app (with lambda.js file) to AWS Lambda and API Gateway using the claudia create command.

claudia create --handler lambda.handler --deploy-proxy-api --region eu-central-1
Enter fullscreen mode Exit fullscreen mode

After a few moments, the command finished and printed the following response:

{
  "lambda": {
    "role": "awesome-serverless-expressjs-app-executor",
    "name": "awesome-serverless-expressjs-app",
    "region": "eu-central-1"
  },
  "api": {
    "id": "iltfb5bke3",
    "url": "https://iltfb5bke3.execute-api.eu-central-1.amazonaws.com/latest"
  }
}
Enter fullscreen mode Exit fullscreen mode

And if you visit the link from that response in your browser, it prints “Hello world!” It worked! 🙀

https://miro.medium.com/max/875/1*vEl8mct7Hz-HWJ6_N9Gyqw.png

Awesome. This is your Serverless Express app deployed now.

Top comments (3)

Collapse
 
yongchanghe profile image
Yongchang He

Thank you, sir, for your sharing. I am now learning react.js, and may I ask what Express.js is and why should we use it? Thank you!

Collapse
 
arvind644 profile image
arvind singharpuria

Express js is a backend framework for node js. Node js is backend JavaScript runtime environment. We use both node and express js to do backend stuff.

Collapse
 
yongchanghe profile image
Yongchang He

Thank you!