Managing secrets like database credentials, API keys, and passwords securely is a critical aspect of application development. Hardcoding secrets or storing them in plaintext configuration files is risky. Fortunately, with Spring Cloud AWS and AWS Secrets Manager, you can easily and securely load secrets into your Spring Boot application.
In this post, we’ll explore how to use the spring-cloud-starter-aws-secrets-manager-config dependency to integrate AWS Secrets Manager with Spring Boot. By the end, your application will be able to automatically load secrets at startup without writing extra boilerplate code.
AWS Secrets Manager is a fully managed service that helps you protect access to your applications and services by storing secrets centrally. It allows automatic rotation, access control via IAM, and audit logging with AWS CloudTrail.
This is a Spring Cloud starter that integrates AWS Secrets Manager with the Spring Cloud Config system. It enables you to externalize your configuration and load secrets directly into your environment using Spring’s property resolution mechanism (@Value, Environment, etc.).
Add the following Maven dependency to your Spring Boot project:
<dependency> <groupId>io.awspring.cloud</groupId> <artifactId>spring-cloud-starter-aws-secrets-manager-config</artifactId> <version>3.0.1</version> <!-- Use latest --> </dependency>
Note: Spring Cloud AWS v3+ is compatible with Spring Boot 3+ and uses
aws-java-sdk-v2
For Gradle:
implementation(“io.awspring.cloud:spring-cloud-starter-aws-secrets-manager-config:3.0.1”)
You can provide AWS credentials in any standard supported method:
spring:
config:
import: aws-secretsmanager:/ # important: tells Spring to import secrets
cloud:
aws:
region:
static: us-east-1
credentials:
profile-name: default
Note: The spring.config.import=aws-secretsmanager:/ line is critical — it tells Spring to import secrets from Secrets Manager as property sources.
Go to the AWS Console → Secrets Manager → Store a new secret.
{
"spring.datasource.url": "jdbc:mysql://db.example.com:3306/mydb",
"spring.datasource.username": "admin",
"spring.datasource.password": "S3cureP@ss!"
}
With your secret configured, Spring will automatically bind the properties.
You can now use them like any normal property:
Java
@Value("${spring.datasource.username}")
private String dbUser;
Or in application.yml:
datasource:
url: ${spring.datasource.url}
username: ${spring.datasource.username}
password: ${spring.datasource.password}
Work with our skilled Cloud developers to accelerate your project and boost its performance.
Hire Cloud Developers