serverless-express icon indicating copy to clipboard operation
serverless-express copied to clipboard

Support for multiple lambdas per express app

Open lucas-rudd opened this issue 5 years ago • 1 comments

I'm very interested in using serverless-express for an application. However, it seems as though, when using this plugin, we will only able to deploy a single lambda to handles all of our routes.

There are two things that concern me with this architecture.

1: Due to the way serverless functions operate, there is a cold-start time when spinning up new containers. Because only one lambda is handling all requests, it's likely that it will increase the number of cold-starts than would occur with one lambda per method.

2: This requires all the application code in a project to be uploaded to a single function. There are limits that Cloud Providers place on the amount of code you can upload to a single function in their environment.

Some back-end projects utilize webpack to do tree-shaking and reduce the overall size of the deployment when using serverless framework. Others include/exclude files on a per function basis to leave out unnecessary files; however, because a single function has the code for all routes, the optimization that can be done is severely limited, leading to bloated function sizes. It's foreseeable that this could lead to the deployment limit being exceeded and the project being unable to be deployed. This is especially true for functions bundled together with node_modules.

This plugin has a variety of values, and we'd like to make use of it so that we have the flexibility to switch from a serverless architecture to a traditional server architecture if necessary.

Is there any way to split out the express application and deploy multiple functions based on the route rather than a single function?

So, instead of having your severless.yml file looking like this:

functions:
  app: 
    handler: handler.handler
    events: 
      - http:
          path: GET
          method: /users

It would look like this

functions:
  get: 
    handler: handler.handler.get
    events: 
      - http:
          path: GET
          method: /users
  post:
    handler: handler.handler.post
    events:
      - http:
           path: POST
           method: /users

lucas-rudd avatar Apr 02 '20 21:04 lucas-rudd

These are all very good points. #1 can be mitigated with provisioned concurrency: https://serverless.com/blog/aws-lambda-provisioned-concurrency/

I think you can do what you want currently, but you would still be uploading the full express app to each individual lambda function. 50mb zipped is a pretty generous limit and I doubt you will hit that soon if using webpack. If you do, I would argue your express app is doing too much and should be refactored into multiple apps.

mikestaub avatar Apr 05 '20 15:04 mikestaub