Skip to content

Node.js express on AWS lambda, deploying with Serverless framework!

License

Notifications You must be signed in to change notification settings

toduq/express-on-serverless

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Express on Serverless

Now you can run Node.js express on AWS Lambda, using Serverless framework!

What's this?

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

Features

  • 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)

Setup

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

Local Testing

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

Deploying to Amazon Web Service

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

Checking on what happened in AWS

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.

Conclusion

It's too easy!!

About

Node.js express on AWS lambda, deploying with Serverless framework!

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published