Search code examples
serverless-framework

folder structure for serverless framework handlers


So I am working on a serverless framework project( and am fairly new to it ) and I am struggling to choose a folder structure for my lambda handlers.

for example, say I have a "user" path and I want to do CRUD operations for users. in my project directory I would have the following:

  • serverless.yml
  • src/users/index.js

the index.js would contain all of the handlers: e.g. module.exports = {createUser,updateUser,deleteUser,getUser}

the alternative would be to use a folder structure with the following:

  • serverless.yml
  • src/users/createUser/handler.js
  • src/users/updateUser/handler.js
  • src/users/deleteUser/handler.js
  • src/users/getUser/handler.js

are there any implications of using the first approach in comparison to second approach when deploying the serverless application?

note: the first approach is not using a mono function , rather just having all the handlers for the path inside that one module.


Solution

  • If you're not using a mono-lambda API pattern, then I suggest grouping files by function - which in this case is the first option you listed, along with specifying individual function packaging with:

    package:
      individually: true
    

    in your serverless.yml template. This ensures that only the code needed by a specific REST resource is included in that function.

    If you want to learn more about the tradeoffs and differences of a mono-lambda API vs a single-function API, you can read my post.