/aws-serverless-express

Run serverless applications and REST APIs using your existing Node.js application framework, on top of AWS Lambda and Amazon API Gateway

Primary LanguageJavaScriptApache License 2.0Apache-2.0

AWS Serverless Express

Join the chat at https://gitter.im/awslabs/aws-serverless-express Build Status npm npm dependencies Status devDependencies Status

Run serverless applications and REST APIs using your existing Node.js application framework, on top of AWS Lambda and Amazon API Gateway. The sample provided allows you to easily build serverless web applications/services and RESTful APIs using the Express framework.

Getting Started

npm install aws-serverless-express
// lambda.js
'use strict'
const awsServerlessExpress = require('aws-serverless-express')
const app = require('./app')
const server = awsServerlessExpress.createServer(app)

exports.handler = (event, context) => { awsServerlessExpress.proxy(server, event, context) }

Package and create your Lambda function, then configure a simple proxy API using Amazon API Gateway and integrate it with your Lambda function.

Quick Start

Want to get up and running quickly? Check out our basic starter example which includes:

Examples

Getting the API Gateway event object

This package includes middleware to easily get the event object Lambda receives from API Gateway

const awsServerlessExpressMiddleware = require('aws-serverless-express/middleware')
app.use(awsServerlessExpressMiddleware.eventContext())
app.get('/', (req, res) => {
  res.json(req.apiGateway.event)
})

Using resolveMode PROMISE with async handler

// lambda.js
'use strict'
const awsServerlessExpress = require('aws-serverless-express')
const app = require('./app')
const server = awsServerlessExpress.createServer(app)

// note that callback the promise is always resolved
// i.e. errors still lead to a resolved promise
// The resolved promise has a a statusCode property that can be checked for error codes

exports.handler = async (event, context) => { return awsServerlessExpress.proxy(server, event, context, 'PROMISE') }

Using resolveMode CALLBACK with callback function

// lambda.js
'use strict'
const awsServerlessExpress = require('aws-serverless-express')
const app = require('./app')
const server = awsServerlessExpress.createServer(app)

exports.handler = (event, context, callback) => {
  // note that callback is always called with (null, responseObject)
  // i.e. errors still lead to a "successful" callback.
  // The responseObject has a a statusCode property that can be checked for error codes
  awsServerlessExpress.proxy(server, event, context, 'CALLBACK', callback)
}

Is AWS serverless right for my app?

Benefits

Considerations

  • For apps that may not see traffic for several minutes at a time, you could see cold starts
  • Cannot use native libraries (aka Addons) unless you package your app on an EC2 machine running Amazon Linux
  • Stateless only
  • API Gateway has a timeout of 30 seconds, and Lambda has a maximum execution time of 15 minutes.