DEV Community

Vadym Kazulkin for AWS Community Builders

Posted on • Edited on

Spring Boot 3 application on AWS Lambda - Part 2 Introduction to AWS Serverless Java Container

Introduction to AWS Serverless Java Container (for Spring Boot 3)

The AWS Serverless Java Container makes it easier to run Java applications written with frameworks such as Spring, Spring Boot 2 and 3, or JAX-RS/Jersey in Lambda. We have already seen that Micronaut framework also uses AWS Serverless Java Container.

The container provides adapter logic to minimize code changes. Incoming events are translated to the Servlet specification so that frameworks work as before **.

Image description

AWS Serverless Java Container delivers the core container and framework specific container like one for Spring Boot 3 which is the area of our interest for this article. There also other containers for Spring, Struts and Jersey frameworks. The major update to the version 2.0 has been recently released for all AWS Serverless Java Containers. If we look into the whole dependency tree, we'll discover another dependency spring-cloud-function-serverless-web that artifact aws-serverless-java-container-springboot3 requires which is the collaboration effort between Spring and AWS Serverless developers. It provides Spring Cloud Function on AWS Lambda functionallity. We'll look deeper into the capabilities of Spring Cloud Function on AWS Lambda in one of my upcoming articles.

AWS Serverless Java Core Container provides also abstractions like AWSProxyRequest/Response for mapping of API Gateway (Rest) request to the Servlet model including different authorizers like the Amazon Cognito and HttpApiV2JwtAuthorizer.

Then everything will be proxied internally in the core container through the AwsHttpServletRequest/Response abstractions or their derivates like AwsProxyHttpServletRequest.

My personal wish is that a subset of abstractions i.e. from the com.amazonaws.serverless.proxy.model package like

-AwsProxyRequest
-ApiGatewayRequestIdentity
-AwsProxyRequestContext
-AwsProxyResponse

and others will be a part of a separate project and therefore also used without the usage of the all other AWS Serverless Java Container APIs only for purpose of mocking the API Gateway Request/Response (i.e. for Priming). I've already used them for Priming requests for Quarkus and Micronaut frameworks. Dependency to the AWS Serverless Java Container was included by default for the Micronaut on AWS Lambda SnapStart Priming example and needed to be added explicitly for the Quarkus on AWS Lambda SnapStart Priming example only to implement web request priming. We'll make use of these abstractions in one of our subsequent articles when we'll discuss cold and warm start time improvements for Spring Boot 3 application on AWS Lambda using AWS Lambda SnapStart in conjunction with priming techniques.

The Lambda runtime must know which handler method to invoke. For this AWS Serverless Spring Boot 3 Container which internally uses AWS Serverless Java Core Container adds only several implementations on top that like SpringDelegatingLambdaContainerHandler or implement our own handler Java class that delegates to AWS Serverless Java Container. This is useful if we want to implement additional functionality like Lambda SnapStart priming technique. This can de done by using SpringBootLambdaContainerHandler abstraction (which inherits AwsLambdaServletContainerHandler from the core container) which can be created by giving SpringBoot class which is annotated with @SpringBootApplication as an input. For Spring Boot 3 applications that take longer than 10 seconds to start, there is an asyncronous way of creating SpringBootLambdaContainerHandler by using SpringBootProxyHandlerBuilder abstraction. Since version 2.0.0 it always runs asynchronously by default, in the prior versions we had to invoke asyncInit method (which is now became deprecated) to run the builder asynchronously. I'll provide more detailed explanation about this with code examples in the next article of the series.

Conclusion

In this article we introduced AWS Serverless Java Container components like core and framework specific adaptors like for Spring Boot 3. In the next article of the series we'll explore how to develop the Lambda functions using AWS Serverless Java Container (for Spring Boot) which receives the request from Amazon API Gateway and stores to and read from Amazon DynamoDB.

** Re-platforming Java applications using the updated AWS Serverless Java Container

Top comments (0)