Why I don’t like API frameworks together with Serverless
Tags: serverless, api
Published: 2022-05-02 10:57:13

_This blog was originally published [here](https://medium.com/@ishtiaque/why-i-dont-like-api-frameworks-together-with-serverless-39200063b4e0)_

Every time when creating a new API that is meant to be serverless, I consider or have been asked to consider using an API framework together with a serverless application framework like [Serverless Framework](https://www.serverless.com/framework) or [AWS SAM](https://aws.amazon.com/serverless/sam/). So far, I have found none that impress me. Here’s why:

## They do not promote modularity
If you have a look at frameworks like [serverless-http](https://github.com/dougmoscrop/serverless-http) or [nestjs + serverless](https://docs.nestjs.com/faq/serverless), or at [zappa + flask](https://pythonforundergradengineers.com/deploy-serverless-web-app-aws-lambda-zappa.html) for python lovers, all of them follow the same pattern:
1. Instantiate an app
2. Add routes and associated handler functions to the app
3. Add middlewares for authorisation, logging, etc, in the app itself
4. Bundle everything into a single lambda function which gets triggered with the base path `ANY /`


![How your deployed code would look with an API framework](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wqx7ta3c4d2a08qab09k.png)
_How your deployed code would look with an API framework_


You see how heavy-weight it can be. It’s like packing your monolith app into a single lambda function and executing it. Heavier code will lead to longer boot times and hence longer response time compared to the no-framework approach. Poor lambdas are not meant to be used in such a way. You might get away with small apps, but if your monolith is huge, it’s better to use some other solution like containerisation with ECS.
A better solution, in my sense, should be something like this:

![Without any API framework](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tk235cwsi9slu0erjgoc.png)
_Without any API framework_

Every lambda function contains just your business logic and code needed to interact with data sources. Such lightweight, much wow, very fast.

## Why not offload overhead actions elsewhere?
An API does not just include your business logic, it needs to have some other must-have functionalities, like routing, input validation and authorisation, to name a few.

Most of these API frameworks come from times before the serverless revolution, and were designed to create API services to be run on “dumb” infrastructure, dumb in the sense that they were just compute resources, not capable of anything else like routing, request authorisation, etc. Hence, everything had to be done (and written by you!) in the code.

Going truly serverless means that you break your system into parts and then use readily available managed services which take care of individual responsibilities. For example, routing and input validation can be done by the API Gateway. You can have a custom lambda authoriser for authenticating all your requests. For your business logic, you can have lambda functions small enough to focus on one topic per function.
Have a look below:

![A better serverless system with offloaded responsibilities](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0i7dvhwhqx8ml7h7ypnp.png)

## Adds unnecessary learning curve
They add an unnecessary learning curve for the team. A new engineer on the team needs to learn the new framework as well. So why introduce one more thing to learn for your team, when writing small functions is the ultimate goal.

Take django for example. This framework has such a high learning curve and is somewhat opinionated. The goal of lambda functions was to enable developers to execute arbitrary functions, not entire apps, in a lightweight container-like solution. Read that again: “…functions, not entire apps, in a lightweight container-like…”. Using just the Serverless Framework or AWS SAM gives you so much flexibility and speed.

## Downsides
One downside I have noticed working with a barebones framework is that the code is non-standard. Each project uses the Serverless framework, but the code structure is different in each one of them. Here are three different structures I’ve worked with recently:

![Different folder structures](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hrcokstj40uv3v06rpoc.png)

You can see clearly how vastly it varies, mostly depending on the team and it might add to the onboarding time. As said earlier, this is a tradeoff against learning an API framework and the heaviness that comes with it. This can be easily tackled by following the same core principles which these API frameworks use.

Another downside of hyper-granularity is that you end up having many functions that share significant portion of the same code, and now you have that many more cold-starts.

## Conclusion
In tech, no solution is the perfect solution, every single one of them comes with tradeoffs, so it’s up to you to decide which ones you can live with.
Got different opinions? I would love to discuss them. Drop them in the comments below!

---

If you liked this article, sharing it in your network would help a lot!

Connect with me on [LinkedIn](https://www.linkedin.com/in/ishtiaquezafar/) or [Twitter](https://twitter.com/zafarishtiaque) or [Medium](https://medium.com/@ishtiaque)