Get started with Aurora DB clusters using an AWS SDK - Amazon Aurora

Get started with Aurora DB clusters using an AWS SDK

The following code examples show how to:

  • Create a custom Aurora DB cluster parameter group and set parameter values.

  • Create a DB cluster that uses the parameter group.

  • Create a DB instance that contains a database.

  • Take a snapshot of the DB cluster, then clean up resources.

.NET
AWS SDK for .NET
Note

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

Run an interactive scenario at a command prompt.

using Amazon.RDS.Model; using Amazon.RDS; using AuroraActions; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Console; using Microsoft.Extensions.Logging.Debug; namespace AuroraScenario; /// <summary> /// Scenario for Amazon Aurora examples. /// </summary> public class AuroraScenario { /* Before running this .NET code example, set up your development environment, including your credentials. This .NET example performs the following tasks: 1. Return a list of the available DB engine families for Aurora MySql using the DescribeDBEngineVersionsAsync method. 2. Select an engine family and create a custom DB cluster parameter group using the CreateDBClusterParameterGroupAsync method. 3. Get the parameter group using the DescribeDBClusterParameterGroupsAsync method. 4. Get some parameters in the group using the DescribeDBClusterParametersAsync method. 5. Parse and display some parameters in the group. 6. Modify the auto_increment_offset and auto_increment_increment parameters using the ModifyDBClusterParameterGroupAsync method. 7. Get and display the updated parameters using the DescribeDBClusterParametersAsync method with a source of "user". 8. Get a list of allowed engine versions using the DescribeDBEngineVersionsAsync method. 9. Create an Aurora DB cluster that contains a MySql database and uses the parameter group using the CreateDBClusterAsync method. 10. Wait for the DB cluster to be ready using the DescribeDBClustersAsync method. 11. Display and select from a list of instance classes available for the selected engine and version using the paginated DescribeOrderableDBInstanceOptions method. 12. Create a database instance in the cluster using the CreateDBInstanceAsync method. 13. Wait for the DB instance to be ready using the DescribeDBInstances method. 14. Display the connection endpoint string for the new DB cluster. 15. Create a snapshot of the DB cluster using the CreateDBClusterSnapshotAsync method. 16. Wait for DB snapshot to be ready using the DescribeDBClusterSnapshotsAsync method. 17. Delete the DB instance using the DeleteDBInstanceAsync method. 18. Delete the DB cluster using the DeleteDBClusterAsync method. 19. Wait for DB cluster to be deleted using the DescribeDBClustersAsync methods. 20. Delete the cluster parameter group using the DeleteDBClusterParameterGroupAsync. */ private static readonly string sepBar = new('-', 80); private static AuroraWrapper auroraWrapper = null!; private static ILogger logger = null!; private static readonly string engine = "aurora-mysql"; static async Task Main(string[] args) { // Set up dependency injection for the Amazon Relational Database Service (Amazon RDS). using var host = Host.CreateDefaultBuilder(args) .ConfigureLogging(logging => logging.AddFilter("System", LogLevel.Debug) .AddFilter<DebugLoggerProvider>("Microsoft", LogLevel.Information) .AddFilter<ConsoleLoggerProvider>("Microsoft", LogLevel.Trace)) .ConfigureServices((_, services) => services.AddAWSService<IAmazonRDS>() .AddTransient<AuroraWrapper>() ) .Build(); logger = LoggerFactory.Create(builder => { builder.AddConsole(); }).CreateLogger<AuroraScenario>(); auroraWrapper = host.Services.GetRequiredService<AuroraWrapper>(); Console.WriteLine(sepBar); Console.WriteLine( "Welcome to the Amazon Aurora: get started with DB clusters example."); Console.WriteLine(sepBar); DBClusterParameterGroup parameterGroup = null!; DBCluster? newCluster = null; DBInstance? newInstance = null; try { var parameterGroupFamily = await ChooseParameterGroupFamilyAsync(); parameterGroup = await CreateDBParameterGroupAsync(parameterGroupFamily); var parameters = await DescribeParametersInGroupAsync(parameterGroup.DBClusterParameterGroupName, new List<string> { "auto_increment_offset", "auto_increment_increment" }); await ModifyParametersAsync(parameterGroup.DBClusterParameterGroupName, parameters); await DescribeUserSourceParameters(parameterGroup.DBClusterParameterGroupName); var engineVersionChoice = await ChooseDBEngineVersionAsync(parameterGroupFamily); var newClusterIdentifier = "Example-Cluster-" + DateTime.Now.Ticks; newCluster = await CreateNewCluster ( parameterGroup, engine, engineVersionChoice.EngineVersion, newClusterIdentifier ); var instanceClassChoice = await ChooseDBInstanceClass(engine, engineVersionChoice.EngineVersion); var newInstanceIdentifier = "Example-Instance-" + DateTime.Now.Ticks; newInstance = await CreateNewInstance( newClusterIdentifier, engine, engineVersionChoice.EngineVersion, instanceClassChoice.DBInstanceClass, newInstanceIdentifier ); DisplayConnectionString(newCluster!); await CreateSnapshot(newCluster!); await CleanupResources(newInstance, newCluster, parameterGroup); Console.WriteLine("Scenario complete."); Console.WriteLine(sepBar); } catch (Exception ex) { await CleanupResources(newInstance, newCluster, parameterGroup); logger.LogError(ex, "There was a problem executing the scenario."); } } /// <summary> /// Choose the Aurora DB parameter group family from a list of available options. /// </summary> /// <returns>The selected parameter group family.</returns> public static async Task<string> ChooseParameterGroupFamilyAsync() { Console.WriteLine(sepBar); // 1. Get a list of available engines. var engines = await auroraWrapper.DescribeDBEngineVersionsForEngineAsync(engine); Console.WriteLine($"1. The following is a list of available DB parameter group families for engine {engine}:"); var parameterGroupFamilies = engines.GroupBy(e => e.DBParameterGroupFamily).ToList(); for (var i = 1; i <= parameterGroupFamilies.Count; i++) { var parameterGroupFamily = parameterGroupFamilies[i - 1]; // List the available parameter group families. Console.WriteLine( $"\t{i}. Family: {parameterGroupFamily.Key}"); } var choiceNumber = 0; while (choiceNumber < 1 || choiceNumber > parameterGroupFamilies.Count) { Console.WriteLine("2. Select an available DB parameter group family by entering a number from the preceding list:"); var choice = Console.ReadLine(); Int32.TryParse(choice, out choiceNumber); } var parameterGroupFamilyChoice = parameterGroupFamilies[choiceNumber - 1]; Console.WriteLine(sepBar); return parameterGroupFamilyChoice.Key; } /// <summary> /// Create and get information on a DB parameter group. /// </summary> /// <param name="dbParameterGroupFamily">The DBParameterGroupFamily for the new DB parameter group.</param> /// <returns>The new DBParameterGroup.</returns> public static async Task<DBClusterParameterGroup> CreateDBParameterGroupAsync(string dbParameterGroupFamily) { Console.WriteLine(sepBar); Console.WriteLine($"2. Create new DB parameter group with family {dbParameterGroupFamily}:"); var parameterGroup = await auroraWrapper.CreateCustomClusterParameterGroupAsync( dbParameterGroupFamily, "ExampleParameterGroup-" + DateTime.Now.Ticks, "New example parameter group"); var groupInfo = await auroraWrapper.DescribeCustomDBClusterParameterGroupAsync(parameterGroup.DBClusterParameterGroupName); Console.WriteLine( $"3. New DB parameter group created: \n\t{groupInfo?.Description}, \n\tARN {groupInfo?.DBClusterParameterGroupName}"); Console.WriteLine(sepBar); return parameterGroup; } /// <summary> /// Get and describe parameters from a DBParameterGroup. /// </summary> /// <param name="parameterGroupName">The name of the DBParameterGroup.</param> /// <param name="parameterNames">Optional specific names of parameters to describe.</param> /// <returns>The list of requested parameters.</returns> public static async Task<List<Parameter>> DescribeParametersInGroupAsync(string parameterGroupName, List<string>? parameterNames = null) { Console.WriteLine(sepBar); Console.WriteLine("4. Get some parameters from the group."); Console.WriteLine(sepBar); var parameters = await auroraWrapper.DescribeDBClusterParametersInGroupAsync(parameterGroupName); var matchingParameters = parameters.Where(p => parameterNames == null || parameterNames.Contains(p.ParameterName)).ToList(); Console.WriteLine("5. Parameter information:"); matchingParameters.ForEach(p => Console.WriteLine( $"\n\tParameter: {p.ParameterName}." + $"\n\tDescription: {p.Description}." + $"\n\tAllowed Values: {p.AllowedValues}." + $"\n\tValue: {p.ParameterValue}.")); Console.WriteLine(sepBar); return matchingParameters; } /// <summary> /// Modify a parameter from a DBParameterGroup. /// </summary> /// <param name="parameterGroupName">Name of the DBParameterGroup.</param> /// <param name="parameters">The parameters to modify.</param> /// <returns>Async task.</returns> public static async Task ModifyParametersAsync(string parameterGroupName, List<Parameter> parameters) { Console.WriteLine(sepBar); Console.WriteLine("6. Modify some parameters in the group."); await auroraWrapper.ModifyIntegerParametersInGroupAsync(parameterGroupName, parameters); Console.WriteLine(sepBar); } /// <summary> /// Describe the user source parameters in the group. /// </summary> /// <param name="parameterGroupName">The name of the DBParameterGroup.</param> /// <returns>Async task.</returns> public static async Task DescribeUserSourceParameters(string parameterGroupName) { Console.WriteLine(sepBar); Console.WriteLine("7. Describe updated user source parameters in the group."); var parameters = await auroraWrapper.DescribeDBClusterParametersInGroupAsync(parameterGroupName, "user"); parameters.ForEach(p => Console.WriteLine( $"\n\tParameter: {p.ParameterName}." + $"\n\tDescription: {p.Description}." + $"\n\tAllowed Values: {p.AllowedValues}." + $"\n\tValue: {p.ParameterValue}.")); Console.WriteLine(sepBar); } /// <summary> /// Choose a DB engine version. /// </summary> /// <param name="dbParameterGroupFamily">DB parameter group family for engine choice.</param> /// <returns>The selected engine version.</returns> public static async Task<DBEngineVersion> ChooseDBEngineVersionAsync(string dbParameterGroupFamily) { Console.WriteLine(sepBar); // Get a list of allowed engines. var allowedEngines = await auroraWrapper.DescribeDBEngineVersionsForEngineAsync(engine, dbParameterGroupFamily); Console.WriteLine($"Available DB engine versions for parameter group family {dbParameterGroupFamily}:"); int i = 1; foreach (var version in allowedEngines) { Console.WriteLine( $"\t{i}. Engine: {version.Engine} Version {version.EngineVersion}."); i++; } var choiceNumber = 0; while (choiceNumber < 1 || choiceNumber > allowedEngines.Count) { Console.WriteLine("8. Select an available DB engine version by entering a number from the list above:"); var choice = Console.ReadLine(); Int32.TryParse(choice, out choiceNumber); } var engineChoice = allowedEngines[choiceNumber - 1]; Console.WriteLine(sepBar); return engineChoice; } /// <summary> /// Create a new RDS DB cluster. /// </summary> /// <param name="parameterGroup">Parameter group to use for the DB cluster.</param> /// <param name="engineName">Engine to use for the DB cluster.</param> /// <param name="engineVersion">Engine version to use for the DB cluster.</param> /// <param name="clusterIdentifier">Cluster identifier to use for the DB cluster.</param> /// <returns>The new DB cluster.</returns> public static async Task<DBCluster?> CreateNewCluster(DBClusterParameterGroup parameterGroup, string engineName, string engineVersion, string clusterIdentifier) { Console.WriteLine(sepBar); Console.WriteLine($"9. Create a new DB cluster with identifier {clusterIdentifier}."); DBCluster newCluster; var clusters = await auroraWrapper.DescribeDBClustersPagedAsync(); var isClusterCreated = clusters.Any(i => i.DBClusterIdentifier == clusterIdentifier); if (isClusterCreated) { Console.WriteLine("Cluster already created."); newCluster = clusters.First(i => i.DBClusterIdentifier == clusterIdentifier); } else { Console.WriteLine("Enter an admin username:"); var username = Console.ReadLine(); Console.WriteLine("Enter an admin password:"); var password = Console.ReadLine(); newCluster = await auroraWrapper.CreateDBClusterWithAdminAsync( "ExampleDatabase", clusterIdentifier, parameterGroup.DBClusterParameterGroupName, engineName, engineVersion, username!, password! ); Console.WriteLine("10. Waiting for DB cluster to be ready..."); while (newCluster.Status != "available") { Console.Write("."); Thread.Sleep(5000); clusters = await auroraWrapper.DescribeDBClustersPagedAsync(clusterIdentifier); newCluster = clusters.First(); } } Console.WriteLine(sepBar); return newCluster; } /// <summary> /// Choose a DB instance class for a particular engine and engine version. /// </summary> /// <param name="engine">DB engine for DB instance choice.</param> /// <param name="engineVersion">DB engine version for DB instance choice.</param> /// <returns>The selected orderable DB instance option.</returns> public static async Task<OrderableDBInstanceOption> ChooseDBInstanceClass(string engine, string engineVersion) { Console.WriteLine(sepBar); // Get a list of allowed DB instance classes. var allowedInstances = await auroraWrapper.DescribeOrderableDBInstanceOptionsPagedAsync(engine, engineVersion); Console.WriteLine($"Available DB instance classes for engine {engine} and version {engineVersion}:"); int i = 1; foreach (var instance in allowedInstances) { Console.WriteLine( $"\t{i}. Instance class: {instance.DBInstanceClass} (storage type {instance.StorageType})"); i++; } var choiceNumber = 0; while (choiceNumber < 1 || choiceNumber > allowedInstances.Count) { Console.WriteLine("11. Select an available DB instance class by entering a number from the preceding list:"); var choice = Console.ReadLine(); Int32.TryParse(choice, out choiceNumber); } var instanceChoice = allowedInstances[choiceNumber - 1]; Console.WriteLine(sepBar); return instanceChoice; } /// <summary> /// Create a new DB instance. /// </summary> /// <param name="engineName">Engine to use for the DB instance.</param> /// <param name="engineVersion">Engine version to use for the DB instance.</param> /// <param name="instanceClass">Instance class to use for the DB instance.</param> /// <param name="instanceIdentifier">Instance identifier to use for the DB instance.</param> /// <returns>The new DB instance.</returns> public static async Task<DBInstance?> CreateNewInstance( string clusterIdentifier, string engineName, string engineVersion, string instanceClass, string instanceIdentifier) { Console.WriteLine(sepBar); Console.WriteLine($"12. Create a new DB instance with identifier {instanceIdentifier}."); bool isInstanceReady = false; DBInstance newInstance; var instances = await auroraWrapper.DescribeDBInstancesPagedAsync(); isInstanceReady = instances.FirstOrDefault(i => i.DBInstanceIdentifier == instanceIdentifier)?.DBInstanceStatus == "available"; if (isInstanceReady) { Console.WriteLine("Instance already created."); newInstance = instances.First(i => i.DBInstanceIdentifier == instanceIdentifier); } else { newInstance = await auroraWrapper.CreateDBInstanceInClusterAsync( clusterIdentifier, instanceIdentifier, engineName, engineVersion, instanceClass ); Console.WriteLine("13. Waiting for DB instance to be ready..."); while (!isInstanceReady) { Console.Write("."); Thread.Sleep(5000); instances = await auroraWrapper.DescribeDBInstancesPagedAsync(instanceIdentifier); isInstanceReady = instances.FirstOrDefault()?.DBInstanceStatus == "available"; newInstance = instances.First(); } } Console.WriteLine(sepBar); return newInstance; } /// <summary> /// Display a connection string for an Amazon RDS DB cluster. /// </summary> /// <param name="cluster">The DB cluster to use to get a connection string.</param> public static void DisplayConnectionString(DBCluster cluster) { Console.WriteLine(sepBar); // Display the connection string. Console.WriteLine("14. New DB cluster connection string: "); Console.WriteLine( $"\n{engine} -h {cluster.Endpoint} -P {cluster.Port} " + $"-u {cluster.MasterUsername} -p [YOUR PASSWORD]\n"); Console.WriteLine(sepBar); } /// <summary> /// Create a snapshot from an Amazon RDS DB cluster. /// </summary> /// <param name="cluster">DB cluster to use when creating a snapshot.</param> /// <returns>The snapshot object.</returns> public static async Task<DBClusterSnapshot> CreateSnapshot(DBCluster cluster) { Console.WriteLine(sepBar); // Create a snapshot. Console.WriteLine($"15. Creating snapshot from DB cluster {cluster.DBClusterIdentifier}."); var snapshot = await auroraWrapper.CreateClusterSnapshotByIdentifierAsync( cluster.DBClusterIdentifier, "ExampleSnapshot-" + DateTime.Now.Ticks); // Wait for the snapshot to be available. bool isSnapshotReady = false; Console.WriteLine($"16. Waiting for snapshot to be ready..."); while (!isSnapshotReady) { Console.Write("."); Thread.Sleep(5000); var snapshots = await auroraWrapper.DescribeDBClusterSnapshotsByIdentifierAsync(cluster.DBClusterIdentifier); isSnapshotReady = snapshots.FirstOrDefault()?.Status == "available"; snapshot = snapshots.First(); } Console.WriteLine( $"Snapshot {snapshot.DBClusterSnapshotIdentifier} status is {snapshot.Status}."); Console.WriteLine(sepBar); return snapshot; } /// <summary> /// Clean up resources from the scenario. /// </summary> /// <param name="newInstance">The instance to clean up.</param> /// <param name="newCluster">The cluster to clean up.</param> /// <param name="parameterGroup">The parameter group to clean up.</param> /// <returns>Async Task.</returns> private static async Task CleanupResources( DBInstance? newInstance, DBCluster? newCluster, DBClusterParameterGroup? parameterGroup) { Console.WriteLine(new string('-', 80)); Console.WriteLine($"Clean up resources."); if (newInstance is not null && GetYesNoResponse($"\tClean up instance {newInstance.DBInstanceIdentifier}? (y/n)")) { // Delete the DB instance. Console.WriteLine($"17. Deleting the DB instance {newInstance.DBInstanceIdentifier}."); await auroraWrapper.DeleteDBInstanceByIdentifierAsync(newInstance.DBInstanceIdentifier); } if (newCluster is not null && GetYesNoResponse($"\tClean up cluster {newCluster.DBClusterIdentifier}? (y/n)")) { // Delete the DB cluster. Console.WriteLine($"18. Deleting the DB cluster {newCluster.DBClusterIdentifier}."); await auroraWrapper.DeleteDBClusterByIdentifierAsync(newCluster.DBClusterIdentifier); // Wait for the DB cluster to delete. Console.WriteLine($"19. Waiting for the DB cluster to delete..."); bool isClusterDeleted = false; while (!isClusterDeleted) { Console.Write("."); Thread.Sleep(5000); var cluster = await auroraWrapper.DescribeDBClustersPagedAsync(); isClusterDeleted = cluster.All(i => i.DBClusterIdentifier != newCluster.DBClusterIdentifier); } Console.WriteLine("DB cluster deleted."); } if (parameterGroup is not null && GetYesNoResponse($"\tClean up parameter group? (y/n)")) { Console.WriteLine($"20. Deleting the DB parameter group {parameterGroup.DBClusterParameterGroupName}."); await auroraWrapper.DeleteClusterParameterGroupByNameAsync(parameterGroup.DBClusterParameterGroupName); Console.WriteLine("Parameter group deleted."); } Console.WriteLine(new string('-', 80)); } /// <summary> /// Get a yes or no response from the user. /// </summary> /// <param name="question">The question string to print on the console.</param> /// <returns>True if the user responds with a yes.</returns> private static bool GetYesNoResponse(string question) { Console.WriteLine(question); var ynResponse = Console.ReadLine(); var response = ynResponse != null && ynResponse.Equals("y", StringComparison.InvariantCultureIgnoreCase); return response; }

