6

when code is executed in amazon aws lambda, my @autowired spring dependencies are null. Makes sense if there is no context being loaded, but i thought SpringBeanAutowiringSupport would help. How do you inject dependencies correctly in amazon lambda?

This is my code that has null autowired fields but otherwise works fine (if i replace autowired with new:

@Component
public class ApplicationEventHandler {

@Autowired
private Foo foo;


         public ApplicationEventHandler() {
             logger.info("I'm sure the constructor is being called");
            SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
             //doesn't seem to help
         }

         public void deliveryFailedPermanentlyHandler(SNSEvent event, Context context) throws IOException {
             foo.doStuff() // causes NPE

         }

thanks in advance!

Ersoy
  • 8,816
  • 6
  • 34
  • 48
bsautner
  • 4,479
  • 1
  • 36
  • 50
  • 3
    Here is a simple way with no "magic" involved: [Just Spring-enabled AWS Lambdas](https://www.profit4cloud.nl/blog/just-spring-enabled-aws-lambdas/) – Attila T Feb 09 '18 at 19:51
  • I've just tested what Attila T is saying, and it worked. Thx mate! – brebDev Jul 31 '19 at 10:13

2 Answers2

3

this project on github provides a template for what i'm trying to do which works fine:

https://github.com/cagataygurturk/aws-lambda-java-boilerplate

bsautner
  • 4,479
  • 1
  • 36
  • 50
  • Hi! I recommend to see also this framework: https://github.com/lambadaframework/lambadaframework – Cagatay Gurturk Mar 31 '16 at 18:08
  • I think it makes sense that AWS lamda isn't executing your code like a spring aware servlet container would, so there is no application context and dependency injection doesn't happen. Lambas are supposed to be small units of work anyway, so if you needed a more complex application the lambda function should call a webservice running on another platform. – bsautner Jul 09 '17 at 15:41
  • In Spring, your class is initiated by Spring itself and then all DI annotations are scanned to provide dependencies. AWS Runtime does not do such a thing, that's why annotations are not evaluated. – Cagatay Gurturk Sep 02 '17 at 13:20
  • The boilerplate in the repo from @ÇağatayGürtürk works for the most part, but it doesn't resolve the Value's from the properties files. I tried using the solution here but it seems like the properties file is getting read (if I change the file name it fails) but the Values are missing. https://stackoverflow.com/a/14168026 – naren8642 Mar 08 '23 at 04:23
2

AWS Lambda Best Practices notes talks about DI:

Minimize the complexity of your dependencies. Prefer simpler frameworks that load quickly on Execution Context startup. For example, prefer simpler Java dependency injection (IoC) frameworks like Dagger or Guice, over more complex ones like Spring Framework.

So I'd like to suggest you to use Dagger 2 (because Square's Dagger 1.x is already deprecated). It provides such benefits:

  • light-weight framework with very few integrations, Java interface/annotation configuration and compile-time code generated bindings;
  • very small size;
  • fail as early as possible ( compile-time, not runtime);
  • performance - as fast as hand-written code and no coding around the framework.
Hleb
  • 7,037
  • 12
  • 58
  • 117