IoC (Inversion of control) Java framework for using in AWS Lambda

Viewed 1452

On my current project I'm working on AWS Lambda function written on Java. At some moment the structure of the function has become quite complex, there became a lot of related components and I decided that would be more clear and convenient to use some IoC framework/library for reducing close coupling and complexity.

Previously I worked only with Spring Framework, but I know that it's pretty highweight stuff and I afraid that with Spring I will reach Lambda limits pretty fast (package size, execution duration, etc). Due to that I'm looking for some alternatives which would fit to AWS Lambda limits and ideology. What would be the best option for my case?

5 Answers

Try using micronaut (http://micronaut.io/). New framework in town still promising for a distributed environment. Especially brings in a lot of benefits by performing DI during buildtime thus reducing startup time and few memory overheads as it avoids reflection during runtime. Worth giving it a try!

Any IoC container that uses code generation should be a good choice. Dagger is one example.

However, depending on what exactly your lambdas do, Spring can still be an appropriate choice.

  • For example, an extra 15 seconds to initialize the context compared to total 5 minute run time is neglectable. If your lambda gets called more than once, the existing lambda instance is very likely to be reused which means, that you can reuse the existing Spring context and avoid the overhead completely.
  • On the other hand, using Spring for lambda that handles API Gateway requests is not the best idea (though AWS provides a nice library that makes Spring MVC apps run in lambda environment), because the typical expectation is that the response should come in <1 second (which is sure not possible if you need 15 seconds just to initialize the context)

You can try hk2 (https://javaee.github.io/hk2/). It is specifically meant for light-weight IoC and has a lot of useful security and AOP features that might be useful for you. It can also help with reducing start-up/classloading costs because the services are only instantiated when requested (normally). Hope you find what you are looking for!

Another good option I found - Quarkus framework, which has compile-time oriented dependency injection, provide ability to compile native applications using GraalVM and has extension for easy creation of Lambda functions.

Related