How to Load Configuration from AWS Parameter Store in a Spring Boot Application


Step 1: Add Necessary Dependencies

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

Step 3: Configure 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:

Step 4: Use Parameters in application.yml

mongodb:
  database: ${database.name}
  uri: ${spring.data.mongodb.uri}

Step 5: Store Parameters in AWS Parameter Store

  • /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

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

  1. 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/*"
    }
  ]
}
  1. Region Configuration: Ensure that you specify the correct AWS region in bootstrap.yml.

Leave a Reply

Your email address will not be published. Required fields are marked *