Creating a Spring Boot Java 21 application with a Public MySQL RDS

Collin Smith
7 min readMar 7, 2024

--

Introduction

In this article we will show how to create a Spring Boot Java 21 application with a Public MySQL RDS. This will involve the deployment of Spring Boot Java 21 application with AWS Lambda across 3 Availability zones in a public subnets of a custom VPC. They will interact with an RDS MySQL Instance that you will have created based in the previous article Creating a Public MySQL RDS instance with Secrets Manager and CDK.

We will be using AWS CDK in Java to create this project but similar steps can be done in the language of your choice. The CDK code can be examined and show how to do this in any of the supported CDK languages.

Business Case

The leprechauns love gold and to hide their treasure(sometimes at the end of rainbows). The want to build a treasure database to record their treasures and locations. This way they will not lose their treasure and can find it when they need it!

Architecture Diagram

Java 21 Spring Boot application in Lambda with MySQL RDS

It is worth noting that when you have Lambdas in a custom VPC you will need to deploy a VPC Endpoint to interact with AWS Secrets Manager. The MySQL RDS instance is configured to have its credentials managed by AWS Secrets Manager.

If you deploy your Lambdas in the default VPC, you do not need to have a VPC Endpoint for AWS Secrets Manager.

Prerequisite

Please ensure that you have the backend set up correctly by ensuring that the MySQL RDS instance within a VPC with Secrets Manager and a VPC endpoint are set up successfully. See Creating a Public MySQL RDS instance with Secrets Manager and CDK .

Building the SAM Project

  1. Validate the Prerequisites (See the Appendix)
  2. Creating the Spring Boot 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.23(or latest), Java: 21

Artifact: spring_public_mysql

Add Dependencies: Spring Web

Your screen should look something like:

Press Generate to download your Spring project file.

Unzip the project file(spring-public-mysql.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 spring-public-mysql 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.

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</version>
</dependency>

3. Creating a LambdaHandler class

Our LambdaHandler class will provide a point of entry into our Spring application.

If you want to go over creating a Java 21 Spring Boot Application in AWS Lambda, I suggest you have a look at https://medium.com/@collin-smith/creating-a-java-21-spring-boot-3-application-on-aws-lambda-48be652fc93f

But in this case, I would just suggest that you ensure you have the following classes/files set up as in the repo provided at https://github.com/collin-smith/spring-public-mysql

Key files include the following:

You will need to configure your application.properties file to your own settings based on how your setup is.

Configured application.properties

You will also need to create/configure the template.yaml file

Configuring the template.yaml file that will be based on the security groups and subnet ids in your VPC. If you followed the Creating a Public RDS MySQL Blog that precedes this blog, you should be able to retrieve this information from the CDK output.

A configured template.yaml

Some key template.yaml configuration information includes the Lambda Security Group, the public subnet ids where you want your Lambdas deployed to. The ARN of the RDS Secret. Additionally, the SnapStart configuration is there as well.

Make sure you have your environment setup properly to deploy to the correct account, region etc.

cd c:\projects\spring-public-mysql
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.

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 Region and yes to “HelloWorldFunction has no authentication.” prompt which you should enter “y”.

You should then be presented a url to test out as follows:

Microservices

There are a number of microservices that are presented for you to test out:

Note that you can review the microservices in the SpringController class with those methods that have the @RequestMapping tag

/test — simply return of some text

test microservice

/getsecretname — get the rds secret name from application.properties

getsecretname microservice

/getsecret — Retrieve the Secret information

getsecret microservice

/getdataandinsert — Query and insert data into the TREASURE table

getdataandinsert microservice

You can also query the data that was inserted in the MySQL Workbench client tool:

Tidying up

You should be able to clean up your SAM deployment with the following command when you are finished

sam delete

You can also clean up the VPC resources including the MySQL database as well.

Conclusion

In this article, we presented how to create a Spring Boot application in Java with Lambda that can successfully query and write data to a MySQL RDS instance.

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:

Appendix

Prerequisites

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

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.

MySQL WorkBench to download the MySQL WorkBench client to connect and query the MySQL database

--

--

Collin Smith

AWS Ambassador/Solutions Architect/Ex-French Foreign Legion