Home » Tutorials » How to use Guice in a Lambda on AWS for dependency injection

How to use Guice in a Lambda on AWS for dependency injection

  • by
Google Guice

Despite AWS recommending to use Dagger for dependency injection in your Java lambdas, you might be required to use Guice as your injection framework, for example to port an existing application running somewhere else into a lambda, and without the time to make the change.

Note that if you are starting a new application, you should rather use Dagger, mainly because it handles the dependency injection at compile time, instead of runtime for Guice. This will save time during the warmup of your lambda, which makes it overall faster to run.

Let’s assume you have a service, with a DAO injected in it, and you would like to use this service in your lambda. Usually, it would be directly injected with the @Inject annotation, but in the case of the lambda, it will slightly differ. Anyway, here’s an example service and DAO:

@Named
public class MyService {

    @Inject
    private MyDAO dao;

    public Entity store(Entity entity) {
        entity.setCreatedAt(new Date());

        return dao.store(entity);
    }

}

@Named
public class MyDAO {

    public Entity store(Entity entity) {
        // Do some stuff in our database...

        return entity;
    }

}

This is a very simple use case, but rest assured, any feature of Guice will be supported, so you can also inject parameters in the constructor or in methods, etc.

Alright, the next step is to have a Guice module that will register what you need to, just like you would have in a normal Guice application. Actually you can most likely use the module that you already have!

import com.google.inject.AbstractModule;

public MyModule extends AbstractModule {

    @Override
    public void configure() {
        install(new ComponentScanModule("com.foo.mypackage", Named.class));
    }

}

Now, one thing left, the only part that will differ from what you would usually be doing: we need to manually create the Guice injector when we initialize our lambda, and from there access the service that we’ll be using.

public class MyLambdaClass {

    private Injector injector;
    private MyService service;

    public MyLambdaClass() {
         // Initializing the injector with your module - you can use multiple modules if needed
        this.injector = Guice.createInjector(new MyModule());
        // Create the service that you need, the injected fields will be filled automatically
        this.service = this.injector.getInstance(MyService.class);
    }

    public void myLambdaFunction() {
        Entity e = new Entity();
        e.doMyStuff();

        this.service.store(e);
    }

}

From there on, you can use the injector to create any component that you need, and all the dependencies will be automatically wired, even if they are nested other dependencies! Remember that any of the other features of the dependency injection framework will be working just fine aswell, and you can just install other modules in your initial module!

This concludes this small article about dependency injection in AWS Lambda, it is something that might appear as difficult to set up in the first place, before diving in, but it’s actually very simple and takes only a few lines of code!

Leave a Reply

Your email address will not be published. Required fields are marked *