
Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:
Once the early-adopter seats are all used, the price will go up and stay at $33/year.
Last updated: March 17, 2024
In the previous article, we were focusing on EC2; now, let’s move on to the Relational Database Service.
Spring Cloud AWS can automatically create a DataSource just by specifying the RDS database identifier and the master password. The username, JDBC driver, and the complete URL are all resolved by Spring.
If an AWS account has an RDS instance with DB instance identifier as spring-cloud-test-db having master password se3retpass, then all that’s required to create a DataSource is the following line in application.properties:
cloud.aws.rds.spring-cloud-test-db.password=se3retpass
Three other properties can be added if you wish to use values other than the RDS default:
cloud.aws.rds.spring-cloud-test-db.username=testuser
cloud.aws.rds.spring-cloud-test-db.readReplicaSupport=true
cloud.aws.rds.spring-cloud-test-db.databaseName=test
In an application without Spring Boot or in cases where custom configurations are required, we can also create the DataSource using the Java-based configuration:
@Configuration
@EnableRdsInstance(
dbInstanceIdentifier = "spring-cloud-test-db",
password = "se3retpass")
public class SpringRDSSupport {
@Bean
public RdsInstanceConfigurer instanceConfigurer() {
return () -> {
TomcatJdbcDataSourceFactory dataSourceFactory
= new TomcatJdbcDataSourceFactory();
dataSourceFactory.setInitialSize(10);
dataSourceFactory.setValidationQuery("SELECT 1");
return dataSourceFactory;
};
}
}
Also, note that we need to add the correct JDBC driver dependency.
In this article, we had a look at various ways of accessing AWS RDS service; in the next and final article of the series, we’ll have a look at AWS Messaging support.