How to use Java framework with AWS Lambda! ??

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

background

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.

How it works

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

Sample app download

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.

Introducing Spring Boot 2 to Lambda

Spring Boot2 Pet Shop Samples

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.


How to take the next step?

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.

[About Java in the Serverless Era](https://www.slideshare.net / AmazonWebServicesJapan / serverlessjava-199195000) About Java in the serverless era

Recommended Posts

How to use Java framework with AWS Lambda! ??
How to use Java API with lambda expression
How to use Java lambda expressions
How to use BootStrap with Play Framework
Use Lambda Layers with Java
[Java] How to use Map
[Java] How to use Map
How to use java Optional
How to use java class
[Java] How to use Optional ②
[Java] How to use removeAll ()
[Java] How to use string.format
How to use Java Map
How to use Java variables
[Java] How to use Optional ①
How to use Java HttpClient (Get)
[Java] How to use join method
[Processing × Java] How to use variables
How to use mssql-tools with alpine
[Java] How to use LinkedHashMap class
[JavaFX] [Java8] How to use GridPane
How to use class methods [Java]
[Java] How to use List [ArrayList]
How to use classes in Java?
[Processing × Java] How to use arrays
[Java] How to use Math class
How to use Java enum type
How to use trained model of tensorflow2.0 with Kotlin / Java
Multilingual Locale in Java How to use Locale
[Java] How to use the File class
How to compile Java with VsCode & Ant
[Java] How to use the hasNext function
[Java] How to compare with equals method
[Rails] How to use rails console with docker
How to use submit method (Java Silver)
[Java] How to use the HashMap class
[Easy-to-understand explanation! ] How to use Java instance
[Java] How to use the toString () method
Studying how to use the constructor (java)
[Processing × Java] How to use the loop
AWS Lambda with Java starting now Part 1
How to use Java classes, definitions, import
[Easy-to-understand explanation! ] How to use Java polymorphism
[Java] [Maven3] Summary of how to use Maven3
[Processing × Java] How to use the class
How to use Java Scanner class (Note)
[Processing × Java] How to use the function
[Easy-to-understand explanation! ] How to use ArrayList [Java]
[Java] How to use the Calendar class
[Java] Learn how to use Optional correctly
[Easy-to-understand explanation! ] How to use Java overload
try-catch-finally exception handling How to use java
[Easy-to-understand explanation! ] How to use Java encapsulation
[Java] How to test for null with JUnit
[Java] How to use FileReader class and BufferedReader class
How to use Map
How to deploy a container on AWS Lambda
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use fields_for