Micronaut ❤️ AWS Lambda 🛠
Micronaut is a great way to combine Java with AWS’ Lambda — but the context remains a challenge:
Micronaut provides a lot of pluming and guides on how to use it with AWS Lambda: It provides facilities for regular Lambdas as also API-Gatway integration in proxy mode. 😃
While the guides work great and any Java-Spring developer has a ‘Home sweet home’ feeling right from the get-go; there remain one challenge:
Challenge
While the concept of an (application-) context is second nature for (nearly) every Java developer it is still hard to figure out what can be @Injected
in a Micronaut application — especially in in the AWS Lambda world: Each call to the handler method carries context which needs to be accessed.
This becomes especially important for any non-trivial application that needs access to the authentication details or the Cognito identity (while being integrated via API gateway)
Actually, the javadoc
documentation is the best point to figure out which types can be autowired:
Solution

- To figure out which ‘beans’ can be injected head over to the API summary at
https://micronaut-projects.github.io/micronaut-aws/1.4.1/api/index.html?overview-summary.html - Search for the types you are interested in. Ie. the
AwsProxyRequestContext
- Confirm that there is a matching
ArgumentBinder
type — actually something that implementsio.micronaut.http.bind.bindesr.TypedRequestArgumentBinder
- Use such a type in your Micronaut Lambda:
@Put("/{id}")
public HttpResponse update(
@PathVariable("id") String id,
@Body MyRequest request,
AwsProxyRequestContext context) {
In this example, the AwsProxyRequestContext
is the actual AWS’ Lamda SDK type itself but there exists numerous views on the current request to inject.
As of Micronaut 2.0.0-M3
the following types can be injected as argument:
com.amazonaws.serverless.proxy.model.AwsProxyRequestContext
io.micronaut.http.BasicAuth
com.amazonaws.services.lambda.runtime.Context
- For the body:
-io.reactivex.Maybe
-java.util.concurrent.CompletableFuture
-io.micronaut.http.server.multipart.MultipartBody
-io.reactivex.Observable
-org.reactivestreams.Publisher
-io.reactivex.Single
Regular @Introspected
/ @Singular
still can be @Inject
ed on a class level into the controller:
@Controller("/api/db/")
public class ServerController {
@Inject private DynamoDBServiceImpl dynamoDBService;