Dependency injection with AWS Lambdas in java

Steve Alves-Blyt
The Startup
Published in
3 min readOct 7, 2019

--

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.

--

--

Steve Alves-Blyt
The Startup

Hand-on Engineering Manager. I love sharing my experience/tips with React, React Native, Serverless and Management