DEV Community

Long Ngo
Long Ngo

Posted on • Updated on

Integrate Spring boot Version 3 with Parameter Store

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 and follow these steps to install AWS Toolkit for Eclipse IDE.

AWS Toolkit for Eclipse

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

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 to create a Spring boot project with Spring boot version 3
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>
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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=
Enter fullscreen mode Exit fullscreen mode

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

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

Parameter name

Select the type, recommend for SecureString.

Parameter type

And finally, input the value for this parameter.

Parameter value

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);


    }

}
Enter fullscreen mode Exit fullscreen mode

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

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

Happy Coding :)

Top comments (0)