Skip to content

How to structure Fastify code for serverless APIs? #546

@dnafication

Description

@dnafication

You have already researched for similar issues?

I have read some discussions around making serverless support better in the future. (fastify/fastify#2746). There aren't much else in the discussions regarding AWS Lambda or Serverless in general.

What are you trying to achieve, or the steps to reproduce?

I have an existing REST API built with Express.js deployed to AWS Lambda using Serverless Framework. It is currently a monolith application i.e. API gateway proxies all requests to a single express route handler. It is easy to swap out express with Fastify and still get a tonne of performance improvement but I want to go a step further. I would like to implement single purpose (or at least common functionalities grouped into one) function that will expose a Fastify instance with its own set of plugins, schemas and routes. Would that be a good idea?

API Gateway
| -- /user/{proxy} --> userHandler 
| -- /products/{proxy} --> productHandler

Or have a Fastify monolith (structure below) where all routes are forwarded to Fastify App which when booted will load all schemas or plugins which may not be needed for the requested route? I really like the directory based routing which comes out of the box with fastify-autoload. Can something like that be implemented?

API Gateway
| -- /{proxy} --> Fastify app handler

Are there any example open source projects where Fastify is used in serverless environment effectively?

What was the result you received?

Not Tested

What did you expect?

Some sample code samples with guidelines of different types of code structuring approaches.

Context

  • node version: 14
  • fastify version: >=3
  • os: Mac, Windows, AWS Lambda
  • any other relevant information:

Please read this entire template before posting any issue. If you ignore these instructions
and post an issue here that does not follow the instructions, your issue might be closed,
locked, and assigned the missing discussion label.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions