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(`S…


This content originally appeared on DEV Community and was authored by Arvind

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}.`)
)

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

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}.`)
)

And then run the local server using the following command:

node app.local.js

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

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
)

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

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"
  }
}

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.


This content originally appeared on DEV Community and was authored by Arvind


Print Share Comment Cite Upload Translate Updates
APA

Arvind | Sciencx (2022-07-11T17:12:19+00:00) Express.js deployment in AWS Lambda. Retrieved from https://www.scien.cx/2022/07/11/express-js-deployment-in-aws-lambda/

MLA
" » Express.js deployment in AWS Lambda." Arvind | Sciencx - Monday July 11, 2022, https://www.scien.cx/2022/07/11/express-js-deployment-in-aws-lambda/
HARVARD
Arvind | Sciencx Monday July 11, 2022 » Express.js deployment in AWS Lambda., viewed ,<https://www.scien.cx/2022/07/11/express-js-deployment-in-aws-lambda/>
VANCOUVER
Arvind | Sciencx - » Express.js deployment in AWS Lambda. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/07/11/express-js-deployment-in-aws-lambda/
CHICAGO
" » Express.js deployment in AWS Lambda." Arvind | Sciencx - Accessed . https://www.scien.cx/2022/07/11/express-js-deployment-in-aws-lambda/
IEEE
" » Express.js deployment in AWS Lambda." Arvind | Sciencx [Online]. Available: https://www.scien.cx/2022/07/11/express-js-deployment-in-aws-lambda/. [Accessed: ]
rf:citation
» Express.js deployment in AWS Lambda | Arvind | Sciencx | https://www.scien.cx/2022/07/11/express-js-deployment-in-aws-lambda/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.