We need to securely store and share sensitive credentials, passwords, and configuration information separately from our source code in Parameter Store in Secrets Manager and access these from Spring Boot. This article demonstrates how to integrate these AWS services into Spring Boot’s configuration ecosystem, extending Spring’s powerful capabilities into the cloud while maintaining the framework’s inherent simplicity.
To implement this integration, you’ll need two additional projects:
Spring Cloud
Spring Cloud AWS (formerly part of Spring Cloud but now maintained as a separate project)
Getting Started
For this tutorial, I’m using Gradle in the sample project, though Maven works equally well. The integration depends on your Spring Boot version – I’m using 3.4.3. Here are the relevant portions of the build.gradle file:
I am using 3.3.0 for Spring Cloud AWS, 2024.0.0 for Spring Cloud, and Spring Boot 3.4.3 for this article. This combination uses the 2.X version of the AWS SDK.
Edit your application.properties, application.yml or whatever spring confg file you are using. Here is the configuration I am using. You can adapt it suit your own needs:
You will place your secrets under the secretsmanager.basepath and the parameter store entries under the ssmparms.basepath. If you need multiple paths, you can add as many as you need in the spring.config.import entry. The scope of how Spring Boot manages the configuration is a vast topic. Please consult https://docs.spring.io/spring-boot/reference/features/external-config.html for reference.
Putting It Together
Once you have the configuration setup, the parameter store and / or secrets manager values set, and your security token, you can retrieve the values through environment.getProperty(“key”) or @Value to populate a property. Here is a very simple controller and service to retrieve and populate an endpoint with the values and keys:
Controller:
@RestController
@RequestMapping("/api/configs")
class ConfigController {
private final ConfigService configService;
public ConfigController(ConfigService configService) {
this.configService = configService;
}
@GetMapping
public Map<String, String> getConfig() {
return configService.getConfigs();
}
}
Service:
@Service
@RequiredArgsConstructor
public class ConfigService {
private final Environment environment;
public Map<String, String> getConfigs(){
Map<String, String> configs = new HashMap<>();
var key1 = "key1";
var key2 = "parameterstorekey";
var key3 = "application.property.value";
configs.put(key1, environment.getProperty(key1));
configs.put(key2, environment.getProperty(key2));
configs.put(key3, environment.getProperty(key3));
return configs;
}
}
Next Steps
Spring Cloud AWS provides many integrations into various AWS Services like S3, Dynamo, SQS, and SNS. You can build off this template to use any other services you need to extend your application’s capabilities.
(function(l) {
if (!l){window.lintrk = function(a,b){window.lintrk.q.push([a,b])};
window.lintrk.q=[]}
var s = document.getElementsByTagName("script")[0];
var b = document.createElement("script");
b.type = "text/javascript";b.async = true;
b.src = "https://snap.licdn.com/li.lms-analytics/insight.min.js";
s.parentNode.insertBefore(b, s);})(window.lintrk);
var astra = {"break_point":"768","isRtl":"","is_scroll_to_id":"","is_scroll_to_top":"1","is_header_footer_builder_active":"1","responsive_cart_click":"flyout","is_dark_palette":"","revealEffectEnable":"","edit_post_url":"https://tenmilesquare.com/wp-admin/post.php?post={{id}}&action=edit","ajax_url":"https://tenmilesquare.com/wp-admin/admin-ajax.php","infinite_count":"2","infinite_total":"0","pagination":"number","infinite_scroll_event":"scroll","no_more_post_message":"No more posts to show.","grid_layout":"1","site_url":"https://tenmilesquare.com","blogArchiveTitleLayout":"","blogArchiveTitleOn":"","show_comments":"Show Comments","masonryEnabled":"","blogMasonryBreakPoint":"0"};
//# sourceURL=astra-theme-js-js-extra