Integrate Spring boot Version 3 with Parameter Store
Published: 2023-03-04 13:02:16

## 1. Introduction

AWS Parameter Store is a service provided by Amazon Web Services (AWS) that help us store and manage parameters and secrets for our applications. It provides a secure, centralized location for storing and accessing sensitive data such as database credentials, API keys, and configuration data.
So we can use this service to manage multiple deployment property files for a Spring Boot Application.

## 2. Implementation
**2.1. Provide access to the development environment**
We need to provide access key and secret key for IDE (I will use Eclipse) to access the Parameter Store feature. You can refer this post [AWS Toolkit for Eclipse](https://aws.amazon.com/eclipse/?nc1=h_ls) and follow these steps to install AWS Toolkit for Eclipse IDE.

![AWS Toolkit for Eclipse](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1vcniuxylyvce2m6fuec.png)

After install successfully, we need to add key pair into AWS Toolkit. You can click the Icon of AWS Toolkit on the header and choose Preferences. If you are already had an access key on your computer, it will be display like this.

![AWS Credential](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m92rab12rtczjd7lry9k.png)

If you don't want to use this key, you can add manually another key by press the plus button next to the Global Configuration.

**2.2. Create a Spring boot project**
We can use Spring Starter Project from Spring Tool Suite (can find at Eclipse Marketplace) or [Spring Initializer](https://start.spring.io/) to create a Spring boot project with <u>Spring boot version 3</u>
Then, we should add some dependency that need to use to integrate with Parameter Store.

```
<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-bootstrap</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-aws-parameter-store-config</artifactId>
			<version>2.2.6.RELEASE</version>
		</dependency>
```
Carefully check you add these dependencies exactly. Because for a Spring Boot version 3, we should use **spring-cloud-starter-bootstrap** to work with Parameter Store. If you don't use it, the issues relative to mismatch version can occur. And finally, we need to use **spring-cloud-dependencies** because Parameter Store work on Spring Cloud.

```
<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>2022.0.1</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
```
**2.3. Read the parameter from Parameter Store**
We need to define application name in application.properties file. For example is `spring.application.name=my-app`. By default, Spring defined the syntax to work with parameter on Parameter Store with format:
`/config/<name-of-the-spring-application>_<profile>/<parameter-name>`
If the default parameter convention does not fit our needs. We can create a `bootstrap.properties` to override.

```
aws.paramstore.enabled=true
aws.paramstore.prefix=/demo
aws.paramstore.name=my-app
aws.paramstore.profileSeparator=
```
Now, we will create some parameter and add some code for testing.
In the AWS Console of AWS System Manager, you can choose Parameter and click on Create Parameter button.

![Parameter Store Console](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i6tbvrb9p44ipd5h7qai.png)

I already have some parameters in there, so I don't create anymore. You can create by input the name of parameter.

![Parameter name](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rv2bolvg85cfml813bzw.png)

Select the type, recommend for SecureString.

![Parameter type](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/twuuekexjwjvfivfgkdn.png)

And finally, input the value for this parameter.

![Parameter value](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/erxvgryibktexw6imrs0.png)

For testing purpose, the `@Value` annotation will be used to resolve the value of parameters. We expect the `port` variable will have value of `/demo/my-app/spring.port` and `message` variable corresponding for `/demo/my-app/spring.message`.

```
@SpringBootApplication
@Slf4j
public class DemoApplication implements CommandLineRunner {

	@Value("${spring.port}")
	private String port;
	
	@Value("${spring.message}")
	private String message;


	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);

	}

	@Override
	public void run(String... args) throws Exception {

		log.info("Resolved message port: {}", port);
		log.info("Resolved message parameter: {}", message);


	}

}
```
Starting the application and view on the console log, we can see the value that we created on AWS Parameter Store

![Log value of parameter](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3yzy4ocrecewlziretb3.png)

## 3. Summary
Using the Parameter Store with Spring Boot application, we can manage the value of properties easily. It can work similar to read values from `application.properties` file by `@Value` annotation. Also help to reduce the mismatch issues between development processing in local environment and deploy processing on AWS Cloud.

The implementation of all these examples can be found in my [GitHub](https://github.com/longngo0924/AWSLab/tree/master/Lab_Parameter_Store)

Happy Coding :)