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:
- 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 containsapp.js
first. - Install the Express module from NPM by running the
npm install express --save
command from terminal. - 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! 🙀
Awesome. This is your Serverless Express app deployed now.
This content originally appeared on DEV Community and was authored by Arvind
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/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.