RDS Database Authentication with Spring Boot: Part 2, IAM Authentication

public class IAMAuthDataSource
extends PGSimpleDataSource
private final static long serialVersionUID = 1L;

private Logger logger = LoggerFactory.getLogger(getClass());

public Connection getConnection(String user, String password)
throws SQLException
// I'd like to do this in constructor, but it can throw SQLException
setProperty("ssl", "true");
setProperty("sslmode", "require");

logger.debug("requesting IAM token for user {}", user);

// adapted from https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/UsingWithRDS.IAMDBAuth.Connecting.Java.html
RdsIamAuthTokenGenerator generator = RdsIamAuthTokenGenerator.builder()
.credentials(new DefaultAWSCredentialsProviderChain())
.region((new DefaultAwsRegionProviderChain()).getRegion())

GetIamAuthTokenRequest request = GetIamAuthTokenRequest.builder()

String authToken = generator.getAuthToken(request);

return super.getConnection(user, authToken);
public class Application
public static void main(String[] args)
SpringApplication.run(Application.class, args);

@ConfigurationProperties(prefix = "spring.datasource")
public HikariDataSource dataSource()
HikariDataSource ds = DataSourceBuilder.create()
return ds;
  1. Create a DataSource class based on a DBMS-specific implementation, and override the getConnection() method.
  2. Add a factory method to your Spring application that will explicitly create a connection pool bean named “spring.datasource”.
  3. Configure both connection pool and underlying datasource in application.properties.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Chariot Solutions

Chariot Solutions

Chariot Solutions is a top IT consulting firm specializing in software and mobile development, and development in the cloud. Visit us at chariotsolutions.com.