Now you can run Node.js express on AWS Lambda, using Serverless framework!
I want to run express on AWS Lambda and deploy it using Serverless.
This can be run with express, koa or other Node.js servers as far as it is requestListener
- express
- GET/POST request
- POST json with body-parser
- POST urlencoded with body-parser
- POST binary file with multer
(need to enable API Gateway binary support)
Install npm modules and generate template.
npm install -g serverless
npm install -S express express-on-serverless
serverless create --template aws-nodejs
Modify handler.js and serverless.yml
// handler.js
const express = require('express')
const app = express()
app.get('/test', (req, res) => {
res.send("I'm fine!")
})
exports.index = require('express-on-serverless')(app)
// Used when run locally
if(!module.parent){
let port = process.env.PORT || 3000;
app.listen(port)
console.log("Listening on port %s",port);
}
# serverless.yml
service: aws-nodejs
provider:
name: aws
runtime: nodejs4.3
functions:
index:
handler: handler.index
events:
- http:
path: /
method: any
- http: any {proxy+}
Assuming you used the example above, the Express app will start and listen on port 3000 (process.env.PORT
) if the server is started directly instead of loaded as a module by Lambda. To start the server locally, you can use the default start
command built in to either npm
or yarn
:
npm start
# Same behavior, if you prefer yarn
yarn start
First, configure your AWS credentials if you haven't already.
The same command is used for both the initial deploy as well as updates.
serverless deploy
Now you can access https://API_GATEWAY_HOST/dev/test
!
See serverless deploy --help
for a summary of available options and the [official serverless docs for deploy](https://serverless.com/framework/docs/providers/aws/guide/deploying/ for the full docs on serverless deployment).
serverless
creates new changes in CloudFormation, API Gateway, and Lambda. Check out the AWS Console for each service to review the impact of your changes.
It's too easy!!