Use GenerateRDSAuthToken with an Amazon SDK or CLI - Amazon Relational Database Service
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Use GenerateRDSAuthToken with an Amazon SDK or CLI

The following code example shows how to use GenerateRDSAuthToken.

Java
SDK for Java 2.x
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

Use the RdsUtilities class to generate an authentication token.

public class GenerateRDSAuthToken { public static void main(String[] args) { final String usage = """ Usage: <dbInstanceIdentifier> <masterUsername> Where: dbInstanceIdentifier - The database instance identifier.\s masterUsername - The master user name.\s """; if (args.length != 2) { System.out.println(usage); System.exit(1); } String dbInstanceIdentifier = args[0]; String masterUsername = args[1]; Region region = Region.US_WEST_2; RdsClient rdsClient = RdsClient.builder() .region(region) .build(); String token = getAuthToken(rdsClient, dbInstanceIdentifier, masterUsername); System.out.println("The token response is " + token); } public static String getAuthToken(RdsClient rdsClient, String dbInstanceIdentifier, String masterUsername) { RdsUtilities utilities = rdsClient.utilities(); try { GenerateAuthenticationTokenRequest tokenRequest = GenerateAuthenticationTokenRequest.builder() .credentialsProvider(ProfileCredentialsProvider.create()) .username(masterUsername) .port(3306) .hostname(dbInstanceIdentifier) .build(); return utilities.generateAuthenticationToken(tokenRequest); } catch (RdsException e) { System.out.println(e.getLocalizedMessage()); System.exit(1); } return ""; } }

For a complete list of Amazon SDK developer guides and code examples, see Using this service with an Amazon SDK. This topic also includes information about getting started and details about previous SDK versions.