Saturday 3 April 2021

Integrating AWS Systems Manager Parameter Store with a Spring Boot Application

AWS Systems Manager Parameter Store allows you to store data such as passwords, database strings, AMI IDs, etc as parameter values in plain text or encryped data format. It is similar to AWS Secrets Manager. However, if you need built-in password generator and automated secret rotation, then you should go for AWS Secrets Manager. Supposing you have a parameter name ``/config/demoapp/backend_dev/welcome.message`` and the value is ``TEST123``, you want to take this value from AWS Systems Manager Parameter Store instead of retrieving from ``application.properties``. To integrate it with a Spring Boot Application, you need to add the dependency in your ``pom.xml``. Make sure you are using Spring Cloud 2.x (Greenwich). ``` org.springframework.cloud spring-cloud-starter-aws-parameter-store-config 2.1.3.RELEASE ``` Then we need to modify ``bootstrap.properties`` to configure the bootstrap context. ``` aws.paramstore.prefix= aws.paramstore.name= aws.paramstore.enabled= aws.paramstore.profileSeparator=<[a-zA-Z0-9.\-_/]+> ``` For example, ``` aws.paramstore.prefix=/config/demoapp aws.paramstore.name=backend aws.paramstore.enabled=true aws.paramstore.profileSeparator=_ ``` In your application.properties ``` aws.region=ap-east-1 spring.application.name=backend spring.profiles.active=dev server.port=8080 ``` In your Controller, use ``@Value`` annotation to inject the value. ``` @Value("${welcome.message}") private String message; ``` Setup your AWS credential ``` [default] aws_access_key_id = your_access_key_id aws_secret_access_key = your_secret_access_key ``` Follow the below code and run the application to test it ``` package com.demo; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; @SpringBootApplication public class StartWebApplication { @Value("${welcome.message}") private String message; public static void main(String[] args) { SpringApplication.run(StartWebApplication.class, args); } @Bean public CommandLineRunner commandLineRunner(ApplicationContext ctx) { return args -> { System.out.println("Message from AWS Parameter Store: " + message); }; } } ``` ``` Message from AWS Parameter Store: TEST123 ```

No comments:

Post a Comment

A Fun Problem - Math

# Problem Statement JATC's math teacher always gives the class some interesting math problems so that they don't get bored. Today t...