Dependency injection with AWS Lambdas in java
Disclaimer: for those who wants to see the solution, the github is here
No need to present the AWS Lambdas ; those have been disruptive innovations and they made the app development process smoother and more flexible. In this article, I will expose a way to add dependency injection within your lambdas.
Why dependency injection with nanoservices ?
The question is fair. Indeed, as each function is independent from the other, why bother yourself adding a new level of abstraction and complexity ?
In fact, if you you want to follow the good practices of scalable code, some parts of your code is shared between functions. For instance, the services querying the database. Thus, your source code grows in complexity and becomes similar to a more « classic » web application, inviting you to follow the SOLID acronym.
Off course, depending on your needs and situation, it would be better to not do it but if you target the long run, it would be suicide to unfollow the good practices (exponential growth of maintenance costs…).
As said in the title, we will focus on the dependency inversion principle and one of its application : dependency injection. For production-ready applications, it would be better to rely on a framework and not implement its own container. For it, the java ecosystem have 3 frameworks available : Spring, Guice and Dagger.