Skip to content

Handle requests without server (AWS Lambda) #29

@rafaelrenanpacheco

Description

@rafaelrenanpacheco

Hi there!

I would like to use Fastify with AWS Lambda the same way AWS Lambda works with Express. For example, the serverless framework has this documentation explaining how to use Express in AWS Lambda. The gotcha here is the serverless-http dependency, which does not fire the express listening server, but instead forwards the requests into the framework handler. In order to do that, express has to expose a handler function.

I tried to port this example using Fastify, but the serverless-http was unable to forward the request to fastify.

Does fastify expose some function do manually call the request handler method?

Here are the steps to reproduce the error:

npm install -g serverless

package.json

{
  "name": "aws",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "fastify": "^1.13.0",
    "serverless-http": "^1.8.0"
  },
  "devDependencies": {
    "serverless-offline": "^3.31.3"
  }
}

serverless.yml

service: my-fastify-application

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

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

plugins:
  - serverless-offline

index.js

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

fastify.get('/', (request, reply) => {
  reply.send({ hello: 'world' })
});

module.exports.handler = serverless(fastify);

Then run:

npm install

sls offline start

And open in your browser:

http://localhost:3000/

This should thrown an error at serverless-http/lib/get-handler.js because it was unable to forward the request to fastify.

Fastify working well with AWS Lambda would be amazing.

Best regards,
Rafael Pacheco.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions