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
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Enterprise AI in 2024: Share your insights into ChatGPT, generative AI, MLOps, and more (+enter the raffle!) for DZone's March Trend Report.

Low-Code Development: Learn the concepts of low code, features + use cases for professional devs, and the low-code implementation process.

E-Commerce Development Essentials: Considering starting or working on an e-commerce business? Learn how to create a back end that scales.

Related

  • How to Build a Chat App With Spring Boot and DynamoDB
  • Spring2quarkus — Spring Boot to Quarkus Migration
  • How To Verify Database Connection From a Spring Boot Application
  • Integrate AWS Secrets Manager in Spring Boot Application

Trending

  • Microservice Design Patterns for AI
  • Cloud Computing's Role in Transforming AML and KYC Operations
  • Guarding the Digital Fortress: A Comprehensive Guide to Intrusion Detection and Prevention Systems
  • Implementation of Data Quality Framework
  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.

Amrut Prabhu user avatar by
Amrut Prabhu
·
Updated Apr. 26, 21 · Tutorial
Like (5)
Save
Tweet
Share
20.4K Views

Join the DZone community and get the full member experience.

Join For Free

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

  • How to Build a Chat App With Spring Boot and DynamoDB
  • Spring2quarkus — Spring Boot to Quarkus Migration
  • How To Verify Database Connection From a Spring Boot Application
  • Integrate AWS Secrets Manager in Spring Boot Application

Comments

Partner Resources

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: