Reputation: 19
Database URL and database password are stored in the AWS parameter store. I have a service in spring-boot, and I need to write a function to test database connectivity.
For this function, I need to fetch the database URL, and credentials from the AWS parameter store. How do I fetch these parameters from the AWS parameter store and pass them as arguments to my function?
I've written my function which will test the database connectivity. Here, the URL, Username, and password for the database I need to test connectivity, are present in the AWS parameter store. I'm unable to fetch those and pass them to my function.
HikariConfig config = new HikariConfig();
config.setJdbcUrl(url);
config.setUsername(username);
config.setPassword(password);
HikariDataSource ds = new HikariDataSource(config);
try (Connection conn = ds.getConnection()) {
// Execute a simple SQL query to test the connection
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM users");
if (rs.next()) {
int count = rs.getInt(1);
System.out.println("Number of users: " + count);
}
}
Upvotes: 0
Views: 655
Reputation: 135
You can use AWS Spring Cloud to get values from SSM params. For that, use Spring Cloud framework-based configuration management. bootstrap.properties file has the default configuration required for Spring Cloud.
cloud.aws.credentials.instanceProfile=false
cloud.aws.credentials.useDefaultAwsCredentialsChain=true
cloud.aws.stack.auto=false
cloud.aws.region.auto=false
cloud.aws.region.static=us-east-1
aws.paramstore.prefix=?
aws.paramstore.defaultContext=application
aws.paramstore.profileSeparator=?
aws.paramstore.failFast=true
aws.paramstore.name=?
aws.paramstore.enabled=true
Upvotes: 1