TL;DR
Ever wanted to use a Java framework such as Spring Boot with AWS Lambda?
In conclusion, it's relatively easy to put a proxy framework called the aws-serverless-java-container framework on top of AWS Lambda. Is feasible.
Today, I would like to write about that while lightly translating this AWS blog.
AWS Open Source Blog Running APIs Written in Java on AWS Lambda
Java developers often develop with familiar frameworks such as Spring, Spring Boot, Jersey, and Spark.
These frameworks can be used as an executable Jar containing an application or by deploying a built War or Jar using a Servlet container such as Tomcat.
With AWS serverless, you can build a WEB backend with AWS Lambda and Amazon API Gateway, but Lambda is responsible for computing and API Gateway is responsible for REST management.
In this article, I will introduce the aws-serverless-java-container framework that can be used with Lambda.
Simply put, aws-serverless-java-container is made with frameworks such as Spring, Spring Boot, Jersey, and Spark. It makes it easy for you to run your application within AWS Lambda with minimal code changes.
aws-serverless-java-container acts as a proxy between the Lambda runtime and the selected framework, pretending to be a Servlet engine. It transforms the Event from the API Gateway into a request object that the framework can understand, and the response object from the application into a format that the API Gateway can understand.
If you want to build your own app with Spring Boot 2 on Lambda, here is the detailed installation method. Quick start Spring Boot2
Let's clone aws-serverless-java-container from awslab. It contains proxies and sample code for various Java frameworks.
$ git clone https://github.com/awslabs/aws-serverless-java-container.git
Click here for proxies for your own applications
You can use these to build your own app, but for the sake of simplicity, I'd like to use the Spring Boot 2 sample in this article.
First, let's meet the following prerequisites.
First, let's build a Java application using the mvn command.
$ cd aws-serverless-java-container/samples/springboot2/pet-store/
$ mvn package
If the mvn command is successful, you should have serverless-spring-boot-example-1.0-SNAPSHOT.jar
in the target
directory.
Next, use AWS SAM to create a package to deploy to Lambda. For <YOUR S3 BUCKET NAME>
, specify an appropriate S3 bucket name.
$ aws cloudformation package --template-file sam.yaml --output-template-file output-sam.yaml --s3-bucket <YOUR S3 BUCKET NAME>
Once you have a package for deployment, it's time to deploy to Lambda. This deployment deploys AWS Lambda, which handles the logic, and API Gateway, which receives HTTPS requests and calls Lambda.
$ aws cloudformation deploy --template-file output-sam.yaml --stack-name ServerlessSpringBootSample --capabilities CAPABILITY_IAM
If the deployment is successful, you can see the API endpoint actually created with the following command.
$ aws cloudformation describe-stacks --stack-name ServerlessSpringBootSample
{
"Stacks": [
{
"StackId": "arn:aws:cloudformation:us-west-2:xxxxxxxx:stack/JerseySample/xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx",
"Description": "Example Pet Store API written with spark with the aws-serverless-java-container library",
"Tags": [],
"Outputs": [
{
"Description": "URL for application",
"OutputKey": "SpringBootPetStoreApi",
"OutputValue": "https://xxxxxxx.execute-api.us-west-2.amazonaws.com/Prod/pets"
}
],
"CreationTime": "2016-12-13T22:59:31.552Z",
"Capabilities": [
"CAPABILITY_IAM"
],
"StackName": "JerseySample",
"NotificationARNs": [],
"StackStatus": "UPDATE_COMPLETE"
}
]
}
The API endpoint specified in ʻOutputValue` will be the URI of the API deployed and launched this time.
Let's check the operation.
$ curl https://xxxxxxx.execute-api.us-west-2.amazonaws.com/Prod/pets
If you get the resulting JSON, you're successful!
How was that? It was so easy to install the sample app.
Spring Boot 2 is a great framework that can be easily run on Lambda by following the steps above, but if you want to introduce a smaller framework with a smaller turn, you can also use Spark
or Micronaut
. Please try it.
In addition, for workloads that are concerned about latency when Lambda is started for the first time, Native execution using GraalVM provided by Oracle is also possible.
Click here for a sample of Native conversion using Micronaut and GraalVM Micronaut X GraalVM Sample Pet Shop
Please refer to the presentation materials for JJUG CCC 2019 Fall as related information in this article.
[](https://www.slideshare.net / AmazonWebServicesJapan / serverlessjava-199195000) About Java in the serverless era
Recommended Posts