Using Spring Boot on AWS Lambda
AWS Lambda
AWS Lambda allow us to create small functions such as file transfer data validation most people using JavaScript to handle those things. In this article we will talk about Spring Boot. Is it a good idea to deploy Spring Boot on AWS Lambda?
Let’s Get Started
1. Setup Spring Boot
In order to create Spring Boot project, you can use InteliJ or Spring Initializr
1.1. Project Structure and Dependencies
In this project we going to use Spring Boot version 2.7.6, Spring Cloud version2021.0.5 and Java 11. We need a couple of dependencies for development.
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-function-adapter-aws</artifactId>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-events</artifactId>
<version>3.11.0</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.2.2</version>
</dependency>We need couple more of spring cloud dependencies management
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>Final step we need to build our application shade since some dependencies are not require for run on AWS Lambda.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<excludes>
<exclude>org.apache.tomcat.embed:*</exclude>
</excludes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>Our project configuration ready to serve request, next we need to create a Java class for handle request. In my case, I require lambda response back as json so, handle request method need to be APIGatewayV2HTTPResponse
public class RequestServiceHandler implements RequestHandler<Map<String, Object>, APIGatewayV2HTTPResponse> {
@Override
public APIGatewayV2HTTPResponse handleRequest(Map<String, Object> stringObjectMap, Context context) {
System.out.println("Lambda function invoke by Java");
return APIGatewayV2HTTPResponse.builder()
.withStatusCode(200)
.withHeaders(Map.of("Content-Type", "application/json"))
.withBody("{\"message\": \"success\"}")
.build();
}
}Our application is ready to build and deploy into Lambda AWS. Build application by using mvn clean package
1.2. Working with DynamoDB
In development environment we need to add DynamoDB SDK into our project.
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-sns</artifactId>
<version>1.12.367</version>
</dependency>Inside handler class we need to initial AmazonDynamoDBClientBuilder
AmazonDynamoDB amazonDynamoDB = AmazonDynamoDBClientBuilder.standard().withRegion(Regions.US_EAST_1).build();To save data into DynamoDB we need to construct a Map with a key as String and value as AttributeValue
Map<String, AttributeValue> map= new HashMap<>();Get Kuylim Tith’s stories in your inbox
Join Medium for free to get updates from this writer.
map.put("id", new AttributeValue(UUID.randomUUID().toString()));
amazonDynamoDB.putItem("your_table_name", map);
1.3. Working with SNS
Similarly, to DynamoDB we also need an SNS SDK
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-sns</artifactId>
<version>1.12.367</version>
</dependency>Back to handler class we need to initial AmazonSNS
AmazonSNS amazonSNS = AmazonSNSClientBuilder.standard().build();Publish your message SNS Topic by using Topic ARN
amazonSNS.publish("your_topic_arn", "Hello from Lambda!");2. Deploy Spring Boot to AWS Lambda
Deploy your application to AWS Lambda we need to create an S3 Bucket to store our jar file
Deploy application from S3 Bucket
Enter bucket endpoint
Config your handler class in Runtime Settings
Conclusion
From my own view, by using Java we can achieve the same thing as JavaScript can but, there are some disadvantages:
- Take longer cold start time, in my case an application performs save a couple fields to
DynamoDBand publish a message toSNStake around7s-10s - Development way harder than
JavaScriptlike2xbecause we need a lot of configurations and code editor inLambdadoes not support. - Deployment required to store build file in
S3becauseLambdasupport build file under10MB, if we upload file larger than10MBtoLambdabrowser will crash. - Required config handler class on
Runtime Settings. - Required minimum memory
512MB










