Creating a Java 21 Spring Boot 3 application on AWS Lambda
You can set up the most up to date microservices with the latest versions of Java, SpringBoot framework on AWS! This article will show you how to do this from nothing with Java 21 and SpringBoot 3.2.
- Validating the Prerequisites
Please skip to “Creating the SpringBoot 3 Project” if your environment is already setup
Access to an AWS Account
Java 21 — The latest Java version to date can be downloaded from here
Confirm that you have the right version in your command line
Eclipse IDE (or your other favorite Java IDE)
During installation from here, select “Eclipse IDE for Enterprise Java and Web Developers”
Note: If you have any issues with Eclipse supporting Java 21 you might need to install the following Marketplace Solution
AWS CLI(AWS Command Line Interface)
Install AWS CLI(AWS Command Line Interface) so that you can manage your AWS Service from your console.
https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html
SAM CLI (Serverless Application Model CLI)
The AWS Serverless Application Model Command Line Interface will provide a toolkit to help build and run serverless applications on AWS
Install from here.
Maven
A tool to help simplify the build processes in the Jakarta Turbine project. Install from here.
GIT
Git will allow you to do source control management. Install from here.
POSTMAN
Download Postman from here. This will allow you to test out your new APIs
2. Creating the SpringBoot 3 Project
Spring Boot makes it easy to create stand-alone, production grade Spring based Applications that you can “just run”.
To create your Spring Boot 3 project, go to https://start.spring.io/
Make the following selections:
Project: Maven, Language: Java, Spring Boot: 3.21(or latest), Java: 21
Artifact: petshop
Add Dependencies: Spring Web
Your screen should look something like:
Press Generate to download your Spring project file.
Unzip the project file(petshop.zip) into your projects folder
Open this project in Eclipse. (File/Open Projects from File System/Select the right folder)
Let’s compile this build this project to see if it compiles:
Right-click the petshop project in the Project Explorer, Run As.., Maven Build…
Type “clean install” in the Goals field and press “Run”
If it compiles correctly, you should see the following “Build Success” message:
Now that we see it is compiling as a good starting point, we can now start making changes to run this to create some AWS Java Lambdas.
- Install the Java Serverless Container
This allows you to run Java Spring Boot 3 applications to run on AWS Lambda
a) Add the required dependencies to run this project in AWS
Add the aws serverless Java Container Support SpringBoot 3 implementation to your pom.xml file in your project.
You can retrieve this from this MVN Repo and add within the <dependencies> tags of the pom.xml. Your pom.xml file should look like this file.
<!-- https://mvnrepository.com/artifact/com.amazonaws.serverless/aws-serverless-java-container-springboot3 -->
<dependency>
<groupId>com.amazonaws.serverless</groupId>
<artifactId>aws-serverless-java-container-springboot3</artifactId>
<version>2.0.0-M2</version>
</dependency>
3. Creating a LambdaHandler class
Our LambdaHandler class will provide a point of entry into our Spring application.
Right click the com.example.petshop package in the Project Exporer and New/Class then type LambdaHandler into the Name field to create the class.
Basically, we are going to implement the same code from the StreamLambdaHandler.java class within our LambdaHandler.
We will also:
- update Application.class to be PetshopApplication.class
- update our LambdaHandler to implement RequestStreamHandler
So our LambdaHandler.java class should look like this file.
4. Creating a Controller
We now should create a controller class to define the interface (or microservices) to be implemented in the spring application. We can create multiple request mappings which are endpoints that can be called externally.
The following tags are included in the Controller class
@RestController — In Spring’s approach to building RESTful web services, HTTP requests are handled by a controller. These components are identified by this annotation
@EnableWebMvc — sets up the basic support for a Spring Mvc project
Let us create a controller class that defines a few endpoints that we will create. We will create a class, similar to how we created the LambdaHandler.java above, com.example.petshop.PetshopController that will define 2 get methods and a post method to prove out how some different microservices can be implemented.
Please verify that your code looks like this file.
5. Create the template.yaml file
Now that we have completed the code to run, we will need to ensure we can build and deploy the lambda. This will involve the use of SAM and will require the creation of a template file(template.yaml) at the base of your project folder.
To create a template.yaml from scratch you can go to your project folder with the command “sam init”
You can then select to create a Java 21 template.yaml by using the following entries when prompted (1, 1, N, 8, 2, 2,N,N,N, Project name: petshop, What package type would you like to use?: 1-ZIP)
We should also include Snapstart to help reduce the start up times of our Lambdas.
SnapStart:
ApplyOn: "PublishedVersions"
Please verify that your template.yaml file looks like this file.
Note: you can customize or clean up the template.yaml more if you wanted going forwards but this will still allow you to deploy some endpoints to test
6. Build the application
sam build
or the following if you have Docker Desktop installed and want it to be containerized
sam build --use-container
The containerized version can take a bit longer on the first try but just wait it out.
This should show that you have successfully built your project
7. Deploy the application
Now you can deploy your application.
You should ensure that you have configured your aws cli with the proper credentials. (Either through using the aws configure command or your aws short term credentials )
sam deploy --guided
You can go with all the defaults except the “HelloWorldFunction has no authentication.” prompt which you should enter “y”.
You should then be presented a url to test out as follows:
If it does not work or there are any issues you can verify by looking at the CloudWatch.
Looking at some of the simple output
/Prod/getnumberofdogs:
/Prod/getnumberofcats
/Prod/submitrequest
This last endpoint is a POST endpoint and you can test it out with Postman by submitting a post request to the same url but ending with submitrequest
8. Cleanup the application
You can remove the application with the following command
sam delete
9. Conclusion
We illustrated how to create Spring Boot 3 application endpoints deployed on AWS Lambda in the latest version of Java 21. This illustrates how to do this from scratch. This is a starting point to more complicated scenarios with other AWS Services.
Now if you want to read on to other related to the concept of Java 21 Spring Boot with database implementations, please consider the following articles as well:
- Creating a Java 21 Spring Boot 3 application on AWS Lambda
- Creating a Custom VPC with Multi-AZ Subnets with AWS CDK
- Creating a Public Postgres RDS Instance with Secrets Manager in CDK
- Creating a Spring Boot Java 21 application with a Public Postgres RDS
- Creating a Public MySQL RDS instance with Secrets Manager and CDK
- Creating a Spring Boot Java 21 application with a Public MySQL RDS
- Creating a Private Postgres RDS instance with a Bastion Host using CDK & SSM
- Creating a Spring Boot Java 21 application with a Private Postgres RDS
- Creating a Private MySQL instance with a Bastion Host using CDK & SSM
- Creating a Spring Boot Java 21 application with a Private MySQL RDS