Wrapper methods that are called by the scenario to manage Aurora actions.

using Amazon.RDS; using Amazon.RDS.Model; namespace AuroraActions; /// <summary> /// Wrapper for the Amazon Aurora cluster client operations. /// </summary> public class AuroraWrapper { private readonly IAmazonRDS _amazonRDS; public AuroraWrapper(IAmazonRDS amazonRDS) { _amazonRDS = amazonRDS; } /// <summary> /// Get a list of DB engine versions for a particular DB engine. /// </summary> /// <param name="engine">The name of the engine.</param> /// <param name="parameterGroupFamily">Optional parameter group family name.</param> /// <returns>A list of DBEngineVersions.</returns> public async Task<List<DBEngineVersion>> DescribeDBEngineVersionsForEngineAsync(string engine, string? parameterGroupFamily = null) { var response = await _amazonRDS.DescribeDBEngineVersionsAsync( new DescribeDBEngineVersionsRequest() { Engine = engine, DBParameterGroupFamily = parameterGroupFamily }); return response.DBEngineVersions; } /// <summary> /// Create a custom cluster parameter group. /// </summary> /// <param name="parameterGroupFamily">The family of the parameter group.</param> /// <param name="groupName">The name for the new parameter group.</param> /// <param name="description">A description for the new parameter group.</param> /// <returns>The new parameter group object.</returns> public async Task<DBClusterParameterGroup> CreateCustomClusterParameterGroupAsync( string parameterGroupFamily, string groupName, string description) { var request = new CreateDBClusterParameterGroupRequest { DBParameterGroupFamily = parameterGroupFamily, DBClusterParameterGroupName = groupName, Description = description, }; var response = await _amazonRDS.CreateDBClusterParameterGroupAsync(request); return response.DBClusterParameterGroup; } /// <summary> /// Describe the cluster parameters in a parameter group. /// </summary> /// <param name="groupName">The name of the parameter group.</param> /// <param name="source">The optional name of the source filter.</param> /// <returns>The collection of parameters.</returns> public async Task<List<Parameter>> DescribeDBClusterParametersInGroupAsync(string groupName, string? source = null) { var paramList = new List<Parameter>(); DescribeDBClusterParametersResponse response; var request = new DescribeDBClusterParametersRequest { DBClusterParameterGroupName = groupName, Source = source, }; // Get the full list if there are multiple pages. do { response = await _amazonRDS.DescribeDBClusterParametersAsync(request); paramList.AddRange(response.Parameters); request.Marker = response.Marker; } while (response.Marker is not null); return paramList; } /// <summary> /// Get the description of a DB cluster parameter group by name. /// </summary> /// <param name="name">The name of the DB parameter group to describe.</param> /// <returns>The parameter group description.</returns> public async Task<DBClusterParameterGroup?> DescribeCustomDBClusterParameterGroupAsync(string name) { var response = await _amazonRDS.DescribeDBClusterParameterGroupsAsync( new DescribeDBClusterParameterGroupsRequest() { DBClusterParameterGroupName = name }); return response.DBClusterParameterGroups.FirstOrDefault(); } /// <summary> /// Modify the specified integer parameters with new values from user input. /// </summary> /// <param name="groupName">The group name for the parameters.</param> /// <param name="parameters">The list of integer parameters to modify.</param> /// <param name="newValue">Optional int value to set for parameters.</param> /// <returns>The name of the group that was modified.</returns> public async Task<string> ModifyIntegerParametersInGroupAsync(string groupName, List<Parameter> parameters, int newValue = 0) { foreach (var p in parameters) { if (p.IsModifiable && p.DataType == "integer") { while (newValue == 0) { Console.WriteLine( $"Enter a new value for {p.ParameterName} from the allowed values {p.AllowedValues} "); var choice = Console.ReadLine(); int.TryParse(choice, out newValue); } p.ParameterValue = newValue.ToString(); } } var request = new ModifyDBClusterParameterGroupRequest { Parameters = parameters, DBClusterParameterGroupName = groupName, }; var result = await _amazonRDS.ModifyDBClusterParameterGroupAsync(request); return result.DBClusterParameterGroupName; } /// <summary> /// Get a list of orderable DB instance options for a specific /// engine and engine version. /// </summary> /// <param name="engine">Name of the engine.</param> /// <param name="engineVersion">Version of the engine.</param> /// <returns>List of OrderableDBInstanceOptions.</returns> public async Task<List<OrderableDBInstanceOption>> DescribeOrderableDBInstanceOptionsPagedAsync(string engine, string engineVersion) { // Use a paginator to get a list of DB instance options. var results = new List<OrderableDBInstanceOption>(); var paginateInstanceOptions = _amazonRDS.Paginators.DescribeOrderableDBInstanceOptions( new DescribeOrderableDBInstanceOptionsRequest() { Engine = engine, EngineVersion = engineVersion, }); // Get the entire list using the paginator. await foreach (var instanceOptions in paginateInstanceOptions.OrderableDBInstanceOptions) { results.Add(instanceOptions); } return results; } /// <summary> /// Delete a particular parameter group by name. /// </summary> /// <param name="groupName">The name of the parameter group.</param> /// <returns>True if successful.</returns> public async Task<bool> DeleteClusterParameterGroupByNameAsync(string groupName) { var request = new DeleteDBClusterParameterGroupRequest { DBClusterParameterGroupName = groupName, }; var response = await _amazonRDS.DeleteDBClusterParameterGroupAsync(request); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; } /// <summary> /// Create a new cluster and database. /// </summary> /// <param name="dbName">The name of the new database.</param> /// <param name="clusterIdentifier">The identifier of the cluster.</param> /// <param name="parameterGroupName">The name of the parameter group.</param> /// <param name="dbEngine">The engine to use for the new cluster.</param> /// <param name="dbEngineVersion">The version of the engine to use.</param> /// <param name="adminName">The admin username.</param> /// <param name="adminPassword">The primary admin password.</param> /// <returns>The cluster object.</returns> public async Task<DBCluster> CreateDBClusterWithAdminAsync( string dbName, string clusterIdentifier, string parameterGroupName, string dbEngine, string dbEngineVersion, string adminName, string adminPassword) { var request = new CreateDBClusterRequest { DatabaseName = dbName, DBClusterIdentifier = clusterIdentifier, DBClusterParameterGroupName = parameterGroupName, Engine = dbEngine, EngineVersion = dbEngineVersion, MasterUsername = adminName, MasterUserPassword = adminPassword, }; var response = await _amazonRDS.CreateDBClusterAsync(request); return response.DBCluster; } /// <summary> /// Returns a list of DB instances. /// </summary> /// <param name="dbInstanceIdentifier">Optional name of a specific DB instance.</param> /// <returns>List of DB instances.</returns> public async Task<List<DBInstance>> DescribeDBInstancesPagedAsync(string? dbInstanceIdentifier = null) { var results = new List<DBInstance>(); var instancesPaginator = _amazonRDS.Paginators.DescribeDBInstances( new DescribeDBInstancesRequest { DBInstanceIdentifier = dbInstanceIdentifier }); // Get the entire list using the paginator. await foreach (var instances in instancesPaginator.DBInstances) { results.Add(instances); } return results; } /// <summary> /// Returns a list of DB clusters. /// </summary> /// <param name="dbInstanceIdentifier">Optional name of a specific DB cluster.</param> /// <returns>List of DB clusters.</returns> public async Task<List<DBCluster>> DescribeDBClustersPagedAsync(string? dbClusterIdentifier = null) { var results = new List<DBCluster>(); DescribeDBClustersResponse response; DescribeDBClustersRequest request = new DescribeDBClustersRequest { DBClusterIdentifier = dbClusterIdentifier }; // Get the full list if there are multiple pages. do { response = await _amazonRDS.DescribeDBClustersAsync(request); results.AddRange(response.DBClusters); request.Marker = response.Marker; } while (response.Marker is not null); return results; } /// <summary> /// Create an Amazon Relational Database Service (Amazon RDS) DB instance /// with a particular set of properties. Use the action DescribeDBInstancesAsync /// to determine when the DB instance is ready to use. /// </summary> /// <param name="dbInstanceIdentifier">DB instance identifier.</param> /// <param name="dbClusterIdentifier">DB cluster identifier.</param> /// <param name="dbEngine">The engine for the DB instance.</param> /// <param name="dbEngineVersion">Version for the DB instance.</param> /// <param name="instanceClass">Class for the DB instance.</param> /// <returns>DB instance object.</returns> public async Task<DBInstance> CreateDBInstanceInClusterAsync( string dbClusterIdentifier, string dbInstanceIdentifier, string dbEngine, string dbEngineVersion, string instanceClass) { // When creating the instance within a cluster, do not specify the name or size. var response = await _amazonRDS.CreateDBInstanceAsync( new CreateDBInstanceRequest() { DBClusterIdentifier = dbClusterIdentifier, DBInstanceIdentifier = dbInstanceIdentifier, Engine = dbEngine, EngineVersion = dbEngineVersion, DBInstanceClass = instanceClass }); return response.DBInstance; } /// <summary> /// Create a snapshot of a cluster. /// </summary> /// <param name="dbClusterIdentifier">DB cluster identifier.</param> /// <param name="snapshotIdentifier">Identifier for the snapshot.</param> /// <returns>DB snapshot object.</returns> public async Task<DBClusterSnapshot> CreateClusterSnapshotByIdentifierAsync(string dbClusterIdentifier, string snapshotIdentifier) { var response = await _amazonRDS.CreateDBClusterSnapshotAsync( new CreateDBClusterSnapshotRequest() { DBClusterIdentifier = dbClusterIdentifier, DBClusterSnapshotIdentifier = snapshotIdentifier, }); return response.DBClusterSnapshot; } /// <summary> /// Return a list of DB snapshots for a particular DB cluster. /// </summary> /// <param name="dbClusterIdentifier">DB cluster identifier.</param> /// <returns>List of DB snapshots.</returns> public async Task<List<DBClusterSnapshot>> DescribeDBClusterSnapshotsByIdentifierAsync(string dbClusterIdentifier) { var results = new List<DBClusterSnapshot>(); DescribeDBClusterSnapshotsResponse response; DescribeDBClusterSnapshotsRequest request = new DescribeDBClusterSnapshotsRequest { DBClusterIdentifier = dbClusterIdentifier }; // Get the full list if there are multiple pages. do { response = await _amazonRDS.DescribeDBClusterSnapshotsAsync(request); results.AddRange(response.DBClusterSnapshots); request.Marker = response.Marker; } while (response.Marker is not null); return results; } /// <summary> /// Delete a particular DB cluster. /// </summary> /// <param name="dbClusterIdentifier">DB cluster identifier.</param> /// <returns>DB cluster object.</returns> public async Task<DBCluster> DeleteDBClusterByIdentifierAsync(string dbClusterIdentifier) { var response = await _amazonRDS.DeleteDBClusterAsync( new DeleteDBClusterRequest() { DBClusterIdentifier = dbClusterIdentifier, SkipFinalSnapshot = true }); return response.DBCluster; } /// <summary> /// Delete a particular DB instance. /// </summary> /// <param name="dbInstanceIdentifier">DB instance identifier.</param> /// <returns>DB instance object.</returns> public async Task<DBInstance> DeleteDBInstanceByIdentifierAsync(string dbInstanceIdentifier) { var response = await _amazonRDS.DeleteDBInstanceAsync( new DeleteDBInstanceRequest() { DBInstanceIdentifier = dbInstanceIdentifier, SkipFinalSnapshot = true, DeleteAutomatedBackups = true }); return response.DBInstance; } }
C++
SDK for C++
Note

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

Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; //! Routine which creates an Amazon Aurora DB cluster and demonstrates several operations //! on that cluster. /*! \sa gettingStartedWithDBClusters() \param clientConfiguration: AWS client configuration. \return bool: Successful completion. */ bool AwsDoc::Aurora::gettingStartedWithDBClusters( const Aws::Client::ClientConfiguration &clientConfig) { Aws::RDS::RDSClient client(clientConfig); printAsterisksLine(); std::cout << "Welcome to the Amazon Relational Database Service (Amazon Aurora)" << std::endl; std::cout << "get started with DB clusters demo." << std::endl; printAsterisksLine(); std::cout << "Checking for an existing DB cluster parameter group named '" << CLUSTER_PARAMETER_GROUP_NAME << "'." << std::endl; Aws::String dbParameterGroupFamily("Undefined"); bool parameterGroupFound = true; { // 1. Check if the DB cluster parameter group already exists. Aws::RDS::Model::DescribeDBClusterParameterGroupsRequest request; request.SetDBClusterParameterGroupName(CLUSTER_PARAMETER_GROUP_NAME); Aws::RDS::Model::DescribeDBClusterParameterGroupsOutcome outcome = client.DescribeDBClusterParameterGroups(request); if (outcome.IsSuccess()) { std::cout << "DB cluster parameter group named '" << CLUSTER_PARAMETER_GROUP_NAME << "' already exists." << std::endl; dbParameterGroupFamily = outcome.GetResult().GetDBClusterParameterGroups()[0].GetDBParameterGroupFamily(); } else if (outcome.GetError().GetErrorType() == Aws::RDS::RDSErrors::D_B_PARAMETER_GROUP_NOT_FOUND_FAULT) { std::cout << "DB cluster parameter group named '" << CLUSTER_PARAMETER_GROUP_NAME << "' does not exist." << std::endl; parameterGroupFound = false; } else { std::cerr << "Error with Aurora::DescribeDBClusterParameterGroups. " << outcome.GetError().GetMessage() << std::endl; return false; } } if (!parameterGroupFound) { Aws::Vector<Aws::RDS::Model::DBEngineVersion> engineVersions; // 2. Get available parameter group families for the specified engine. if (!getDBEngineVersions(DB_ENGINE, NO_PARAMETER_GROUP_FAMILY, engineVersions, client)) { return false; } std::cout << "Getting available parameter group families for " << DB_ENGINE << "." << std::endl; std::vector<Aws::String> families; for (const Aws::RDS::Model::DBEngineVersion &version: engineVersions) { Aws::String family = version.GetDBParameterGroupFamily(); if (std::find(families.begin(), families.end(), family) == families.end()) { families.push_back(family); std::cout << " " << families.size() << ": " << family << std::endl; } } int choice = askQuestionForIntRange("Which family do you want to use? ", 1, static_cast<int>(families.size())); dbParameterGroupFamily = families[choice - 1]; } if (!parameterGroupFound) { // 3. Create a DB cluster parameter group. Aws::RDS::Model::CreateDBClusterParameterGroupRequest request; request.SetDBClusterParameterGroupName(CLUSTER_PARAMETER_GROUP_NAME); request.SetDBParameterGroupFamily(dbParameterGroupFamily); request.SetDescription("Example cluster parameter group."); Aws::RDS::Model::CreateDBClusterParameterGroupOutcome outcome = client.CreateDBClusterParameterGroup(request); if (outcome.IsSuccess()) { std::cout << "The DB cluster parameter group was successfully created." << std::endl; } else { std::cerr << "Error with Aurora::CreateDBClusterParameterGroup. " << outcome.GetError().GetMessage() << std::endl; return false; } } printAsterisksLine(); std::cout << "Let's set some parameter values in your cluster parameter group." << std::endl; Aws::Vector<Aws::RDS::Model::Parameter> autoIncrementParameters; // 4. Get the parameters in the DB cluster parameter group. if (!getDBCLusterParameters(CLUSTER_PARAMETER_GROUP_NAME, AUTO_INCREMENT_PREFIX, NO_SOURCE, autoIncrementParameters, client)) { cleanUpResources(CLUSTER_PARAMETER_GROUP_NAME, "", "", client); return false; } Aws::Vector<Aws::RDS::Model::Parameter> updateParameters; for (Aws::RDS::Model::Parameter &autoIncParameter: autoIncrementParameters) { if (autoIncParameter.GetIsModifiable() && (autoIncParameter.GetDataType() == "integer")) { std::cout << "The " << autoIncParameter.GetParameterName() << " is described as: " << autoIncParameter.GetDescription() << "." << std::endl; if (autoIncParameter.ParameterValueHasBeenSet()) { std::cout << "The current value is " << autoIncParameter.GetParameterValue() << "." << std::endl; } std::vector<int> splitValues = splitToInts( autoIncParameter.GetAllowedValues(), '-'); if (splitValues.size() == 2) { int newValue = askQuestionForIntRange( Aws::String("Enter a new value between ") + autoIncParameter.GetAllowedValues() + ": ", splitValues[0], splitValues[1]); autoIncParameter.SetParameterValue(std::to_string(newValue)); updateParameters.push_back(autoIncParameter); } else { std::cerr << "Error parsing " << autoIncParameter.GetAllowedValues() << std::endl; } } } { // 5. Modify the auto increment parameters in the DB cluster parameter group. Aws::RDS::Model::ModifyDBClusterParameterGroupRequest request; request.SetDBClusterParameterGroupName(CLUSTER_PARAMETER_GROUP_NAME); request.SetParameters(updateParameters); Aws::RDS::Model::ModifyDBClusterParameterGroupOutcome outcome = client.ModifyDBClusterParameterGroup(request); if (outcome.IsSuccess()) { std::cout << "The DB cluster parameter group was successfully modified." << std::endl; } else { std::cerr << "Error with Aurora::ModifyDBClusterParameterGroup. " << outcome.GetError().GetMessage() << std::endl; } } std::cout << "You can get a list of parameters you've set by specifying a source of 'user'." << std::endl; Aws::Vector<Aws::RDS::Model::Parameter> userParameters; // 6. Display the modified parameters in the DB cluster parameter group. if (!getDBCLusterParameters(CLUSTER_PARAMETER_GROUP_NAME, NO_NAME_PREFIX, "user", userParameters, client)) { cleanUpResources(CLUSTER_PARAMETER_GROUP_NAME, "", "", client); return false; } for (const auto &userParameter: userParameters) { std::cout << " " << userParameter.GetParameterName() << ", " << userParameter.GetDescription() << ", parameter value - " << userParameter.GetParameterValue() << std::endl; } printAsterisksLine(); std::cout << "Checking for an existing DB Cluster." << std::endl; Aws::RDS::Model::DBCluster dbCluster; // 7. Check if the DB cluster already exists. if (!describeDBCluster(DB_CLUSTER_IDENTIFIER, dbCluster, client)) { cleanUpResources(CLUSTER_PARAMETER_GROUP_NAME, "", "", client); return false; } Aws::String engineVersionName; Aws::String engineName; if (dbCluster.DBClusterIdentifierHasBeenSet()) { std::cout << "The DB cluster already exists." << std::endl; engineVersionName = dbCluster.GetEngineVersion(); engineName = dbCluster.GetEngine(); } else { std::cout << "Let's create a DB cluster." << std::endl; const Aws::String administratorName = askQuestion( "Enter an administrator username for the database: "); const Aws::String administratorPassword = askQuestion( "Enter a password for the administrator (at least 8 characters): "); Aws::Vector<Aws::RDS::Model::DBEngineVersion> engineVersions; // 8. Get a list of engine versions for the parameter group family. if (!getDBEngineVersions(DB_ENGINE, dbParameterGroupFamily, engineVersions, client)) { cleanUpResources(CLUSTER_PARAMETER_GROUP_NAME, "", "", client); return false; } std::cout << "The available engines for your parameter group family are:" << std::endl; int index = 1; for (const Aws::RDS::Model::DBEngineVersion &engineVersion: engineVersions) { std::cout << " " << index << ": " << engineVersion.GetEngineVersion() << std::endl; ++index; } int choice = askQuestionForIntRange("Which engine do you want to use? ", 1, static_cast<int>(engineVersions.size())); const Aws::RDS::Model::DBEngineVersion engineVersion = engineVersions[choice - 1]; engineName = engineVersion.GetEngine(); engineVersionName = engineVersion.GetEngineVersion(); std::cout << "Creating a DB cluster named '" << DB_CLUSTER_IDENTIFIER << "' and database '" << DB_NAME << "'.\n" << "The DB cluster is configured to use your custom cluster parameter group '" << CLUSTER_PARAMETER_GROUP_NAME << "', and \n" << "selected engine version " << engineVersion.GetEngineVersion() << ".\nThis typically takes several minutes." << std::endl; Aws::RDS::Model::CreateDBClusterRequest request; request.SetDBClusterIdentifier(DB_CLUSTER_IDENTIFIER); request.SetDBClusterParameterGroupName(CLUSTER_PARAMETER_GROUP_NAME); request.SetEngine(engineName); request.SetEngineVersion(engineVersionName); request.SetMasterUsername(administratorName); request.SetMasterUserPassword(administratorPassword); Aws::RDS::Model::CreateDBClusterOutcome outcome = client.CreateDBCluster(request); if (outcome.IsSuccess()) { std::cout << "The DB cluster creation has started." << std::endl; } else { std::cerr << "Error with Aurora::CreateDBCluster. " << outcome.GetError().GetMessage() << std::endl; cleanUpResources(CLUSTER_PARAMETER_GROUP_NAME, "", "", client); return false; } } std::cout << "Waiting for the DB cluster to become available." << std::endl; int counter = 0; // 11. Wait for the DB cluster to become available. do { std::this_thread::sleep_for(std::chrono::seconds(1)); ++counter; if (counter > 900) { std::cerr << "Wait for cluster to become available timed out ofter " << counter << " seconds." << std::endl; cleanUpResources(CLUSTER_PARAMETER_GROUP_NAME, DB_CLUSTER_IDENTIFIER, "", client); return false; } dbCluster = Aws::RDS::Model::DBCluster(); if (!describeDBCluster(DB_CLUSTER_IDENTIFIER, dbCluster, client)) { cleanUpResources(CLUSTER_PARAMETER_GROUP_NAME, DB_CLUSTER_IDENTIFIER, "", client); return false; } if ((counter % 20) == 0) { std::cout << "Current DB cluster status is '" << dbCluster.GetStatus() << "' after " << counter << " seconds." << std::endl; } } while (dbCluster.GetStatus() != "available"); if (dbCluster.GetStatus() == "available") { std::cout << "The DB cluster has been created." << std::endl; } printAsterisksLine(); Aws::RDS::Model::DBInstance dbInstance; // 11. Check if the DB instance already exists. if (!describeDBInstance(DB_INSTANCE_IDENTIFIER, dbInstance, client)) { cleanUpResources(CLUSTER_PARAMETER_GROUP_NAME, DB_CLUSTER_IDENTIFIER, "", client); return false; } if (dbInstance.DbInstancePortHasBeenSet()) { std::cout << "The DB instance already exists." << std::endl; } else { std::cout << "Let's create a DB instance." << std::endl; Aws::String dbInstanceClass; // 12. Get a list of instance classes. if (!chooseDBInstanceClass(engineName, engineVersionName, dbInstanceClass, client)) { cleanUpResources(CLUSTER_PARAMETER_GROUP_NAME, DB_CLUSTER_IDENTIFIER, "", client); return false; } std::cout << "Creating a DB instance named '" << DB_INSTANCE_IDENTIFIER << "' with selected DB instance class '" << dbInstanceClass << "'.\nThis typically takes several minutes." << std::endl; // 13. Create a DB instance. Aws::RDS::Model::CreateDBInstanceRequest request; request.SetDBInstanceIdentifier(DB_INSTANCE_IDENTIFIER); request.SetDBClusterIdentifier(DB_CLUSTER_IDENTIFIER); request.SetEngine(engineName); request.SetDBInstanceClass(dbInstanceClass); Aws::RDS::Model::CreateDBInstanceOutcome outcome = client.CreateDBInstance(request); if (outcome.IsSuccess()) { std::cout << "The DB instance creation has started." << std::endl; } else { std::cerr << "Error with RDS::CreateDBInstance. " << outcome.GetError().GetMessage() << std::endl; cleanUpResources(CLUSTER_PARAMETER_GROUP_NAME, DB_CLUSTER_IDENTIFIER, "", client); return false; } } std::cout << "Waiting for the DB instance to become available." << std::endl; counter = 0; // 14. Wait for the DB instance to become available. do { std::this_thread::sleep_for(std::chrono::seconds(1)); ++counter; if (counter > 900) { std::cerr << "Wait for instance to become available timed out ofter " << counter << " seconds." << std::endl; cleanUpResources(CLUSTER_PARAMETER_GROUP_NAME, DB_CLUSTER_IDENTIFIER, DB_INSTANCE_IDENTIFIER, client); return false; } dbInstance = Aws::RDS::Model::DBInstance(); if (!describeDBInstance(DB_INSTANCE_IDENTIFIER, dbInstance, client)) { cleanUpResources(CLUSTER_PARAMETER_GROUP_NAME, DB_CLUSTER_IDENTIFIER, DB_INSTANCE_IDENTIFIER, client); return false; } if ((counter % 20) == 0) { std::cout << "Current DB instance status is '" << dbInstance.GetDBInstanceStatus() << "' after " << counter << " seconds." << std::endl; } } while (dbInstance.GetDBInstanceStatus() != "available"); if (dbInstance.GetDBInstanceStatus() == "available") { std::cout << "The DB instance has been created." << std::endl; } // 15. Display the connection string that can be used to connect a 'mysql' shell to the database. displayConnection(dbCluster); printAsterisksLine(); if (askYesNoQuestion( "Do you want to create a snapshot of your DB cluster (y/n)? ")) { Aws::String snapshotID(DB_CLUSTER_IDENTIFIER + "-" + Aws::String(Aws::Utils::UUID::RandomUUID())); { std::cout << "Creating a snapshot named " << snapshotID << "." << std::endl; std::cout << "This typically takes a few minutes." << std::endl; // 16. Create a snapshot of the DB cluster. (CreateDBClusterSnapshot) Aws::RDS::Model::CreateDBClusterSnapshotRequest request; request.SetDBClusterIdentifier(DB_CLUSTER_IDENTIFIER); request.SetDBClusterSnapshotIdentifier(snapshotID); Aws::RDS::Model::CreateDBClusterSnapshotOutcome outcome = client.CreateDBClusterSnapshot(request); if (outcome.IsSuccess()) { std::cout << "Snapshot creation has started." << std::endl; } else { std::cerr << "Error with Aurora::CreateDBClusterSnapshot. " << outcome.GetError().GetMessage() << std::endl; cleanUpResources(CLUSTER_PARAMETER_GROUP_NAME, DB_CLUSTER_IDENTIFIER, DB_INSTANCE_IDENTIFIER, client); return false; } } std::cout << "Waiting for the snapshot to become available." << std::endl; Aws::RDS::Model::DBClusterSnapshot snapshot; counter = 0; do { std::this_thread::sleep_for(std::chrono::seconds(1)); ++counter; if (counter > 600) { std::cerr << "Wait for snapshot to be available timed out ofter " << counter << " seconds." << std::endl; cleanUpResources(CLUSTER_PARAMETER_GROUP_NAME, DB_CLUSTER_IDENTIFIER, DB_INSTANCE_IDENTIFIER, client); return false; } // 17. Wait for the snapshot to become available. Aws::RDS::Model::DescribeDBClusterSnapshotsRequest request; request.SetDBClusterSnapshotIdentifier(snapshotID); Aws::RDS::Model::DescribeDBClusterSnapshotsOutcome outcome = client.DescribeDBClusterSnapshots(request); if (outcome.IsSuccess()) { snapshot = outcome.GetResult().GetDBClusterSnapshots()[0]; } else { std::cerr << "Error with Aurora::DescribeDBClusterSnapshots. " << outcome.GetError().GetMessage() << std::endl; cleanUpResources(CLUSTER_PARAMETER_GROUP_NAME, DB_CLUSTER_IDENTIFIER, DB_INSTANCE_IDENTIFIER, client); return false; } if ((counter % 20) == 0) { std::cout << "Current snapshot status is '" << snapshot.GetStatus() << "' after " << counter << " seconds." << std::endl; } } while (snapshot.GetStatus() != "available"); if (snapshot.GetStatus() != "available") { std::cout << "A snapshot has been created." << std::endl; } } printAsterisksLine(); bool result = true; if (askYesNoQuestion( "Do you want to delete the DB cluster, DB instance, and parameter group (y/n)? ")) { result = cleanUpResources(CLUSTER_PARAMETER_GROUP_NAME, DB_CLUSTER_IDENTIFIER, DB_INSTANCE_IDENTIFIER, client); } return result; } //! Routine which gets a DB cluster description. /*! \sa describeDBCluster() \param dbClusterIdentifier: A DB cluster identifier. \param clusterResult: The 'DBCluster' object containing the description. \param client: 'RDSClient' instance. \return bool: Successful completion. */ bool AwsDoc::Aurora::describeDBCluster(const Aws::String &dbClusterIdentifier, Aws::RDS::Model::DBCluster &clusterResult, const Aws::RDS::RDSClient &client) { Aws::RDS::Model::DescribeDBClustersRequest request; request.SetDBClusterIdentifier(dbClusterIdentifier); Aws::RDS::Model::DescribeDBClustersOutcome outcome = client.DescribeDBClusters(request); bool result = true; if (outcome.IsSuccess()) { clusterResult = outcome.GetResult().GetDBClusters()[0]; } // This example does not log an error if the DB cluster does not exist. // Instead, it returns false. else if (outcome.GetError().GetErrorType() != Aws::RDS::RDSErrors::D_B_CLUSTER_NOT_FOUND_FAULT) { result = false; std::cerr << "Error with Aurora::GDescribeDBClusters. " << outcome.GetError().GetMessage() << std::endl; } return result; } //! Routine which gets DB parameters using the 'DescribeDBClusterParameters' api. /*! \sa getDBCLusterParameters() \param parameterGroupName: The name of the cluster parameter group. \param namePrefix: Prefix string to filter results by parameter name. \param source: A source such as 'user', ignored if empty. \param parametersResult: Vector of 'Parameter' objects returned by the routine. \param client: 'RDSClient' instance. \return bool: Successful completion. */ bool AwsDoc::Aurora::getDBCLusterParameters(const Aws::String &parameterGroupName, const Aws::String &namePrefix, const Aws::String &source, Aws::Vector<Aws::RDS::Model::Parameter> &parametersResult, const Aws::RDS::RDSClient &client) { Aws::String marker; // The marker is used for pagination. do { Aws::RDS::Model::DescribeDBClusterParametersRequest request; request.SetDBClusterParameterGroupName(CLUSTER_PARAMETER_GROUP_NAME); if (!marker.empty()) { request.SetMarker(marker); } if (!source.empty()) { request.SetSource(source); } Aws::RDS::Model::DescribeDBClusterParametersOutcome outcome = client.DescribeDBClusterParameters(request); if (outcome.IsSuccess()) { const Aws::Vector<Aws::RDS::Model::Parameter> &parameters = outcome.GetResult().GetParameters(); for (const Aws::RDS::Model::Parameter &parameter: parameters) { if (!namePrefix.empty()) { if (parameter.GetParameterName().find(namePrefix) == 0) { parametersResult.push_back(parameter); } } else { parametersResult.push_back(parameter); } } marker = outcome.GetResult().GetMarker(); } else { std::cerr << "Error with Aurora::DescribeDBClusterParameters. " << outcome.GetError().GetMessage() << std::endl; return false; } } while (!marker.empty()); return true; } //! Routine which gets available DB engine versions for an engine name and //! an optional parameter group family. /*! \sa getDBEngineVersions() \param engineName: A DB engine name. \param parameterGroupFamily: A parameter group family name, ignored if empty. \param engineVersionsResult: Vector of 'DBEngineVersion' objects returned by the routine. \param client: 'RDSClient' instance. \return bool: Successful completion. */ bool AwsDoc::Aurora::getDBEngineVersions(const Aws::String &engineName, const Aws::String &parameterGroupFamily, Aws::Vector<Aws::RDS::Model::DBEngineVersion> &engineVersionsResult, const Aws::RDS::RDSClient &client) { Aws::RDS::Model::DescribeDBEngineVersionsRequest request; request.SetEngine(engineName); if (!parameterGroupFamily.empty()) { request.SetDBParameterGroupFamily(parameterGroupFamily); } Aws::RDS::Model::DescribeDBEngineVersionsOutcome outcome = client.DescribeDBEngineVersions(request); if (outcome.IsSuccess()) { engineVersionsResult = outcome.GetResult().GetDBEngineVersions(); } else { std::cerr << "Error with Aurora::DescribeDBEngineVersionsRequest. " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); } //! Routine which gets a DB instance description. /*! \sa describeDBCluster() \param dbInstanceIdentifier: A DB instance identifier. \param instanceResult: The 'DBInstance' object containing the description. \param client: 'RDSClient' instance. \return bool: Successful completion. */ bool AwsDoc::Aurora::describeDBInstance(const Aws::String &dbInstanceIdentifier, Aws::RDS::Model::DBInstance &instanceResult, const Aws::RDS::RDSClient &client) { Aws::RDS::Model::DescribeDBInstancesRequest request; request.SetDBInstanceIdentifier(dbInstanceIdentifier); Aws::RDS::Model::DescribeDBInstancesOutcome outcome = client.DescribeDBInstances(request); bool result = true; if (outcome.IsSuccess()) { instanceResult = outcome.GetResult().GetDBInstances()[0]; } // This example does not log an error if the DB instance does not exist. // Instead, it returns false. else if (outcome.GetError().GetErrorType() != Aws::RDS::RDSErrors::D_B_INSTANCE_NOT_FOUND_FAULT) { result = false; std::cerr << "Error with Aurora::DescribeDBInstances. " << outcome.GetError().GetMessage() << std::endl; } return result; } //! Routine which gets available DB instance classes, displays the list //! to the user, and returns the user selection. /*! \sa chooseDBInstanceClass() \param engineName: The DB engine name. \param engineVersion: The DB engine version. \param dbInstanceClass: String for DB instance class chosen by the user. \param client: 'RDSClient' instance. \return bool: Successful completion. */ bool AwsDoc::Aurora::chooseDBInstanceClass(const Aws::String &engine, const Aws::String &engineVersion, Aws::String &dbInstanceClass, const Aws::RDS::RDSClient &client) { std::vector<Aws::String> instanceClasses; Aws::String marker; // The marker is used for pagination. do { Aws::RDS::Model::DescribeOrderableDBInstanceOptionsRequest request; request.SetEngine(engine); request.SetEngineVersion(engineVersion); if (!marker.empty()) { request.SetMarker(marker); } Aws::RDS::Model::DescribeOrderableDBInstanceOptionsOutcome outcome = client.DescribeOrderableDBInstanceOptions(request); if (outcome.IsSuccess()) { const Aws::Vector<Aws::RDS::Model::OrderableDBInstanceOption> &options = outcome.GetResult().GetOrderableDBInstanceOptions(); for (const Aws::RDS::Model::OrderableDBInstanceOption &option: options) { const Aws::String &instanceClass = option.GetDBInstanceClass(); instanceClasses.push_back(instanceClass); } marker = outcome.GetResult().GetMarker(); } else { std::cerr << "Error with Aurora::DescribeOrderableDBInstanceOptions. " << outcome.GetError().GetMessage() << std::endl; return false; } } while (!marker.empty()); std::cout << "The available DB instance classes for your database engine are:" << std::endl; for (int i = 0; i < instanceClasses.size(); ++i) { std::cout << " " << i + 1 << ": " << instanceClasses[i] << std::endl; } int choice = askQuestionForIntRange( "Which DB instance class do you want to use? ", 1, static_cast<int>(instanceClasses.size())); dbInstanceClass = instanceClasses[choice - 1]; return true; } //! Routine which deletes resources created by the scenario. /*! \sa cleanUpResources() \param parameterGroupName: A parameter group name, this may be empty. \param dbInstanceIdentifier: A DB instance identifier, this may be empty. \param client: 'RDSClient' instance. \return bool: Successful completion. */ bool AwsDoc::Aurora::cleanUpResources(const Aws::String &parameterGroupName, const Aws::String &dbClusterIdentifier, const Aws::String &dbInstanceIdentifier, const Aws::RDS::RDSClient &client) { bool result = true; bool instanceDeleting = false; bool clusterDeleting = false; if (!dbInstanceIdentifier.empty()) { { // 18. Delete the DB instance. Aws::RDS::Model::DeleteDBInstanceRequest request; request.SetDBInstanceIdentifier(dbInstanceIdentifier); request.SetSkipFinalSnapshot(true); request.SetDeleteAutomatedBackups(true); Aws::RDS::Model::DeleteDBInstanceOutcome outcome = client.DeleteDBInstance(request); if (outcome.IsSuccess()) { std::cout << "DB instance deletion has started." << std::endl; instanceDeleting = true; std::cout << "Waiting for DB instance to delete before deleting the parameter group." << std::endl; } else { std::cerr << "Error with Aurora::DeleteDBInstance. " << outcome.GetError().GetMessage() << std::endl; result = false; } } } if (!dbClusterIdentifier.empty()) { { // 19. Delete the DB cluster. Aws::RDS::Model::DeleteDBClusterRequest request; request.SetDBClusterIdentifier(dbClusterIdentifier); request.SetSkipFinalSnapshot(true); Aws::RDS::Model::DeleteDBClusterOutcome outcome = client.DeleteDBCluster(request); if (outcome.IsSuccess()) { std::cout << "DB cluster deletion has started." << std::endl; clusterDeleting = true; std::cout << "Waiting for DB cluster to delete before deleting the parameter group." << std::endl; std::cout << "This may take a while." << std::endl; } else { std::cerr << "Error with Aurora::DeleteDBCluster. " << outcome.GetError().GetMessage() << std::endl; result = false; } } } int counter = 0; while (clusterDeleting || instanceDeleting) { // 20. Wait for the DB cluster and instance to be deleted. std::this_thread::sleep_for(std::chrono::seconds(1)); ++counter; if (counter > 800) { std::cerr << "Wait for instance to delete timed out ofter " << counter << " seconds." << std::endl; return false; } Aws::RDS::Model::DBInstance dbInstance = Aws::RDS::Model::DBInstance(); if (instanceDeleting) { if (!describeDBInstance(dbInstanceIdentifier, dbInstance, client)) { return false; } instanceDeleting = dbInstance.DBInstanceIdentifierHasBeenSet(); } Aws::RDS::Model::DBCluster dbCluster = Aws::RDS::Model::DBCluster(); if (clusterDeleting) { if (!describeDBCluster(dbClusterIdentifier, dbCluster, client)) { return false; } clusterDeleting = dbCluster.DBClusterIdentifierHasBeenSet(); } if ((counter % 20) == 0) { if (instanceDeleting) { std::cout << "Current DB instance status is '" << dbInstance.GetDBInstanceStatus() << "." << std::endl; } if (clusterDeleting) { std::cout << "Current DB cluster status is '" << dbCluster.GetStatus() << "." << std::endl; } } } if (!parameterGroupName.empty()) { // 21. Delete the DB cluster parameter group. Aws::RDS::Model::DeleteDBClusterParameterGroupRequest request; request.SetDBClusterParameterGroupName(parameterGroupName); Aws::RDS::Model::DeleteDBClusterParameterGroupOutcome outcome = client.DeleteDBClusterParameterGroup(request); if (outcome.IsSuccess()) { std::cout << "The DB parameter group was successfully deleted." << std::endl; } else { std::cerr << "Error with Aurora::DeleteDBClusterParameterGroup. " << outcome.GetError().GetMessage() << std::endl; result = false; } } return result; }
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 AWS Code Examples Repository.

