1. Introduction
Let's discuss the benefits of using AWS Lambda and Spring Boot 3 before we begin. You're probably accustomed to packaging your programmes as JAR files and distributing them to servers. You might even deploy the application to Kubernetes in a containerised form using Docker.
Scalability, security, and patching are all important considerations when managing your own server. You must take into account the volume of traffic your application will experience when you deploy it to a server. You might need to scale up to accommodate the traffic if your server is running at 100 percent of its capacity
If your server is only being used 10% utilization, you might consider scaling down to save money.
AWS Lambda is a serverless compute solution that allows you to run code without the need for server provisioning or management. You only pay for the compute time you use. This indicates that my own project, which will not receive a lot of traffic, is a great choice for AWS Lambda. This use case is ideal for applications with variable traffic or that can scale to zero.
Most people are familiar with this concept when it comes to deploying functions as a service (FaaS), however in this article we will deploy a whole Spring Boot REST API. According to the AWS Pricing Page, the free tier of AWS Lambda includes one million free requests per month and 400,000 GB-seconds of computing time each month. This means that for the majority of the side projects I'm working on, I can deploy them to AWS Lambda for free.
2. Prerequisites
Read the Quick Start here: https://github.com/awslabs/aws-serverless-java-container/wiki/Quick-start---Spring-Boot3
Thanks to Mark Sailes for all of his help with this tutoJava 17
Spring Boot 3.1
AWS CLI
AWS SAM CLI
GIT
You clone my repository because I have everything ready for you.
git clone https://github.com/Assassin010/SpringBoot-Serverless-Api-G.git
git clone https://Assassin10@bitbucket.org/assassin10/springboot-serverless-api-b.git
git clone https://gitlab.com/Assassin010/springboot-serverless-api-l.git
3. Agenda
In this article, you will build a new Spring Boot 3 application, as well as a CRUD REST API, which you will package and deploy to AWS Lambda.
start.spring.io
spring boot 3.1.0
maven / java
web, webfluxBuild CRUD REST API
post package
Post Record
PostNotFoundException
PostControllerBlog Posts Loader
JsonPlaceholderServiceTest locally using http client before packaging for production
4. Maven Model
The Maven Archetype is a project template that you can use to create new projects. The AWS Serverless Spring Boot 3 Archetype is a Maven project that you can use to create a new Spring Boot 3 project that includes an AWS Serverless container for Spring Boot 3. This will also create the exact Maven configuration needed. needed to create an AWS Lambda deployment package. If you are using IntelliJ IDEA, you can use the Maven Archetype to generate a new project but you can also use Spring Initializr. aws-serverless-springboot3-archetype
If Maven is already installed, you can use the following command to create a new project:
mvn archetype:generate -DgroupId=my.service -DartifactId=my-service -Dversion=1.0-SNAPSHOT \
-DarchetypeGroupId=com.amazonaws.serverless.archetypes \
-DarchetypeArtifactId=aws-serverless-springboot3-archetype \
-DarchetypeVersion=2.0.0-M1
5. AWS Resources
To deploy your application to AWS Lambda, you must first create the AWS resources listed below.
- AWS Lambda
- API Gateway
You can create the resources manually using the AWS Console, or you can use code. In this Article you will use the AWS Console, but I have included some resources if you want to learn more about SAM and CDK.
AWS SAM
SAM stands for Serverless Application Model. It's an open-source framework for creating serverless applications on AWS. It simplifies the definition of AWS resources (infrastructure as code) required to run your application. It also includes a command line interface (CLI) for packaging and deploying your application to AWS.
SAM defines your application using a template file (template.yaml
). The AWS resources required to run your application are defined in the template file. You should go over that template file and review it before deploying your application.
mvn clean package sam deploy --guided
https://aws.amazon.com/serverless/sam/
AWS CDK
CDK stands for Cloud Development Kit
AWS CDK is an open source software development framework that allows you to define the resources of your cloud application using familiar programming languages. Because the AWS CDK allows you to define your infrastructure in standard programming languages, you can write automated unit tests for your infrastructure code in the same way that you do for your application code.
Manual steps
run this command to generate zip file mvn clean package
If you want to use SAM CLI, I created for you a template called template.yaml.
What you require is to run the following commands:
mvn clean package
sam build
sam deploy --guided
5. References
https://aws.amazon.com/lambda/
https://aws.amazon.com/api-gateway/
https://aws.amazon.com/serverless/sam/
https://aws.amazon.com/cloudformation/
https://aws.amazon.com/s3/
https://spring.io/projects/spring-boot
https://start.spring.io
Happy coding!
END - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
If you like my work and want to support me…
The BEST way is following me on dev.to here
Feel free to give likes, or writing comments so I know how helpful this post was for you.
Gauthier Kwatatshey__
Connect with me on LinkdIn
Top comments (4)
How did you address the cold start time? My SOAP wrapper function written in spring boot took in excess of 25 sec from cold start and about 3-4 sec on warm start. Rewrote the whole thing in POJO to get times down to 5-6 sec in cold start, still too slow in comparison to other languages supported under AWS Lambda. Been looking at Dagger, hopefully it performs better. Also found a few hacks for preloading classes using capitalone.com/tech/cloud/aws-lamb.... But still no real solution to get sub 3 sec replies from cold start.
Look for SnapStart. It preload java apps, before cold starts:
Would love to try this using serverless framework once .. thanks
What’s the cold start time and what’s the image size?