Spring Boot 3 application on AWS Lambda - Part 2 Introduction to AWS Serverless Java Container
Tags: java, springboot, aws, serverless
Published: 2024-04-01 14:08:33

## Introduction to AWS Serverless Java Container (for Spring Boot 3)
The [AWS Serverless Java Container](https://github.com/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](https://dev.to/aws-builders/measuring-java-11-lambda-cold-starts-with-snapstart-part-6-priming-the-request-invocation-30od) also uses AWS Serverless Java Container.

The container provides adapter logic to minimize code changes. Incoming events are translated to the [Servlet specification](https://jakarta.ee/specifications/servlet/) so that frameworks work as before **.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cqt5kssv2a8fwpxnmmp8.png)

AWS Serverless Java Container delivers the [core container](https://github.com/aws/serverless-java-container/tree/main/aws-serverless-java-container-core) and framework specific container like one for [Spring Boot 3](https://github.com/aws/serverless-java-container/tree/main/aws-serverless-java-container-springboot3) 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](https://github.com/spring-cloud/spring-cloud-function/tree/main/spring-cloud-function-adapters/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](https://docs.spring.io/spring-cloud-function/reference/adapters/aws-intro.html) 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](https://dev.to/aws-builders/measuring-java-11-lambda-cold-starts-with-snapstart-part-6-priming-the-request-invocation-30od). Dependency to the AWS Serverless Java Container was included by default for the [Micronaut on AWS Lambda SnapStart Priming example](https://github.com/Vadym79/AWSLambdaJavaSnapStart/tree/main/micronaut) and needed to be added explicitly for the [Quarkus on AWS Lambda SnapStart Priming example](https://github.com/Vadym79/AWSLambdaJavaSnapStart/tree/main/quarkus) only to implement [web request priming](https://github.com/Vadym79/AWSLambdaJavaSnapStart/blob/main/quarkus/src/main/java/software/amazonaws/example/product/handler/ProductAPIResource.java). 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](https://aws.amazon.com/de/blogs/compute/re-platforming-java-applications-using-the-updated-aws-serverless-java-container/)