/** * Before running this Java (v2) code example, set up your development environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html * * This Java example performs the following tasks: * * 1. Gets available engine families for Amazon Aurora MySQL-Compatible Edition by calling the DescribeDbEngineVersions(Engine='aurora-mysql') method. * 2. Selects an engine family and creates a custom DB cluster parameter group by invoking the describeDBClusterParameters method. * 3. Gets the parameter groups by invoking the describeDBClusterParameterGroups method. * 4. Gets parameters in the group by invoking the describeDBClusterParameters method. * 5. Modifies the auto_increment_offset parameter by invoking the modifyDbClusterParameterGroupRequest method. * 6. Gets and displays the updated parameters. * 7. Gets a list of allowed engine versions by invoking the describeDbEngineVersions method. * 8. Creates an Aurora DB cluster database cluster that contains a MySQL database. * 9. Waits for DB instance to be ready. * 10. Gets a list of instance classes available for the selected engine. * 11. Creates a database instance in the cluster. * 12. Waits for DB instance to be ready. * 13. Creates a snapshot. * 14. Waits for DB snapshot to be ready. * 15. Deletes the DB cluster. * 16. Deletes the DB cluster group. */ public class AuroraScenario { public static long sleepTime = 20; public static final String DASHES = new String(new char[80]).replace("\0", "-"); public static void main(String[] args) throws InterruptedException { final String usage = "\n" + "Usage:\n" + " <dbClusterGroupName> <dbParameterGroupFamily> <dbInstanceClusterIdentifier> <dbInstanceIdentifier> <dbName> <dbSnapshotIdentifier> <username> <userPassword>" + "Where:\n" + " dbClusterGroupName - The name of the DB cluster parameter group. \n"+ " dbParameterGroupFamily - The DB cluster parameter group family name (for example, aurora-mysql5.7). \n"+ " dbInstanceClusterIdentifier - The instance cluster identifier value.\n"+ " dbInstanceIdentifier - The database instance identifier.\n"+ " dbName The database name.\n"+ " dbSnapshotIdentifier - The snapshot identifier.\n"+ " username - The database user name.\n" + " userPassword - The database user name password.\n" ; if (args.length != 8) { System.out.println(usage); System.exit(1); } String dbClusterGroupName = args[0]; String dbParameterGroupFamily = args[1]; String dbInstanceClusterIdentifier = args[2]; String dbInstanceIdentifier = args[3]; String dbName = args[4]; String dbSnapshotIdentifier = args[5]; String username = args[6]; String userPassword = args[7]; Region region = Region.US_WEST_2; RdsClient rdsClient = RdsClient.builder() .region(region) .credentialsProvider(ProfileCredentialsProvider.create()) .build(); System.out.println(DASHES); System.out.println("Welcome to the Amazon Aurora example scenario."); System.out.println(DASHES); System.out.println(DASHES); System.out.println("1. Return a list of the available DB engines"); describeDBEngines(rdsClient); System.out.println(DASHES); System.out.println(DASHES); System.out.println("2. Create a custom parameter group"); createDBClusterParameterGroup(rdsClient, dbClusterGroupName, dbParameterGroupFamily); System.out.println(DASHES); System.out.println(DASHES); System.out.println("3. Get the parameter group"); describeDbClusterParameterGroups(rdsClient, dbClusterGroupName); System.out.println(DASHES); System.out.println(DASHES); System.out.println("4. Get the parameters in the group"); describeDbClusterParameters(rdsClient, dbClusterGroupName, 0); System.out.println(DASHES); System.out.println(DASHES); System.out.println("5. Modify the auto_increment_offset parameter"); modifyDBClusterParas(rdsClient, dbClusterGroupName); System.out.println(DASHES); System.out.println(DASHES); System.out.println("6. Display the updated parameter value"); describeDbClusterParameters(rdsClient, dbClusterGroupName, -1); System.out.println(DASHES); System.out.println(DASHES); System.out.println("7. Get a list of allowed engine versions"); getAllowedEngines(rdsClient, dbParameterGroupFamily); System.out.println(DASHES); System.out.println(DASHES); System.out.println("8. Create an Aurora DB cluster database"); String arnClusterVal = createDBCluster(rdsClient, dbClusterGroupName, dbName, dbInstanceClusterIdentifier, username, userPassword) ; System.out.println("The ARN of the cluster is "+arnClusterVal); System.out.println(DASHES); System.out.println(DASHES); System.out.println("9. Wait for DB instance to be ready" ); waitForInstanceReady(rdsClient, dbInstanceClusterIdentifier); System.out.println(DASHES); System.out.println(DASHES); System.out.println("10. Get a list of instance classes available for the selected engine"); String instanceClass = getListInstanceClasses(rdsClient); System.out.println(DASHES); System.out.println(DASHES); System.out.println("11. Create a database instance in the cluster."); String clusterDBARN = createDBInstanceCluster(rdsClient, dbInstanceIdentifier, dbInstanceClusterIdentifier, instanceClass); System.out.println("The ARN of the database is "+clusterDBARN); System.out.println(DASHES); System.out.println(DASHES); System.out.println("12. Wait for DB instance to be ready" ); waitDBInstanceReady(rdsClient, dbInstanceIdentifier); System.out.println(DASHES); System.out.println(DASHES); System.out.println("13. Create a snapshot"); createDBClusterSnapshot(rdsClient, dbInstanceClusterIdentifier, dbSnapshotIdentifier); System.out.println(DASHES); System.out.println(DASHES); System.out.println("14. Wait for DB snapshot to be ready" ); waitForSnapshotReady(rdsClient, dbSnapshotIdentifier, dbInstanceClusterIdentifier); System.out.println(DASHES); System.out.println(DASHES); System.out.println("14. Delete the DB instance" ); deleteDatabaseInstance(rdsClient, dbInstanceIdentifier); System.out.println(DASHES); System.out.println(DASHES); System.out.println("15. Delete the DB cluster"); deleteCluster(rdsClient, dbInstanceClusterIdentifier); System.out.println(DASHES); System.out.println(DASHES); System.out.println("16. Delete the DB cluster group"); deleteDBClusterGroup(rdsClient, dbClusterGroupName, clusterDBARN); System.out.println(DASHES); System.out.println(DASHES); System.out.println("The Scenario has successfully completed." ); System.out.println(DASHES); rdsClient.close(); } public static void deleteDBClusterGroup( RdsClient rdsClient, String dbClusterGroupName, String clusterDBARN) throws InterruptedException { try { boolean isDataDel = false; boolean didFind; String instanceARN ; // Make sure that the database has been deleted. while (!isDataDel) { DescribeDbInstancesResponse response = rdsClient.describeDBInstances(); List<DBInstance> instanceList = response.dbInstances(); int listSize = instanceList.size(); isDataDel = false ; didFind = false; int index = 1; for (DBInstance instance: instanceList) { instanceARN = instance.dbInstanceArn(); if (instanceARN.compareTo(clusterDBARN) == 0) { System.out.println(clusterDBARN + " still exists"); didFind = true ; } if ((index == listSize) && (!didFind)) { // Went through the entire list and did not find the database ARN. isDataDel = true; } Thread.sleep(sleepTime * 1000); index ++; } } DeleteDbClusterParameterGroupRequest clusterParameterGroupRequest = DeleteDbClusterParameterGroupRequest.builder() .dbClusterParameterGroupName(dbClusterGroupName) .build(); rdsClient.deleteDBClusterParameterGroup(clusterParameterGroupRequest); System.out.println(dbClusterGroupName +" was deleted."); } catch (RdsException e) { System.out.println(e.getLocalizedMessage()); System.exit(1); } } public static void deleteCluster(RdsClient rdsClient, String dbInstanceClusterIdentifier) { try { DeleteDbClusterRequest deleteDbClusterRequest = DeleteDbClusterRequest.builder() .dbClusterIdentifier(dbInstanceClusterIdentifier) .skipFinalSnapshot(true) .build(); rdsClient.deleteDBCluster(deleteDbClusterRequest); System.out.println(dbInstanceClusterIdentifier +" was deleted!"); } catch (RdsException e) { System.out.println(e.getLocalizedMessage()); System.exit(1); } } public static void deleteDatabaseInstance( RdsClient rdsClient, String dbInstanceIdentifier) { try { DeleteDbInstanceRequest deleteDbInstanceRequest = DeleteDbInstanceRequest.builder() .dbInstanceIdentifier(dbInstanceIdentifier) .deleteAutomatedBackups(true) .skipFinalSnapshot(true) .build(); DeleteDbInstanceResponse response = rdsClient.deleteDBInstance(deleteDbInstanceRequest); System.out.println("The status of the database is " + response.dbInstance().dbInstanceStatus()); } catch (RdsException e) { System.out.println(e.getLocalizedMessage()); System.exit(1); } } public static void waitForSnapshotReady(RdsClient rdsClient, String dbSnapshotIdentifier, String dbInstanceClusterIdentifier) { try { boolean snapshotReady = false; String snapshotReadyStr; System.out.println("Waiting for the snapshot to become available."); DescribeDbClusterSnapshotsRequest snapshotsRequest = DescribeDbClusterSnapshotsRequest.builder() .dbClusterSnapshotIdentifier(dbSnapshotIdentifier) .dbClusterIdentifier(dbInstanceClusterIdentifier) .build(); while (!snapshotReady) { DescribeDbClusterSnapshotsResponse response = rdsClient.describeDBClusterSnapshots (snapshotsRequest); List<DBClusterSnapshot> snapshotList = response.dbClusterSnapshots(); for (DBClusterSnapshot snapshot : snapshotList) { snapshotReadyStr = snapshot.status(); if (snapshotReadyStr.contains("available")) { snapshotReady = true; } else { System.out.println("."); Thread.sleep(sleepTime * 5000); } } } System.out.println("The Snapshot is available!"); } catch (RdsException | InterruptedException e) { System.out.println(e.getLocalizedMessage()); System.exit(1); } } public static void createDBClusterSnapshot(RdsClient rdsClient, String dbInstanceClusterIdentifier, String dbSnapshotIdentifier) { try { CreateDbClusterSnapshotRequest snapshotRequest = CreateDbClusterSnapshotRequest.builder() .dbClusterIdentifier(dbInstanceClusterIdentifier) .dbClusterSnapshotIdentifier(dbSnapshotIdentifier) .build(); CreateDbClusterSnapshotResponse response = rdsClient.createDBClusterSnapshot(snapshotRequest); System.out.println("The Snapshot ARN is " + response.dbClusterSnapshot().dbClusterSnapshotArn()); } catch (RdsException e) { System.out.println(e.getLocalizedMessage()); System.exit(1); } } public static void waitDBInstanceReady(RdsClient rdsClient, String dbInstanceIdentifier) { boolean instanceReady = false; String instanceReadyStr; System.out.println("Waiting for instance to become available."); try { DescribeDbInstancesRequest instanceRequest = DescribeDbInstancesRequest.builder() .dbInstanceIdentifier(dbInstanceIdentifier) .build(); String endpoint=""; while (!instanceReady) { DescribeDbInstancesResponse response = rdsClient.describeDBInstances(instanceRequest); List<DBInstance> instanceList = response.dbInstances(); for (DBInstance instance : instanceList) { instanceReadyStr = instance.dbInstanceStatus(); if (instanceReadyStr.contains("available")) { endpoint = instance.endpoint().address(); instanceReady = true; } else { System.out.print("."); Thread.sleep(sleepTime * 1000); } } } System.out.println("Database instance is available! The connection endpoint is "+ endpoint); } catch (RdsException | InterruptedException e) { System.err.println(e.getMessage()); System.exit(1); } } public static String createDBInstanceCluster(RdsClient rdsClient, String dbInstanceIdentifier, String dbInstanceClusterIdentifier, String instanceClass){ try { CreateDbInstanceRequest instanceRequest = CreateDbInstanceRequest.builder() .dbInstanceIdentifier(dbInstanceIdentifier) .dbClusterIdentifier(dbInstanceClusterIdentifier) .engine("aurora-mysql") .dbInstanceClass(instanceClass) .build() ; CreateDbInstanceResponse response = rdsClient.createDBInstance(instanceRequest); System.out.print("The status is " + response.dbInstance().dbInstanceStatus()); return response.dbInstance().dbInstanceArn(); } catch (RdsException e) { System.err.println(e.getMessage()); System.exit(1); } return ""; } public static String getListInstanceClasses(RdsClient rdsClient) { try{ DescribeOrderableDbInstanceOptionsRequest optionsRequest = DescribeOrderableDbInstanceOptionsRequest.builder() .engine("aurora-mysql") .maxRecords(20) .build(); DescribeOrderableDbInstanceOptionsResponse response = rdsClient.describeOrderableDBInstanceOptions(optionsRequest); List<OrderableDBInstanceOption> instanceOptions = response.orderableDBInstanceOptions(); String instanceClass = ""; for (OrderableDBInstanceOption instanceOption: instanceOptions) { instanceClass = instanceOption.dbInstanceClass(); System.out.println("The instance class is " +instanceOption.dbInstanceClass()); System.out.println("The engine version is " +instanceOption.engineVersion()); } return instanceClass; } catch (RdsException e) { System.err.println(e.getMessage()); System.exit(1); } return ""; } // Waits until the database instance is available. public static void waitForInstanceReady(RdsClient rdsClient, String dbClusterIdentifier) { boolean instanceReady = false; String instanceReadyStr; System.out.println("Waiting for instance to become available."); try { DescribeDbClustersRequest instanceRequest = DescribeDbClustersRequest.builder() .dbClusterIdentifier(dbClusterIdentifier) .build(); while (!instanceReady) { DescribeDbClustersResponse response = rdsClient.describeDBClusters(instanceRequest); List<DBCluster> clusterList = response.dbClusters(); for (DBCluster cluster : clusterList) { instanceReadyStr = cluster.status(); if (instanceReadyStr.contains("available")) { instanceReady = true; } else { System.out.print("."); Thread.sleep(sleepTime * 1000); } } } System.out.println("Database cluster is available!"); } catch (RdsException | InterruptedException e) { System.err.println(e.getMessage()); System.exit(1); } } public static String createDBCluster(RdsClient rdsClient, String dbParameterGroupFamily, String dbName, String dbClusterIdentifier, String userName, String password) { try { CreateDbClusterRequest clusterRequest = CreateDbClusterRequest.builder() .databaseName(dbName) .dbClusterIdentifier(dbClusterIdentifier) .dbClusterParameterGroupName(dbParameterGroupFamily) .engine("aurora-mysql") .masterUsername(userName) .masterUserPassword(password) .build(); CreateDbClusterResponse response = rdsClient.createDBCluster(clusterRequest); return response.dbCluster().dbClusterArn(); } catch (RdsException e) { System.out.println(e.getLocalizedMessage()); System.exit(1); } return ""; } // Get a list of allowed engine versions. public static void getAllowedEngines(RdsClient rdsClient, String dbParameterGroupFamily) { try { DescribeDbEngineVersionsRequest versionsRequest = DescribeDbEngineVersionsRequest.builder() .dbParameterGroupFamily(dbParameterGroupFamily) .engine("aurora-mysql") .build(); DescribeDbEngineVersionsResponse response = rdsClient.describeDBEngineVersions(versionsRequest); List<DBEngineVersion> dbEngines = response.dbEngineVersions(); for (DBEngineVersion dbEngine: dbEngines) { System.out.println("The engine version is " +dbEngine.engineVersion()); System.out.println("The engine description is " +dbEngine.dbEngineDescription()); } } catch (RdsException e) { System.out.println(e.getLocalizedMessage()); System.exit(1); } } // Modify the auto_increment_offset parameter. public static void modifyDBClusterParas(RdsClient rdsClient, String dClusterGroupName) { try { Parameter parameter1 = Parameter.builder() .parameterName("auto_increment_offset") .applyMethod("immediate") .parameterValue("5") .build(); List<Parameter> paraList = new ArrayList<>(); paraList.add(parameter1); ModifyDbClusterParameterGroupRequest groupRequest = ModifyDbClusterParameterGroupRequest.builder() .dbClusterParameterGroupName(dClusterGroupName) .parameters(paraList) .build(); ModifyDbClusterParameterGroupResponse response = rdsClient.modifyDBClusterParameterGroup(groupRequest); System.out.println("The parameter group "+ response.dbClusterParameterGroupName() +" was successfully modified"); } catch (RdsException e) { System.out.println(e.getLocalizedMessage()); System.exit(1); } } public static void describeDbClusterParameters(RdsClient rdsClient, String dbCLusterGroupName, int flag) { try { DescribeDbClusterParametersRequest dbParameterGroupsRequest; if (flag == 0) { dbParameterGroupsRequest = DescribeDbClusterParametersRequest.builder() .dbClusterParameterGroupName(dbCLusterGroupName) .build(); } else { dbParameterGroupsRequest = DescribeDbClusterParametersRequest.builder() .dbClusterParameterGroupName(dbCLusterGroupName) .source("user") .build(); } DescribeDbClusterParametersResponse response = rdsClient.describeDBClusterParameters(dbParameterGroupsRequest); List<Parameter> dbParameters = response.parameters(); String paraName; for (Parameter para: dbParameters) { // Only print out information about either auto_increment_offset or auto_increment_increment. paraName = para.parameterName(); if ( (paraName.compareTo("auto_increment_offset") ==0) || (paraName.compareTo("auto_increment_increment ") ==0)) { System.out.println("*** The parameter name is " + paraName); System.out.println("*** The parameter value is " + para.parameterValue()); System.out.println("*** The parameter data type is " + para.dataType()); System.out.println("*** The parameter description is " + para.description()); System.out.println("*** The parameter allowed values is " + para.allowedValues()); } } } catch (RdsException e) { System.out.println(e.getLocalizedMessage()); System.exit(1); } } public static void describeDbClusterParameterGroups(RdsClient rdsClient, String dbClusterGroupName) { try { DescribeDbClusterParameterGroupsRequest groupsRequest = DescribeDbClusterParameterGroupsRequest.builder() .dbClusterParameterGroupName(dbClusterGroupName) .maxRecords(20) .build(); List<DBClusterParameterGroup> groups = rdsClient.describeDBClusterParameterGroups(groupsRequest).dbClusterParameterGroups(); for (DBClusterParameterGroup group: groups) { System.out.println("The group name is "+group.dbClusterParameterGroupName()); System.out.println("The group ARN is "+group.dbClusterParameterGroupArn()); } } catch (RdsException e) { System.out.println(e.getLocalizedMessage()); System.exit(1); } } public static void createDBClusterParameterGroup(RdsClient rdsClient, String dbClusterGroupName, String dbParameterGroupFamily) { try { CreateDbClusterParameterGroupRequest groupRequest = CreateDbClusterParameterGroupRequest.builder() .dbClusterParameterGroupName(dbClusterGroupName) .dbParameterGroupFamily(dbParameterGroupFamily) .description("Created by using the AWS SDK for Java") .build(); CreateDbClusterParameterGroupResponse response = rdsClient.createDBClusterParameterGroup(groupRequest); System.out.println("The group name is "+ response.dbClusterParameterGroup().dbClusterParameterGroupName()); } catch (RdsException e) { System.out.println(e.getLocalizedMessage()); System.exit(1); } } public static void describeDBEngines( RdsClient rdsClient) { try { DescribeDbEngineVersionsRequest engineVersionsRequest = DescribeDbEngineVersionsRequest.builder() .engine("aurora-mysql") .defaultOnly(true) .maxRecords(20) .build(); DescribeDbEngineVersionsResponse response = rdsClient.describeDBEngineVersions(engineVersionsRequest); List<DBEngineVersion> engines = response.dbEngineVersions(); // Get all DBEngineVersion objects. for (DBEngineVersion engineOb: engines) { System.out.println("The name of the DB parameter group family for the database engine is "+engineOb.dbParameterGroupFamily()); System.out.println("The name of the database engine "+engineOb.engine()); System.out.println("The version number of the database engine "+engineOb.engineVersion()); } } catch (RdsException e) { System.out.println(e.getLocalizedMessage()); System.exit(1); } } }
Kotlin
SDK for Kotlin
Note

This is prerelease documentation for a feature in preview release. It is subject to change.

Note

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

/** Before running this Kotlin code example, set up your development environment, including your credentials. For more information, see the following documentation topic: https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html This Kotlin example performs the following tasks: 1. Returns a list of the available DB engines. 2. Creates a custom DB parameter group. 3. Gets the parameter groups. 4. Gets the parameters in the group. 5. Modifies the auto_increment_increment parameter. 6. Displays the updated parameter value. 7. Gets a list of allowed engine versions. 8. Creates an Aurora DB cluster database. 9. Waits for DB instance to be ready. 10. Gets a list of instance classes available for the selected engine. 11. Creates a database instance in the cluster. 12. Waits for the database instance in the cluster to be ready. 13. Creates a snapshot. 14. Waits for DB snapshot to be ready. 15. Deletes the DB instance. 16. Deletes the DB cluster. 17. Deletes the DB cluster group. */ var slTime: Long = 20 suspend fun main(args: Array<String>) { val usage = """ Usage: <dbClusterGroupName> <dbParameterGroupFamily> <dbInstanceClusterIdentifier> <dbName> <dbSnapshotIdentifier> <username> <userPassword> Where: dbClusterGroupName - The database group name. dbParameterGroupFamily - The database parameter group name. dbInstanceClusterIdentifier - The database instance identifier. dbName - The database name. dbSnapshotIdentifier - The snapshot identifier. username - The user name. userPassword - The password that corresponds to the user name. """ if (args.size != 7) { println(usage) exitProcess(1) } val dbClusterGroupName = args[0] val dbParameterGroupFamily = args[1] val dbInstanceClusterIdentifier = args[2] val dbInstanceIdentifier = args[3] val dbName = args[4] val dbSnapshotIdentifier = args[5] val username = args[6] val userPassword = args[7] println("1. Return a list of the available DB engines") describeAuroraDBEngines() println("2. Create a custom parameter group") createDBClusterParameterGroup(dbClusterGroupName, dbParameterGroupFamily) println("3. Get the parameter group") describeDbClusterParameterGroups(dbClusterGroupName) println("4. Get the parameters in the group") describeDbClusterParameters(dbClusterGroupName, 0) println("5. Modify the auto_increment_offset parameter") modifyDBClusterParas(dbClusterGroupName) println("6. Display the updated parameter value") describeDbClusterParameters(dbClusterGroupName, -1) println("7. Get a list of allowed engine versions") getAllowedClusterEngines(dbParameterGroupFamily) println("8. Create an Aurora DB cluster database") val arnClusterVal = createDBCluster(dbClusterGroupName, dbName, dbInstanceClusterIdentifier, username, userPassword) println("The ARN of the cluster is $arnClusterVal") println("9. Wait for DB instance to be ready") waitForClusterInstanceReady(dbInstanceClusterIdentifier) println("10. Get a list of instance classes available for the selected engine") val instanceClass = getListInstanceClasses() println("11. Create a database instance in the cluster.") val clusterDBARN = createDBInstanceCluster(dbInstanceIdentifier, dbInstanceClusterIdentifier, instanceClass) println("The ARN of the database is $clusterDBARN") println("12. Wait for DB instance to be ready") waitDBAuroraInstanceReady(dbInstanceIdentifier) println("13. Create a snapshot") createDBClusterSnapshot(dbInstanceClusterIdentifier, dbSnapshotIdentifier) println("14. Wait for DB snapshot to be ready") waitSnapshotReady(dbSnapshotIdentifier, dbInstanceClusterIdentifier) println("15. Delete the DB instance") deleteDBInstance(dbInstanceIdentifier) println("16. Delete the DB cluster") deleteCluster(dbInstanceClusterIdentifier) println("17. Delete the DB cluster group") if (clusterDBARN != null) { deleteDBClusterGroup(dbClusterGroupName, clusterDBARN) } println("The Scenario has successfully completed.") } @Throws(InterruptedException::class) suspend fun deleteDBClusterGroup(dbClusterGroupName: String, clusterDBARN: String) { var isDataDel = false var didFind: Boolean var instanceARN: String RdsClient { region = "us-west-2" }.use { rdsClient -> // Make sure that the database has been deleted. while (!isDataDel) { val response = rdsClient.describeDbInstances() val instanceList = response.dbInstances val listSize = instanceList?.size isDataDel = false didFind = false var index = 1 if (instanceList != null) { for (instance in instanceList) { instanceARN = instance.dbInstanceArn.toString() if (instanceARN.compareTo(clusterDBARN) == 0) { println("$clusterDBARN still exists") didFind = true } if (index == listSize && !didFind) { // Went through the entire list and did not find the database ARN. isDataDel = true } delay(slTime * 1000) index++ } } } val clusterParameterGroupRequest = DeleteDbClusterParameterGroupRequest { dbClusterParameterGroupName = dbClusterGroupName } rdsClient.deleteDbClusterParameterGroup(clusterParameterGroupRequest) println("$dbClusterGroupName was deleted.") } } suspend fun deleteCluster(dbInstanceClusterIdentifier: String) { val deleteDbClusterRequest = DeleteDbClusterRequest { dbClusterIdentifier = dbInstanceClusterIdentifier skipFinalSnapshot = true } RdsClient { region = "us-west-2" }.use { rdsClient -> rdsClient.deleteDbCluster(deleteDbClusterRequest) println("$dbInstanceClusterIdentifier was deleted!") } } suspend fun deleteDBInstance(dbInstanceIdentifierVal: String) { val deleteDbInstanceRequest = DeleteDbInstanceRequest { dbInstanceIdentifier = dbInstanceIdentifierVal deleteAutomatedBackups = true skipFinalSnapshot = true } RdsClient { region = "us-west-2" }.use { rdsClient -> val response = rdsClient.deleteDbInstance(deleteDbInstanceRequest) print("The status of the database is ${response.dbInstance?.dbInstanceStatus}") } } suspend fun waitSnapshotReady(dbSnapshotIdentifier: String?, dbInstanceClusterIdentifier: String?) { var snapshotReady = false var snapshotReadyStr: String println("Waiting for the snapshot to become available.") val snapshotsRequest = DescribeDbClusterSnapshotsRequest { dbClusterSnapshotIdentifier = dbSnapshotIdentifier dbClusterIdentifier = dbInstanceClusterIdentifier } RdsClient { region = "us-west-2" }.use { rdsClient -> while (!snapshotReady) { val response = rdsClient.describeDbClusterSnapshots(snapshotsRequest) val snapshotList = response.dbClusterSnapshots if (snapshotList != null) { for (snapshot in snapshotList) { snapshotReadyStr = snapshot.status.toString() if (snapshotReadyStr.contains("available")) { snapshotReady = true } else { println(".") delay(slTime * 5000) } } } } } println("The Snapshot is available!") } suspend fun createDBClusterSnapshot(dbInstanceClusterIdentifier: String?, dbSnapshotIdentifier: String?) { val snapshotRequest = CreateDbClusterSnapshotRequest { dbClusterIdentifier = dbInstanceClusterIdentifier dbClusterSnapshotIdentifier = dbSnapshotIdentifier } RdsClient { region = "us-west-2" }.use { rdsClient -> val response = rdsClient.createDbClusterSnapshot(snapshotRequest) println("The Snapshot ARN is ${response.dbClusterSnapshot?.dbClusterSnapshotArn}") } } suspend fun waitDBAuroraInstanceReady(dbInstanceIdentifierVal: String?) { var instanceReady = false var instanceReadyStr: String println("Waiting for instance to become available.") val instanceRequest = DescribeDbInstancesRequest { dbInstanceIdentifier = dbInstanceIdentifierVal } var endpoint = "" RdsClient { region = "us-west-2" }.use { rdsClient -> while (!instanceReady) { val response = rdsClient.describeDbInstances(instanceRequest) response.dbInstances?.forEach { instance -> instanceReadyStr = instance.dbInstanceStatus.toString() if (instanceReadyStr.contains("available")) { endpoint = instance.endpoint?.address.toString() instanceReady = true } else { print(".") delay(sleepTime * 1000) } } } } println("Database instance is available! The connection endpoint is $endpoint") } suspend fun createDBInstanceCluster(dbInstanceIdentifierVal: String?, dbInstanceClusterIdentifierVal: String?, instanceClassVal: String?): String? { val instanceRequest = CreateDbInstanceRequest { dbInstanceIdentifier = dbInstanceIdentifierVal dbClusterIdentifier = dbInstanceClusterIdentifierVal engine = "aurora-mysql" dbInstanceClass = instanceClassVal } RdsClient { region = "us-west-2" }.use { rdsClient -> val response = rdsClient.createDbInstance(instanceRequest) print("The status is ${response.dbInstance?.dbInstanceStatus}") return response.dbInstance?.dbInstanceArn } } suspend fun getListInstanceClasses(): String { val optionsRequest = DescribeOrderableDbInstanceOptionsRequest { engine = "aurora-mysql" maxRecords = 20 } var instanceClass = "" RdsClient { region = "us-west-2" }.use { rdsClient -> val response = rdsClient.describeOrderableDbInstanceOptions(optionsRequest) response.orderableDbInstanceOptions?.forEach { instanceOption -> instanceClass = instanceOption.dbInstanceClass.toString() println("The instance class is ${instanceOption.dbInstanceClass}") println("The engine version is ${instanceOption.engineVersion}") } } return instanceClass } // Waits until the database instance is available. suspend fun waitForClusterInstanceReady(dbClusterIdentifierVal: String?) { var instanceReady = false var instanceReadyStr: String println("Waiting for instance to become available.") val instanceRequest = DescribeDbClustersRequest { dbClusterIdentifier = dbClusterIdentifierVal } RdsClient { region = "us-west-2" }.use { rdsClient -> while (!instanceReady) { val response = rdsClient.describeDbClusters(instanceRequest) response.dbClusters?.forEach { cluster -> instanceReadyStr = cluster.status.toString() if (instanceReadyStr.contains("available")) { instanceReady = true } else { print(".") delay(sleepTime * 1000) } } } } println("Database cluster is available!") } suspend fun createDBCluster(dbParameterGroupFamilyVal: String?, dbName: String?, dbClusterIdentifierVal: String?, userName: String?, password: String?): String? { val clusterRequest = CreateDbClusterRequest { databaseName = dbName dbClusterIdentifier = dbClusterIdentifierVal dbClusterParameterGroupName = dbParameterGroupFamilyVal engine = "aurora-mysql" masterUsername = userName masterUserPassword = password } RdsClient { region = "us-west-2" }.use { rdsClient -> val response = rdsClient.createDbCluster(clusterRequest) return response.dbCluster?.dbClusterArn } } // Get a list of allowed engine versions. suspend fun getAllowedClusterEngines(dbParameterGroupFamilyVal: String?) { val versionsRequest = DescribeDbEngineVersionsRequest { dbParameterGroupFamily = dbParameterGroupFamilyVal engine = "aurora-mysql" } RdsClient { region = "us-west-2" }.use { rdsClient -> val response = rdsClient.describeDbEngineVersions(versionsRequest) response.dbEngineVersions?.forEach { dbEngine -> println("The engine version is ${dbEngine.engineVersion}") println("The engine description is ${dbEngine.dbEngineDescription}") } } } // Modify the auto_increment_offset parameter. suspend fun modifyDBClusterParas(dClusterGroupName: String?) { val parameter1 = Parameter { parameterName = "auto_increment_offset" applyMethod = ApplyMethod.fromValue("immediate") parameterValue = "5" } val paraList = ArrayList<Parameter>() paraList.add(parameter1) val groupRequest = ModifyDbClusterParameterGroupRequest { dbClusterParameterGroupName = dClusterGroupName parameters = paraList } RdsClient { region = "us-west-2" }.use { rdsClient -> val response = rdsClient.modifyDbClusterParameterGroup(groupRequest) println("The parameter group ${response.dbClusterParameterGroupName} was successfully modified") } } suspend fun describeDbClusterParameters(dbCLusterGroupName: String?, flag: Int) { val dbParameterGroupsRequest: DescribeDbClusterParametersRequest dbParameterGroupsRequest = if (flag == 0) { DescribeDbClusterParametersRequest { dbClusterParameterGroupName = dbCLusterGroupName } } else { DescribeDbClusterParametersRequest { dbClusterParameterGroupName = dbCLusterGroupName source = "user" } } RdsClient { region = "us-west-2" }.use { rdsClient -> val response = rdsClient.describeDbClusterParameters(dbParameterGroupsRequest) response.parameters?.forEach { para -> // Only print out information about either auto_increment_offset or auto_increment_increment. val paraName = para.parameterName if (paraName != null) { if (paraName.compareTo("auto_increment_offset") == 0 || paraName.compareTo("auto_increment_increment ") == 0) { println("*** The parameter name is $paraName") println("*** The parameter value is ${para.parameterValue}") println("*** The parameter data type is ${para.dataType}") println("*** The parameter description is ${para.description}") println("*** The parameter allowed values is ${para.allowedValues}") } } } } } suspend fun describeDbClusterParameterGroups(dbClusterGroupName: String?) { val groupsRequest = DescribeDbClusterParameterGroupsRequest { dbClusterParameterGroupName = dbClusterGroupName maxRecords = 20 } RdsClient { region = "us-west-2" }.use { rdsClient -> val response = rdsClient.describeDbClusterParameterGroups(groupsRequest) response.dbClusterParameterGroups?.forEach { group -> println("The group name is ${group.dbClusterParameterGroupName}") println("The group ARN is ${group.dbClusterParameterGroupArn}") } } } suspend fun createDBClusterParameterGroup(dbClusterGroupNameVal: String?, dbParameterGroupFamilyVal: String?) { val groupRequest = CreateDbClusterParameterGroupRequest { dbClusterParameterGroupName = dbClusterGroupNameVal dbParameterGroupFamily = dbParameterGroupFamilyVal description = "Created by using the AWS SDK for Kotlin" } RdsClient { region = "us-west-2" }.use { rdsClient -> val response = rdsClient.createDbClusterParameterGroup(groupRequest) println("The group name is ${response.dbClusterParameterGroup?.dbClusterParameterGroupName}") } } suspend fun describeAuroraDBEngines() { val engineVersionsRequest = DescribeDbEngineVersionsRequest { engine = "aurora-mysql" defaultOnly = true maxRecords = 20 } RdsClient { region = "us-west-2" }.use { rdsClient -> val response = rdsClient.describeDbEngineVersions(engineVersionsRequest) response.dbEngineVersions?.forEach { engineOb -> println("The name of the DB parameter group family for the database engine is ${engineOb.dbParameterGroupFamily}") println("The name of the database engine ${engineOb.engine}") println("The version number of the database engine ${engineOb.engineVersion}") } } }
Python
SDK for Python (Boto3)
Note

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

Run an interactive scenario at a command prompt.

class AuroraClusterScenario: """Runs a scenario that shows how to get started using Aurora DB clusters.""" def __init__(self, aurora_wrapper): """ :param aurora_wrapper: An object that wraps Aurora DB cluster actions. """ self.aurora_wrapper = aurora_wrapper def create_parameter_group(self, db_engine, parameter_group_name): """ Shows how to get available engine versions for a specified database engine and create a DB cluster parameter group that is compatible with a selected engine family. :param db_engine: The database engine to use as a basis. :param parameter_group_name: The name given to the newly created parameter group. :return: The newly created parameter group. """ print(f"Checking for an existing DB cluster parameter group named {parameter_group_name}.") parameter_group = self.aurora_wrapper.get_parameter_group(parameter_group_name) if parameter_group is None: print(f"Getting available database engine versions for {db_engine}.") engine_versions = self.aurora_wrapper.get_engine_versions(db_engine) families = list({ver['DBParameterGroupFamily'] for ver in engine_versions}) family_index = q.choose("Which family do you want to use? ", families) print(f"Creating a DB cluster parameter group.") self.aurora_wrapper.create_parameter_group( parameter_group_name, families[family_index], 'Example parameter group.') parameter_group = self.aurora_wrapper.get_parameter_group(parameter_group_name) print(f"Parameter group {parameter_group['DBClusterParameterGroupName']}:") pp(parameter_group) print('-'*88) return parameter_group def set_user_parameters(self, parameter_group_name): """ Shows how to get the parameters contained in a custom parameter group and update some of the parameter values in the group. :param parameter_group_name: The name of the parameter group to query and modify. """ print("Let's set some parameter values in your parameter group.") auto_inc_parameters = self.aurora_wrapper.get_parameters( parameter_group_name, name_prefix='auto_increment') update_params = [] for auto_inc in auto_inc_parameters: if auto_inc['IsModifiable'] and auto_inc['DataType'] == 'integer': print(f"The {auto_inc['ParameterName']} parameter is described as:") print(f"\t{auto_inc['Description']}") param_range = auto_inc['AllowedValues'].split('-') auto_inc['ParameterValue'] = str(q.ask( f"Enter a value between {param_range[0]} and {param_range[1]}: ", q.is_int, q.in_range(int(param_range[0]), int(param_range[1])))) update_params.append(auto_inc) self.aurora_wrapper.update_parameters(parameter_group_name, update_params) print("You can get a list of parameters you've set by specifying a source of 'user'.") user_parameters = self.aurora_wrapper.get_parameters(parameter_group_name, source='user') pp(user_parameters) print('-'*88) def create_cluster(self, cluster_name, db_engine, db_name, parameter_group): """ Shows how to create an Aurora DB cluster that contains a database of a specified type. The database is also configured to use a custom DB cluster parameter group. :param cluster_name: The name given to the newly created DB cluster. :param db_engine: The engine of the created database. :param db_name: The name given to the created database. :param parameter_group: The parameter group that is associated with the DB cluster. :return: The newly created DB cluster. """ print("Checking for an existing DB cluster.") cluster = self.aurora_wrapper.get_db_cluster(cluster_name) if cluster is None: admin_username = q.ask( "Enter an administrator user name for the database: ", q.non_empty) admin_password = q.ask( "Enter a password for the administrator (at least 8 characters): ", q.non_empty) engine_versions = self.aurora_wrapper.get_engine_versions( db_engine, parameter_group['DBParameterGroupFamily']) engine_choices = [ver['EngineVersion'] for ver in engine_versions] print("The available engines for your parameter group are:") engine_index = q.choose("Which engine do you want to use? ", engine_choices) print(f"Creating DB cluster {cluster_name} and database {db_name}.\n" f"The DB cluster is configured to use\n" f"your custom parameter group {parameter_group['DBClusterParameterGroupName']}\n" f"and selected engine {engine_choices[engine_index]}.\n" f"This typically takes several minutes.") cluster = self.aurora_wrapper.create_db_cluster( cluster_name, parameter_group['DBClusterParameterGroupName'], db_name, db_engine, engine_choices[engine_index], admin_username, admin_password) while cluster.get('Status') != 'available': wait(10) cluster = self.aurora_wrapper.get_db_cluster(cluster_name) print("Cluster created and available.\n") print("Cluster data:") pp(cluster) print('-'*88) return cluster def create_instance(self, cluster): """ Shows how to create a DB instance in an existing Aurora DB cluster. A new DB cluster contains no DB instances, so you must add one. The first DB instance that is added to a DB cluster defaults to a read-write DB instance. :param cluster: The DB cluster where the DB instance is added. :return: The newly created DB instance. """ print("Checking for an existing database instance.") cluster_name = cluster['DBClusterIdentifier'] db_inst = self.aurora_wrapper.get_db_instance(cluster_name) if db_inst is None: print("Let's create a database instance in your DB cluster.") print("First, choose a DB instance type:") inst_opts = self.aurora_wrapper.get_orderable_instances( cluster['Engine'], cluster['EngineVersion']) inst_choices = list({opt['DBInstanceClass'] for opt in inst_opts}) inst_index = q.choose("Which DB instance class do you want to use? ", inst_choices) print(f"Creating a database instance. This typically takes several minutes.") db_inst = self.aurora_wrapper.create_instance_in_cluster( cluster_name, cluster_name, cluster['Engine'], inst_choices[inst_index]) while db_inst.get('DBInstanceStatus') != 'available': wait(10) db_inst = self.aurora_wrapper.get_db_instance(cluster_name) print("Instance data:") pp(db_inst) print('-'*88) return db_inst @staticmethod def display_connection(cluster): """ Displays connection information about an Aurora DB cluster and tips on how to connect to it. :param cluster: The DB cluster to display. """ print("You can now connect to your database using your favorite MySql client.\n" "One way to connect is by using the 'mysql' shell on an Amazon EC2 instance\n" "that is running in the same VPC as your database cluster. Pass the endpoint,\n" "port, and administrator user name to 'mysql' and enter your password\n" "when prompted:\n") print(f"\n\tmysql -h {cluster['Endpoint']} -P {cluster['Port']} -u {cluster['MasterUsername']} -p\n") print("For more information, see the User Guide for Aurora:\n" "\thttps://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/CHAP_GettingStartedAurora.CreatingConnecting.Aurora.html#CHAP_GettingStartedAurora.Aurora.Connect") print('-'*88) def create_snapshot(self, cluster_name): """ Shows how to create a DB cluster snapshot and wait until it's available. :param cluster_name: The name of a DB cluster to snapshot. """ if q.ask("Do you want to create a snapshot of your DB cluster (y/n)? ", q.is_yesno): snapshot_id = f"{cluster_name}-{uuid.uuid4()}" print(f"Creating a snapshot named {snapshot_id}. This typically takes a few minutes.") snapshot = self.aurora_wrapper.create_cluster_snapshot(snapshot_id, cluster_name) while snapshot.get('Status') != 'available': wait(10) snapshot = self.aurora_wrapper.get_cluster_snapshot(snapshot_id) pp(snapshot) print('-'*88) def cleanup(self, db_inst, cluster, parameter_group): """ Shows how to clean up a DB instance, DB cluster, and DB cluster parameter group. Before the DB cluster parameter group can be deleted, all associated DB instances and DB clusters must first be deleted. :param db_inst: The DB instance to delete. :param cluster: The DB cluster to delete. :param parameter_group: The DB cluster parameter group to delete. """ cluster_name = cluster['DBClusterIdentifier'] parameter_group_name = parameter_group['DBClusterParameterGroupName'] if q.ask( "\nDo you want to delete the database instance, DB cluster, and parameter " "group (y/n)? ", q.is_yesno): print(f"Deleting database instance {db_inst['DBInstanceIdentifier']}.") self.aurora_wrapper.delete_db_instance(db_inst['DBInstanceIdentifier']) print(f"Deleting database cluster {cluster_name}.") self.aurora_wrapper.delete_db_cluster(cluster_name) print("Waiting for the DB instance and DB cluster to delete.\n" "This typically takes several minutes.") while db_inst is not None or cluster is not None: wait(10) if db_inst is not None: db_inst = self.aurora_wrapper.get_db_instance(db_inst['DBInstanceIdentifier']) if cluster is not None: cluster = self.aurora_wrapper.get_db_cluster(cluster['DBClusterIdentifier']) print(f"Deleting parameter group {parameter_group_name}.") self.aurora_wrapper.delete_parameter_group(parameter_group_name) def run_scenario(self, db_engine, parameter_group_name, cluster_name, db_name): print('-'*88) print("Welcome to the Amazon Relational Database Service (Amazon RDS) get started\n" "with Aurora DB clusters demo.") print('-'*88) parameter_group = self.create_parameter_group(db_engine, parameter_group_name) self.set_user_parameters(parameter_group_name) cluster = self.create_cluster(cluster_name, db_engine, db_name, parameter_group) db_inst = self.create_instance(cluster) self.display_connection(cluster) self.create_snapshot(cluster_name) self.cleanup(db_inst, cluster, parameter_group) print("\nThanks for watching!") print('-'*88) if __name__ == '__main__': logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s') try: scenario = AuroraClusterScenario(AuroraWrapper.from_client()) scenario.run_scenario( 'aurora-mysql', 'doc-example-cluster-parameter-group', 'doc-example-aurora', 'docexampledb') except Exception: logging.exception("Something went wrong with the demo.")

Define functions that are called by the scenario to manage Aurora actions.

class AuroraWrapper: """Encapsulates Aurora DB cluster actions.""" def __init__(self, rds_client): """ :param rds_client: A Boto3 Amazon Relational Database Service (Amazon RDS) client. """ self.rds_client = rds_client @classmethod def from_client(cls): """ Instantiates this class from a Boto3 client. """ rds_client = boto3.client('rds') return cls(rds_client) def get_parameter_group(self, parameter_group_name): """ Gets a DB cluster parameter group. :param parameter_group_name: The name of the parameter group to retrieve. :return: The requested parameter group. """ try: response = self.rds_client.describe_db_cluster_parameter_groups( DBClusterParameterGroupName=parameter_group_name) parameter_group = response['DBClusterParameterGroups'][0] except ClientError as err: if err.response['Error']['Code'] == 'DBParameterGroupNotFound': logger.info("Parameter group %s does not exist.", parameter_group_name) else: logger.error( "Couldn't get parameter group %s. Here's why: %s: %s", parameter_group_name, err.response['Error']['Code'], err.response['Error']['Message']) raise else: return parameter_group def create_parameter_group(self, parameter_group_name, parameter_group_family, description): """ Creates a DB cluster parameter group that is based on the specified parameter group family. :param parameter_group_name: The name of the newly created parameter group. :param parameter_group_family: The family that is used as the basis of the new parameter group. :param description: A description given to the parameter group. :return: Data about the newly created parameter group. """ try: response = self.rds_client.create_db_cluster_parameter_group( DBClusterParameterGroupName=parameter_group_name, DBParameterGroupFamily=parameter_group_family, Description=description) except ClientError as err: logger.error( "Couldn't create parameter group %s. Here's why: %s: %s", parameter_group_name, err.response['Error']['Code'], err.response['Error']['Message']) raise else: return response def delete_parameter_group(self, parameter_group_name): """ Deletes a DB cluster parameter group. :param parameter_group_name: The name of the parameter group to delete. :return: Data about the parameter group. """ try: response = self.rds_client.delete_db_cluster_parameter_group( DBClusterParameterGroupName=parameter_group_name) except ClientError as err: logger.error( "Couldn't delete parameter group %s. Here's why: %s: %s", parameter_group_name, err.response['Error']['Code'], err.response['Error']['Message']) raise else: return response def get_parameters(self, parameter_group_name, name_prefix='', source=None): """ Gets the parameters that are contained in a DB cluster parameter group. :param parameter_group_name: The name of the parameter group to query. :param name_prefix: When specified, the retrieved list of parameters is filtered to contain only parameters that start with this prefix. :param source: When specified, only parameters from this source are retrieved. For example, a source of 'user' retrieves only parameters that were set by a user. :return: The list of requested parameters. """ try: kwargs = {'DBClusterParameterGroupName': parameter_group_name} if source is not None: kwargs['Source'] = source parameters = [] paginator = self.rds_client.get_paginator('describe_db_cluster_parameters') for page in paginator.paginate(**kwargs): parameters += [ p for p in page['Parameters'] if p['ParameterName'].startswith(name_prefix)] except ClientError as err: logger.error( "Couldn't get parameters for %s. Here's why: %s: %s", parameter_group_name, err.response['Error']['Code'], err.response['Error']['Message']) raise else: return parameters def update_parameters(self, parameter_group_name, update_parameters): """ Updates parameters in a custom DB cluster parameter group. :param parameter_group_name: The name of the parameter group to update. :param update_parameters: The parameters to update in the group. :return: Data about the modified parameter group. """ try: response = self.rds_client.modify_db_cluster_parameter_group( DBClusterParameterGroupName=parameter_group_name, Parameters=update_parameters) except ClientError as err: logger.error( "Couldn't update parameters in %s. Here's why: %s: %s", parameter_group_name, err.response['Error']['Code'], err.response['Error']['Message']) raise else: return response def get_db_cluster(self, cluster_name): """ Gets data about an Aurora DB cluster. :param cluster_name: The name of the DB cluster to retrieve. :return: The retrieved DB cluster. """ try: response = self.rds_client.describe_db_clusters( DBClusterIdentifier=cluster_name) cluster = response['DBClusters'][0] except ClientError as err: if err.response['Error']['Code'] == 'DBClusterNotFoundFault': logger.info("Cluster %s does not exist.", cluster_name) else: logger.error( "Couldn't verify the existence of DB cluster %s. Here's why: %s: %s", cluster_name, err.response['Error']['Code'], err.response['Error']['Message']) raise else: return cluster def create_db_cluster( self, cluster_name, parameter_group_name, db_name, db_engine, db_engine_version, admin_name, admin_password): """ Creates a DB cluster that is configured to use the specified parameter group. The newly created DB cluster contains a database that uses the specified engine and engine version. :param cluster_name: The name of the DB cluster to create. :param parameter_group_name: The name of the parameter group to associate with the DB cluster. :param db_name: The name of the database to create. :param db_engine: The database engine of the database that is created, such as MySql. :param db_engine_version: The version of the database engine. :param admin_name: The user name of the database administrator. :param admin_password: The password of the database administrator. :return: The newly created DB cluster. """ try: response = self.rds_client.create_db_cluster( DatabaseName=db_name, DBClusterIdentifier=cluster_name, DBClusterParameterGroupName=parameter_group_name, Engine=db_engine, EngineVersion=db_engine_version, MasterUsername=admin_name, MasterUserPassword=admin_password) cluster = response['DBCluster'] except ClientError as err: logger.error( "Couldn't create database %s. Here's why: %s: %s", db_name, err.response['Error']['Code'], err.response['Error']['Message']) raise else: return cluster def delete_db_cluster(self, cluster_name): """ Deletes a DB cluster. :param cluster_name: The name of the DB cluster to delete. """ try: self.rds_client.delete_db_cluster( DBClusterIdentifier=cluster_name, SkipFinalSnapshot=True) logger.info("Deleted DB cluster %s.", cluster_name) except ClientError: logger.exception("Couldn't delete DB cluster %s.", cluster_name) raise def create_cluster_snapshot(self, snapshot_id, cluster_id): """ Creates a snapshot of a DB cluster. :param snapshot_id: The ID to give the created snapshot. :param cluster_id: The DB cluster to snapshot. :return: Data about the newly created snapshot. """ try: response = self.rds_client.create_db_cluster_snapshot( DBClusterSnapshotIdentifier=snapshot_id, DBClusterIdentifier=cluster_id) snapshot = response['DBClusterSnapshot'] except ClientError as err: logger.error( "Couldn't create snapshot of %s. Here's why: %s: %s", cluster_id, err.response['Error']['Code'], err.response['Error']['Message']) raise else: return snapshot def get_cluster_snapshot(self, snapshot_id): """ Gets a DB cluster snapshot. :param snapshot_id: The ID of the snapshot to retrieve. :return: The retrieved snapshot. """ try: response = self.rds_client.describe_db_cluster_snapshots( DBClusterSnapshotIdentifier=snapshot_id) snapshot = response['DBClusterSnapshots'][0] except ClientError as err: logger.error( "Couldn't get DB cluster snapshot %s. Here's why: %s: %s", snapshot_id, err.response['Error']['Code'], err.response['Error']['Message']) raise else: return snapshot def create_instance_in_cluster(self, instance_id, cluster_id, db_engine, instance_class): """ Creates a database instance in an existing DB cluster. The first database that is created defaults to a read-write DB instance. :param instance_id: The ID to give the newly created DB instance. :param cluster_id: The ID of the DB cluster where the DB instance is created. :param db_engine: The database engine of a database to create in the DB instance. This must be compatible with the configured parameter group of the DB cluster. :param instance_class: The DB instance class for the newly created DB instance. :return: Data about the newly created DB instance. """ try: response = self.rds_client.create_db_instance( DBInstanceIdentifier=instance_id, DBClusterIdentifier=cluster_id, Engine=db_engine, DBInstanceClass=instance_class) db_inst = response['DBInstance'] except ClientError as err: logger.error( "Couldn't create DB instance %s. Here's why: %s: %s", instance_id, err.response['Error']['Code'], err.response['Error']['Message']) raise else: return db_inst def get_engine_versions(self, engine, parameter_group_family=None): """ Gets database engine versions that are available for the specified engine and parameter group family. :param engine: The database engine to look up. :param parameter_group_family: When specified, restricts the returned list of engine versions to those that are compatible with this parameter group family. :return: The list of database engine versions. """ try: kwargs = {'Engine': engine} if parameter_group_family is not None: kwargs['DBParameterGroupFamily'] = parameter_group_family response = self.rds_client.describe_db_engine_versions(**kwargs) versions = response['DBEngineVersions'] except ClientError as err: logger.error( "Couldn't get engine versions for %s. Here's why: %s: %s", engine, err.response['Error']['Code'], err.response['Error']['Message']) raise else: return versions def get_orderable_instances(self, db_engine, db_engine_version): """ Gets DB instance options that can be used to create DB instances that are compatible with a set of specifications. :param db_engine: The database engine that must be supported by the DB instance. :param db_engine_version: The engine version that must be supported by the DB instance. :return: The list of DB instance options that can be used to create a compatible DB instance. """ try: inst_opts = [] paginator = self.rds_client.get_paginator('describe_orderable_db_instance_options') for page in paginator.paginate(Engine=db_engine, EngineVersion=db_engine_version): inst_opts += page['OrderableDBInstanceOptions'] except ClientError as err: logger.error( "Couldn't get orderable DB instances. Here's why: %s: %s", err.response['Error']['Code'], err.response['Error']['Message']) raise else: return inst_opts def get_db_instance(self, instance_id): """ Gets data about a DB instance. :param instance_id: The ID of the DB instance to retrieve. :return: The retrieved DB instance. """ try: response = self.rds_client.describe_db_instances( DBInstanceIdentifier=instance_id) db_inst = response['DBInstances'][0] except ClientError as err: if err.response['Error']['Code'] == 'DBInstanceNotFound': logger.info("Instance %s does not exist.", instance_id) else: logger.error( "Couldn't get DB instance %s. Here's why: %s: %s", instance_id, err.response['Error']['Code'], err.response['Error']['Message']) raise else: return db_inst def delete_db_instance(self, instance_id): """ Deletes a DB instance. :param instance_id: The ID of the DB instance to delete. :return: Data about the deleted DB instance. """ try: response = self.rds_client.delete_db_instance( DBInstanceIdentifier=instance_id, SkipFinalSnapshot=True, DeleteAutomatedBackups=True) db_inst = response['DBInstance'] except ClientError as err: logger.error( "Couldn't delete DB instance %s. Here's why: %s: %s", instance_id, err.response['Error']['Code'], err.response['Error']['Message']) raise else: return db_inst

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