Serverless vs AWS SAM (aws-serverless-express)

The aws-serverless-express README is nice and simple and you can quickly get up and running with a single endpoint, single-path express app. Then there’s a quantum leap to their “example” which seems the ditch the serverless.yml file and explode in complexity to the AWS SAM configuration, which seems to obviate the nice abstractions that serverless makes.

Is there a simpler “pure” serverless.yml way to configure AWS/Express and/or is this an attempt by AWS to take-over serverless by with AWS lock-in configurations?

2 Likes

Hey @richburdon, there sure is! You should check out the awesome serverless-http plugin to work with that.

You can get started with the following steps:

  1. Install serverless-http by running: npm install --save serverless-http

  2. Update your Express index file. You’ll need to import the serverless-http library at the top of your file:

const serverless = require('serverless-http');

then export your wrapped application:

module.exports.handler = serverless(app);.

For reference, an example application might look like this:

#index.js

const serverless = require('serverless-http');
const express = require('express')
const app = express()

app.get('/', function (req, res) {
  res.send('Hello World!')
})

app.get('/:name', function (req, res) {
  res.send(`Hello ${req.params.name}!`)
})

module.exports.handler = serverless(app);
  1. Set up your serverless.yml. You can use a single function that captures all traffic:
# serverless.yml

service: express-app

provider:
  name: aws
  runtime: nodejs6.10
  stage: dev
  region: us-east-1

functions:
  app:
    handler: index.handler
    events:
      - http: ANY /
      - http: 'ANY {proxy+}'

Just make sure your handler refers to the file and exported function of your Express app.

Let me know if that works for you!

2 Likes

So in this example if I just wanted to work with get functions I could call handler.get in serverless.yml? Also I could name it app rather than handler? Do nderstand this correctly?

Does similar package existing for python as well? I tried wsgi wrapper but ran into some issues. I’m just more fluent in Python than js