DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • A Practical Guide to Creating a Spring Modulith Project
  • Integrate AWS Secrets Manager in Spring Boot Application
  • Keep Your Application Secrets Secret
  • Distributed Tracing System (Spring Cloud Sleuth + OpenZipkin)

Trending

  • Introducing senro: A Pipeline Engine You Program in Go
  • A Walk-Through of the DZone Article Editor
  • Going All In On the JVM 25 Virtual Threads
  • The Context Window Trap: Why More Context Doesn’t Mean Better AI

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Data Engineering
  3. Databases
  4. Spring Boot: Handle AWS RDS Password Change or Rotation Without Restarting

Spring Boot: Handle AWS RDS Password Change or Rotation Without Restarting

This article is about how you can handle AWS RDS secrets rotation without restarting your Spring Boot application.

By 
Amrut Prabhu
·
Updated Apr. 26, 21 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
23.8K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

This article is about how you can handle AWS RDS secrets rotation without restarting your Spring Boot application.

I had this problem wherein I had to update my database connection whenever the database password was updated for my AWS RDS instance. This can be because of a monthly password rotation policy or maybe the database credentials got compromised and you want all your running applications to keep running even when the database password is changed.

To solve this kind of problem, AWS provides a library that will handle this updating of the database connection without even restarting your Spring Boot application.

AWS has an open-source library called AWS Secrets Manager JDBC, that handles database connections while your application is running and talking to the RDS instance.

Let’s see how this works.

Firstly, add the following dependency in the build file. Considering Maven, it would look as follows:

XML
 




xxxxxxxxxx
1


 
1
<dependency>
2
    <groupId>com.amazonaws.secretsmanager</groupId>
3
    <artifactId>aws-secretsmanager-jdbc</artifactId>
4
    <version>1.0.5</version>
5
</dependency>



Next, specify the JDBC Datasource URL with the scheme jdbc-secretsmanager instead of jdbc

Java
 




xxxxxxxxxx
1


 
1
spring:
2
  datasource:
3
    url: jdbc-secretsmanager:mysql://database-host:3306/rotate_db



Next, you need to specify the driver's class name. For this article, we will stick to a MySQL RDS instance. So it’s going to be com.amazonaws.secretsmanager.sql.AWSSecretsManagerMySQLDrive.

This library also requires a database-specific connection library. So you will need to add the MySQL connector library, which is commonly the artifact mysql-connector-java. This will be used to make the actual connection with the database.

In case you are dealing with other databases, you can find the corresponding drivers from the source code here.

Next, create an AWS secret for the RDS instance using the database credentials section in the AWS Secrets Manager.

   






Next, in the properties file application.yaml, specify the secret name you just created as the username and you don’t have to specify any password as it’s now stored in the secrets manager.

Your property file should look something like this:

Java
 




x


 
1
spring:
2
  datasource:
3
    url: jdbc-secretsmanager:mysql://database-host:3306/rotate_db
4
    username: secret/rotation
5
    driver-class-name: com.amazonaws.secretsmanager.sql.AWSSecretsManagerMySQLDriver



Now, for the application to communicate with AWS and fetch the secret value, you would have to have AWS CLI set up and configured. Here is the link to it.

Once you have this in place, your application can connect to AWS by exporting the environment variable AWS_PROFILE with the profile you set up while configuring the AWS configuration.

With this, you are done with the changes.

Now start the application and it should be able to communicate with AWS Secrets Manager to fetch the credentials and start communicating with the AWS RDS instance.

You can test this by clicking on the rotate secret option in the secret which will generate a new password for the database and check the communication with the database.

Here is a GitHub link to my implementation.

Bonus: 

This also works if you have a Liquibase integration in place. You just have to specify the same URL in the Liquibase configuration and the database secret as the username and the Liquibase setup will work for you.

Java
 




xxxxxxxxxx
1


 
1
spring:
2
  datasource:
3
    url: jdbc-secretsmanager:mysql://database-host:3306/rotate_db
4
    username: secret/rotation
5
    driver-class-name: com.amazonaws.secretsmanager.sql.AWSSecretsManagerMySQLDriver
6

          
7
  liquibase:
8
    url: jdbc-secretsmanager:mysql://database-host:3306/rotate_db
9
    user: secret/rotation



Enjoy and have fun!

AWS Spring Framework Spring Boot Database connection application

Opinions expressed by DZone contributors are their own.

Related

  • A Practical Guide to Creating a Spring Modulith Project
  • Integrate AWS Secrets Manager in Spring Boot Application
  • Keep Your Application Secrets Secret
  • Distributed Tracing System (Spring Cloud Sleuth + OpenZipkin)

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook