简体   繁体   中英

How to fetch Database url, credentials from AWS parameter store, into my spring-boot service, to test database connectivity?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM