How to Structure Serverless Framework Applications?

So, I’m new to serveless and have been trying to create a serverless backend for my project and scratching my head over the best way to organize things. Initially, I thought of laying it out like this:

|--src
|    |--auth-service
|    |--products
|    |--customers
|--package.json
|--serverless.yml

However, I quickly realized an issue – the node_modules folder. For instance, the auth section requires the bcrypt package, whereas the customers section does not. Deploying the same node_modules folder to every function includes unnecessary packages.
So, I came up with a different game plan:

|--customers
|    |--node_modules
|    |--handler.js
|    |--package.json
|    |--serverless.yml
|--products
|    |--node_modules
|    |--handler.js
|    |--package.json
|    |--serverless.yml
|--auth-service
|    |--node_modules
|    |--handler.js
|    |--package.json
|    |--serverless.yml

Here, the node_modules folder is not global but is scoped to contain only the packages required by each function.

With this setup, I have to deploy by running serverless deploy within each folder.

  • Question 1)
    A challenge remains – how do I configure “auth-service” (which has a function checking JWT) as an authorizer for the “customers” and “products” functions within their respective serverless.yml files? My initial thought was to use the ARN for the auth function and set it as the authorizer for all other functions, but I am not sure if this is a good approach

  • Question 2)
    Is there a better way to structure the project, especially since now deploying resourcing is a pain, for example if I want a dynamodb table, I should’nt be defining it as a resouce inside a particual functions serverless.yml file, right? So is there a way to kind to bundle all these different services, and maybe define resources in centralized place.

I just wondered what the best approach to build and create all that stuff would be? My first idea was having an own Lambda project for each of the endpoints I plan to have. I wanted to create all the resources within one stack with cdk. 192.168.100.1 192.168.1.1