AWS Secrets Manager, RDS and Spring Boot example using Docker


AWS Secrets Manager helps you protect secrets needed to access your applications, services, and IT resources.

You must not store sensitive data such as database credentials in your repository (Git). AWS secrete manager provides APIs to retrieve application secrets when deploying the applications. It also has built-in integration for Amazon RDS, Amazon Redshift, and Amazon DocumentDB.

First of all, we created a Database in AWS RDS and stored the database credentials in the AWS Secrets Manager.

AWS secret manager

We will access this secret while setting up the Spring data sources. Check the below code segment.

In the above code, we have retrieved the database secrets and configured HikarCP to create a data source.

According to Spring Cloud AWS documentation, to configure Parameter Store and Secrets Manager by overriding, we need to define our own Spring Cloud bootstrap configuration class with a bean of type AWSSimpleSystemsManagement. Instead of doing this, I created a Bean which connects only to AWS Secrets manager to retrieve the sensitive data.

In this way, I can create an AWSSecretsManager with the region.
There is another library called aws-secretsmanager-jdbc which supports AWS RDS integration, but I encountered several difficulties when working with it. I have explained this in a later part.

Setting up a tunnel to the AWS RDS for Local Development

In my project, I needed to connect to the secure development database in AWS RDS. This database is running in a private subnet in a VPC. It cannot be accessed directly from my local development environment.

To connect to the database, I used a mechanism called local port forwarding.
Read more:

The command I used is this :
ssh -i testApiEc2.pem -N -L

Running the project with Eclipse

When running the project with eclipse, you have to pass the database host with the VM arguments because the host is a configurable value. AWS region is in the code itself and Aws credentials details are retrieved from the .aws folder.

Running the project with Docker

When running the project with Docker, we need to pass the credentials with the database host.

Using AWS Secrets Manager JDBC Library (Another way to do this)
This is one of the approaches I used before. With this library, we only need to add the proper configuration to the "" file. Retrieving the secret and connecting to the AWS RDS will be handled by this library.

spring.datasource.validationQuery=SELECT 1

The issue with this approach was, when I tried to run this project with Docker, it was not able to detect the AWS region inside docker.
This library retrieves the AWS region and credential details from the .aws/config and .aws/credentials files on your computer and my docker container did not contain .aws/config and .aws/credentials files.

Adding the region to class did not configure the AWS region properly.

Check this thread:

Code for the project: