Code structuring on large API / generic serverless AWS beginner questions

Hi there,

I’m pretty new to serverless framework and AWS, therefore I have some questions on general nature, before the project ends in a mess :slight_smile: We plan to have a quite large REST-API (maybe 100 “routes”) on AWS Lambda nodejs, but there are still some questions left from my side.

  1. Does it make sense to use one service function for the whole API? Like for example This would reduce in my eyes at least the problem with cold starts (because there will be only 1 generic lambda instead of hundreds). On the other hand, this will cause problems with documentation and also prevent us deploying some services/functions only

  2. From what I see, each function in serverless creates one specific lambda version. Which leads me to the problem already mentioned above. Cold starts. I’m not sure if i should be afraid of it, but I heard some stories. Would it make sense to reduce the code size here for each function? For example only include the NPM packages which are needed by this specific function? If so, how could this be accomplished? NPMs bring a lot of dependencies, so i will have to have huge lists of what to include where.

  3. How does serverless packaging work in general? Do i have to install my NPM packages locally exactly in the node version which is offered remote? Where and when is the “npm install” performed?

  4. Is there any larger API project in nodejs/serverless which i can take as a reference for code structuring?

  5. Whats the recommended deployment process with serverless? I ready everywhere about the CLI deployment, but that about ci/cd? HOw to integrate serverless here, or does it not make that much sense? Any articles on this, my research did not find anything relevant

  6. What about Database connections and similar? Should they be persistant or closed after function call? From what i see it would make sense to keep them open, as long as the lambda container is up and running

Okay, a lot of questions, maybe someone has any clue about any topic here, I would be really glad.


I have a little experience that may help. I designed an api that I did not consider large. It worked fine in development using the offline plugin. When I deployed it I ran into problems. See Deploy error: number of resources greater than 200

To address Question #1

I had to break my project into sub services which caused a lot of pain. I use a serverless.yaml at the top of my project for offline development. I have half a dozen sub-services with their own serverless.yaml for deployment. I use the serverless-domain-manager plugin to manage the api-gate, but it requires a prefix on the url, which makes my front end my complicated.

Question #3

I use nvm to manage node versions. I work in both node 6 and 8 without problem. I do not know about how the packaging works because it just works. Run npm (or yarn) to install your dependencies locally and they will be copied when deploying.

Question #5

I use the cli and have no problems. There is a project for deploy multiple for deploying multiple project but I have not used it.

Question #6

I found if the database connection had to close or else the function would time out.

I hope this helps a bit.

1 Like

Many thanks for your input and thoughts