In this guide, we will walk through the steps to load configuration values from AWS Parameter Store into your Spring Boot application’s application.yml
file. This is particularly useful for managing sensitive configuration data in a centralized and secure manner.
Step 1: Add Necessary Dependencies
First, you need to add the Spring Cloud AWS dependencies to your pom.xml
(for Maven) or build.gradle
(for Gradle) file.
Maven
<dependency>
<groupId>io.awspring.cloud</groupId>
<artifactId>spring-cloud-starter-aws-parameter-store-config</artifactId>
<version>2.3.1</version>
</dependency>
Gradle
dependencies {
implementation 'io.awspring.cloud:spring-cloud-starter-aws-parameter-store-config:2.3.1'
}
Step 2: Configure AWS Credentials
Ensure that your AWS credentials are configured correctly. You can set up your credentials in several ways, including using a ~/.aws/credentials
file, environment variables, or configuring IAM roles if you are running your application on AWS services.
Step 3: Configure bootstrap.yml
Create a bootstrap.yml
file in the src/main/resources
directory and add the configuration for AWS Parameter Store.
spring:
application:
name: my-app
cloud:
aws:
region:
static: your-aws-region # e.g., us-west-2
stack:
auto: false
config:
import: aws-parameterstore:
Step 4: Use Parameters in application.yml
In your application.yml
file, reference the properties as usual.
mongodb:
database: ${database.name}
uri: ${spring.data.mongodb.uri}
Step 5: Store Parameters in AWS Parameter Store
Ensure that the parameters are stored in AWS Parameter Store. For example:
/config/my-app/database.name
with the value of your database name./config/my-app/spring.data.mongodb.uri
with your MongoDB URI.
Step 6: Load Parameters at Startup
With the above configurations, Spring Cloud AWS Parameter Store will automatically load the parameters from AWS Parameter Store when your application starts.
Full Example
bootstrap.yml
spring:
application:
name: my-app
cloud:
aws:
region:
static: your-aws-region # e.g., us-west-2
stack:
auto: false
config:
import: aws-parameterstore:
application.yml
mongodb:
database: ${database.name}
uri: ${spring.data.mongodb.uri}
Important Notes
- AWS IAM Permissions: Ensure that the EC2 instance or application accessing Parameter Store has the necessary IAM permissions to read the parameters. You can attach an IAM policy similar to this:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ssm:GetParameter",
"ssm:GetParameters",
"ssm:GetParametersByPath"
],
"Resource": "arn:aws:ssm:your-region:your-account-id:parameter/config/my-app/*"
}
]
}
- Region Configuration: Ensure that you specify the correct AWS region in
bootstrap.yml
.
By following these steps, your Spring Boot application will be able to load configuration values from AWS Parameter Store seamlessly.
This guide should help you integrate AWS Parameter Store with your Spring Boot application effectively.