Amazon RDS examples using Amazon SDK for .NET - Amazon SDK for .NET
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Amazon RDS examples using Amazon SDK for .NET

The following code examples show you how to perform actions and implement common scenarios by using the Amazon SDK for .NET with Amazon RDS.

Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios and cross-service examples.

Scenarios are code examples that show you how to accomplish a specific task by calling multiple functions within the same service.

Each example includes a link to GitHub, where you can find instructions on how to set up and run the code in context.

Get started

The following code examples show how to get started using Amazon RDS.

Amazon SDK for .NET
Note

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

using System; using System.Threading.Tasks; using Amazon.RDS; using Amazon.RDS.Model; namespace RDSActions; public static class HelloRds { static async Task Main(string[] args) { var rdsClient = new AmazonRDSClient(); Console.WriteLine($"Hello Amazon RDS! Following are some of your DB instances:"); Console.WriteLine(); // You can use await and any of the async methods to get a response. // Let's get the first twenty DB instances. var response = await rdsClient.DescribeDBInstancesAsync( new DescribeDBInstancesRequest() { MaxRecords = 20 // Must be between 20 and 100. }); foreach (var instance in response.DBInstances) { Console.WriteLine($"\tDB name: {instance.DBName}"); Console.WriteLine($"\tArn: {instance.DBInstanceArn}"); Console.WriteLine($"\tIdentifier: {instance.DBInstanceIdentifier}"); Console.WriteLine(); } } }

Actions

The following code example shows how to use CreateDBInstance.

Amazon SDK for .NET
Note

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

/// <summary> /// Create an 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="dbName">Name for the DB instance.</param> /// <param name="dbInstanceIdentifier">DB instance identifier.</param> /// <param name="parameterGroupName">DB parameter group to associate with the instance.</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> /// <param name="allocatedStorage">The amount of storage in gibibytes (GiB) to allocate to the DB instance.</param> /// <param name="adminName">Admin user name.</param> /// <param name="adminPassword">Admin user password.</param> /// <returns>DB instance object.</returns> public async Task<DBInstance> CreateDBInstance(string dbName, string dbInstanceIdentifier, string parameterGroupName, string dbEngine, string dbEngineVersion, string instanceClass, int allocatedStorage, string adminName, string adminPassword) { var response = await _amazonRDS.CreateDBInstanceAsync( new CreateDBInstanceRequest() { DBName = dbName, DBInstanceIdentifier = dbInstanceIdentifier, DBParameterGroupName = parameterGroupName, Engine = dbEngine, EngineVersion = dbEngineVersion, DBInstanceClass = instanceClass, AllocatedStorage = allocatedStorage, MasterUsername = adminName, MasterUserPassword = adminPassword }); return response.DBInstance; }

The following code example shows how to use CreateDBParameterGroup.

Amazon SDK for .NET
Note

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

/// <summary> /// Create a new DB parameter group. Use the action DescribeDBParameterGroupsAsync /// to determine when the DB parameter group is ready to use. /// </summary> /// <param name="name">Name of the DB parameter group.</param> /// <param name="family">Family of the DB parameter group.</param> /// <param name="description">Description of the DB parameter group.</param> /// <returns>The new DB parameter group.</returns> public async Task<DBParameterGroup> CreateDBParameterGroup( string name, string family, string description) { var response = await _amazonRDS.CreateDBParameterGroupAsync( new CreateDBParameterGroupRequest() { DBParameterGroupName = name, DBParameterGroupFamily = family, Description = description }); return response.DBParameterGroup; }

The following code example shows how to use CreateDBSnapshot.

Amazon SDK for .NET
Note

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

/// <summary> /// Create a snapshot of a DB instance. /// </summary> /// <param name="dbInstanceIdentifier">DB instance identifier.</param> /// <param name="snapshotIdentifier">Identifier for the snapshot.</param> /// <returns>DB snapshot object.</returns> public async Task<DBSnapshot> CreateDBSnapshot(string dbInstanceIdentifier, string snapshotIdentifier) { var response = await _amazonRDS.CreateDBSnapshotAsync( new CreateDBSnapshotRequest() { DBSnapshotIdentifier = snapshotIdentifier, DBInstanceIdentifier = dbInstanceIdentifier }); return response.DBSnapshot; }

The following code example shows how to use DeleteDBInstance.

Amazon SDK for .NET
Note

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

/// <summary> /// Delete a particular DB instance. /// </summary> /// <param name="dbInstanceIdentifier">DB instance identifier.</param> /// <returns>DB instance object.</returns> public async Task<DBInstance> DeleteDBInstance(string dbInstanceIdentifier) { var response = await _amazonRDS.DeleteDBInstanceAsync( new DeleteDBInstanceRequest() { DBInstanceIdentifier = dbInstanceIdentifier, SkipFinalSnapshot = true, DeleteAutomatedBackups = true }); return response.DBInstance; }

The following code example shows how to use DeleteDBParameterGroup.

Amazon SDK for .NET
Note

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

/// <summary> /// Delete a DB parameter group. The group cannot be a default DB parameter group /// or be associated with any DB instances. /// </summary> /// <param name="name">Name of the DB parameter group.</param> /// <returns>True if successful.</returns> public async Task<bool> DeleteDBParameterGroup(string name) { var response = await _amazonRDS.DeleteDBParameterGroupAsync( new DeleteDBParameterGroupRequest() { DBParameterGroupName = name, }); return response.HttpStatusCode == HttpStatusCode.OK; }

The following code example shows how to use DescribeDBEngineVersions.

Amazon SDK for .NET
Note

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

/// <summary> /// Get a list of DB engine versions for a particular DB engine. /// </summary> /// <param name="engine">Name of the engine.</param> /// <param name="dbParameterGroupFamily">Optional parameter group family name.</param> /// <returns>List of DBEngineVersions.</returns> public async Task<List<DBEngineVersion>> DescribeDBEngineVersions(string engine, string dbParameterGroupFamily = null) { var response = await _amazonRDS.DescribeDBEngineVersionsAsync( new DescribeDBEngineVersionsRequest() { Engine = engine, DBParameterGroupFamily = dbParameterGroupFamily }); return response.DBEngineVersions; }

The following code example shows how to use DescribeDBInstances.

Amazon SDK for .NET
Note

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

/// <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>> DescribeDBInstances(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; }

The following code example shows how to use DescribeDBParameterGroups.

Amazon SDK for .NET
Note

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

/// <summary> /// Get descriptions of DB parameter groups. /// </summary> /// <param name="name">Optional name of the DB parameter group to describe.</param> /// <returns>The list of DB parameter group descriptions.</returns> public async Task<List<DBParameterGroup>> DescribeDBParameterGroups(string name = null) { var response = await _amazonRDS.DescribeDBParameterGroupsAsync( new DescribeDBParameterGroupsRequest() { DBParameterGroupName = name }); return response.DBParameterGroups; }

The following code example shows how to use DescribeDBParameters.

Amazon SDK for .NET
Note

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

/// <summary> /// Get a list of DB parameters from a specific parameter group. /// </summary> /// <param name="dbParameterGroupName">Name of a specific DB parameter group.</param> /// <param name="source">Optional source for selecting parameters.</param> /// <returns>List of parameter values.</returns> public async Task<List<Parameter>> DescribeDBParameters(string dbParameterGroupName, string source = null) { var results = new List<Parameter>(); var paginateParameters = _amazonRDS.Paginators.DescribeDBParameters( new DescribeDBParametersRequest() { DBParameterGroupName = dbParameterGroupName, Source = source }); // Get the entire list using the paginator. await foreach (var parameters in paginateParameters.Parameters) { results.Add(parameters); } return results; }

The following code example shows how to use DescribeDBSnapshots.

Amazon SDK for .NET
Note

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

/// <summary> /// Return a list of DB snapshots for a particular DB instance. /// </summary> /// <param name="dbInstanceIdentifier">DB instance identifier.</param> /// <returns>List of DB snapshots.</returns> public async Task<List<DBSnapshot>> DescribeDBSnapshots(string dbInstanceIdentifier) { var results = new List<DBSnapshot>(); var snapshotsPaginator = _amazonRDS.Paginators.DescribeDBSnapshots( new DescribeDBSnapshotsRequest() { DBInstanceIdentifier = dbInstanceIdentifier }); // Get the entire list using the paginator. await foreach (var snapshots in snapshotsPaginator.DBSnapshots) { results.Add(snapshots); } return results; }

The following code example shows how to use DescribeOrderableDBInstanceOptions.

Amazon SDK for .NET
Note

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

/// <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>> DescribeOrderableDBInstanceOptions(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; }

The following code example shows how to use ModifyDBParameterGroup.

Amazon SDK for .NET
Note

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

/// <summary> /// Update a DB parameter group. Use the action DescribeDBParameterGroupsAsync /// to determine when the DB parameter group is ready to use. /// </summary> /// <param name="name">Name of the DB parameter group.</param> /// <param name="parameters">List of parameters. Maximum of 20 per request.</param> /// <returns>The updated DB parameter group name.</returns> public async Task<string> ModifyDBParameterGroup( string name, List<Parameter> parameters) { var response = await _amazonRDS.ModifyDBParameterGroupAsync( new ModifyDBParameterGroupRequest() { DBParameterGroupName = name, Parameters = parameters, }); return response.DBParameterGroupName; }

Scenarios

The following code example shows how to:

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

  • Create a DB instance that's configured to use the parameter group. The DB instance also contains a database.

  • Take a snapshot of the instance.

  • Delete the instance and parameter group.

Amazon SDK for .NET
Note

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

Run an interactive scenario at a command prompt.

/// <summary> /// Scenario for RDS DB instance example. /// </summary> public class RDSInstanceScenario { /* Before running this .NET code example, set up your development environment, including your credentials. This .NET example performs the following tasks: 1. Returns a list of the available DB engine families using the DescribeDBEngineVersionsAsync method. 2. Selects an engine family and creates a custom DB parameter group using the CreateDBParameterGroupAsync method. 3. Gets the parameter groups using the DescribeDBParameterGroupsAsync method. 4. Gets parameters in the group using the DescribeDBParameters method. 5. Parses and displays parameters in the group. 6. Modifies both the auto_increment_offset and auto_increment_increment parameters using the ModifyDBParameterGroupAsync method. 7. Gets and displays the updated parameters using the DescribeDBParameters method with a source of "user". 8. Gets a list of allowed engine versions using the DescribeDBEngineVersionsAsync method. 9. Displays and selects from a list of micro instance classes available for the selected engine and version. 10. Creates an RDS DB instance that contains a MySql database and uses the parameter group using the CreateDBInstanceAsync method. 11. Waits for DB instance to be ready using the DescribeDBInstancesAsync method. 12. Prints out the connection endpoint string for the new DB instance. 13. Creates a snapshot of the DB instance using the CreateDBSnapshotAsync method. 14. Waits for DB snapshot to be ready using the DescribeDBSnapshots method. 15. Deletes the DB instance using the DeleteDBInstanceAsync method. 16. Waits for DB instance to be deleted using the DescribeDbInstances method. 17. Deletes the parameter group using the DeleteDBParameterGroupAsync. */ private static readonly string sepBar = new('-', 80); private static RDSWrapper rdsWrapper = null!; private static ILogger logger = null!; private static readonly string engine = "mysql"; static async Task Main(string[] args) { // Set up dependency injection for the Amazon RDS service. 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<RDSWrapper>() ) .Build(); logger = LoggerFactory.Create(builder => { builder.AddConsole(); }).CreateLogger<RDSInstanceScenario>(); rdsWrapper = host.Services.GetRequiredService<RDSWrapper>(); Console.WriteLine(sepBar); Console.WriteLine( "Welcome to the Amazon Relational Database Service (Amazon RDS) DB instance scenario example."); Console.WriteLine(sepBar); try { var parameterGroupFamily = await ChooseParameterGroupFamily(); var parameterGroup = await CreateDbParameterGroup(parameterGroupFamily); var parameters = await DescribeParametersInGroup(parameterGroup.DBParameterGroupName, new List<string> { "auto_increment_offset", "auto_increment_increment" }); await ModifyParameters(parameterGroup.DBParameterGroupName, parameters); await DescribeUserSourceParameters(parameterGroup.DBParameterGroupName); var engineVersionChoice = await ChooseDbEngineVersion(parameterGroupFamily); var instanceChoice = await ChooseDbInstanceClass(engine, engineVersionChoice.EngineVersion); var newInstanceIdentifier = "Example-Instance-" + DateTime.Now.Ticks; var newInstance = await CreateRdsNewInstance(parameterGroup, engine, engineVersionChoice.EngineVersion, instanceChoice.DBInstanceClass, newInstanceIdentifier); if (newInstance != null) { DisplayConnectionString(newInstance); await CreateSnapshot(newInstance); await DeleteRdsInstance(newInstance); } await DeleteParameterGroup(parameterGroup); Console.WriteLine("Scenario complete."); Console.WriteLine(sepBar); } catch (Exception ex) { logger.LogError(ex, "There was a problem executing the scenario."); } } /// <summary> /// Choose the RDS DB parameter group family from a list of available options. /// </summary> /// <returns>The selected parameter group family.</returns> public static async Task<string> ChooseParameterGroupFamily() { Console.WriteLine(sepBar); // 1. Get a list of available engines. var engines = await rdsWrapper.DescribeDBEngineVersions(engine); Console.WriteLine("1. The following is a list of available DB parameter group families:"); int i = 1; var parameterGroupFamilies = engines.GroupBy(e => e.DBParameterGroupFamily).ToList(); foreach (var parameterGroupFamily in parameterGroupFamilies) { // List the available parameter group families. Console.WriteLine( $"\t{i}. Family: {parameterGroupFamily.Key}"); i++; } var choiceNumber = 0; while (choiceNumber < 1 || choiceNumber > parameterGroupFamilies.Count) { Console.WriteLine("Select an available DB parameter group family by entering a number from the list above:"); 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<DBParameterGroup> CreateDbParameterGroup(string dbParameterGroupFamily) { Console.WriteLine(sepBar); Console.WriteLine($"2. Create new DB parameter group with family {dbParameterGroupFamily}:"); var parameterGroup = await rdsWrapper.CreateDBParameterGroup( "ExampleParameterGroup-" + DateTime.Now.Ticks, dbParameterGroupFamily, "New example parameter group"); var groupInfo = await rdsWrapper.DescribeDBParameterGroups(parameterGroup .DBParameterGroupName); Console.WriteLine( $"3. New DB parameter group: \n\t{groupInfo[0].Description}, \n\tARN {groupInfo[0].DBParameterGroupArn}"); Console.WriteLine(sepBar); return parameterGroup; } /// <summary> /// Get and describe parameters from a DBParameterGroup. /// </summary> /// <param name="parameterGroupName">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>> DescribeParametersInGroup(string parameterGroupName, List<string>? parameterNames = null) { Console.WriteLine(sepBar); Console.WriteLine("4. Get some parameters from the group."); Console.WriteLine(sepBar); var parameters = await rdsWrapper.DescribeDBParameters(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 ModifyParameters(string parameterGroupName, List<Parameter> parameters) { Console.WriteLine(sepBar); Console.WriteLine("6. Modify some parameters in the group."); foreach (var p in parameters) { if (p.IsModifiable && p.DataType == "integer") { int newValue = 0; while (newValue == 0) { Console.WriteLine( $"Enter a new value for {p.ParameterName} from the allowed values {p.AllowedValues} "); var choice = Console.ReadLine(); Int32.TryParse(choice, out newValue); } p.ParameterValue = newValue.ToString(); } } await rdsWrapper.ModifyDBParameterGroup(parameterGroupName, parameters); Console.WriteLine(sepBar); } /// <summary> /// Describe the user source parameters in the group. /// </summary> /// <param name="parameterGroupName">Name of the DBParameterGroup.</param> /// <returns>Async task.</returns> public static async Task DescribeUserSourceParameters(string parameterGroupName) { Console.WriteLine(sepBar); Console.WriteLine("7. Describe user source parameters in the group."); var parameters = await rdsWrapper.DescribeDBParameters(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> ChooseDbEngineVersion(string dbParameterGroupFamily) { Console.WriteLine(sepBar); // Get a list of allowed engines. var allowedEngines = await rdsWrapper.DescribeDBEngineVersions(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> /// 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 rdsWrapper.DescribeOrderableDBInstanceOptions(engine, engineVersion); Console.WriteLine($"8. Available micro DB instance classes for engine {engine} and version {engineVersion}:"); int i = 1; // Filter to micro instances for this example. allowedInstances = allowedInstances .Where(i => i.DBInstanceClass.Contains("micro")).ToList(); 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("9. Select an available DB instance class by entering a number from the list above:"); var choice = Console.ReadLine(); Int32.TryParse(choice, out choiceNumber); } var instanceChoice = allowedInstances[choiceNumber - 1]; Console.WriteLine(sepBar); return instanceChoice; } /// <summary> /// Create a new RDS DB instance. /// </summary> /// <param name="parameterGroup">Parameter group to use for the DB instance.</param> /// <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?> CreateRdsNewInstance(DBParameterGroup parameterGroup, string engineName, string engineVersion, string instanceClass, string instanceIdentifier) { Console.WriteLine(sepBar); Console.WriteLine($"10. Create a new DB instance with identifier {instanceIdentifier}."); bool isInstanceReady = false; DBInstance newInstance; var instances = await rdsWrapper.DescribeDBInstances(); isInstanceReady = instances.FirstOrDefault(i => i.DBInstanceIdentifier == instanceIdentifier)?.DBInstanceStatus == "available"; if (isInstanceReady) { Console.WriteLine("Instance already created."); newInstance = instances.First(i => i.DBInstanceIdentifier == instanceIdentifier); } else { Console.WriteLine("Please enter an admin user name:"); var username = Console.ReadLine(); Console.WriteLine("Please enter an admin password:"); var password = Console.ReadLine(); newInstance = await rdsWrapper.CreateDBInstance( "ExampleInstance", instanceIdentifier, parameterGroup.DBParameterGroupName, engineName, engineVersion, instanceClass, 20, username, password ); // 11. Wait for the DB instance to be ready. Console.WriteLine("11. Waiting for DB instance to be ready..."); while (!isInstanceReady) { instances = await rdsWrapper.DescribeDBInstances(instanceIdentifier); isInstanceReady = instances.FirstOrDefault()?.DBInstanceStatus == "available"; newInstance = instances.First(); Thread.Sleep(30000); } } Console.WriteLine(sepBar); return newInstance; } /// <summary> /// Display a connection string for an RDS DB instance. /// </summary> /// <param name="instance">The DB instance to use to get a connection string.</param> public static void DisplayConnectionString(DBInstance instance) { Console.WriteLine(sepBar); // Display the connection string. Console.WriteLine("12. New DB instance connection string: "); Console.WriteLine( $"\n{engine} -h {instance.Endpoint.Address} -P {instance.Endpoint.Port} " + $"-u {instance.MasterUsername} -p [YOUR PASSWORD]\n"); Console.WriteLine(sepBar); } /// <summary> /// Create a snapshot from an RDS DB instance. /// </summary> /// <param name="instance">DB instance to use when creating a snapshot.</param> /// <returns>The snapshot object.</returns> public static async Task<DBSnapshot> CreateSnapshot(DBInstance instance) { Console.WriteLine(sepBar); // Create a snapshot. Console.WriteLine($"13. Creating snapshot from DB instance {instance.DBInstanceIdentifier}."); var snapshot = await rdsWrapper.CreateDBSnapshot(instance.DBInstanceIdentifier, "ExampleSnapshot-" + DateTime.Now.Ticks); // Wait for the snapshot to be available bool isSnapshotReady = false; Console.WriteLine($"14. Waiting for snapshot to be ready..."); while (!isSnapshotReady) { var snapshots = await rdsWrapper.DescribeDBSnapshots(instance.DBInstanceIdentifier); isSnapshotReady = snapshots.FirstOrDefault()?.Status == "available"; snapshot = snapshots.First(); Thread.Sleep(30000); } Console.WriteLine( $"Snapshot {snapshot.DBSnapshotIdentifier} status is {snapshot.Status}."); Console.WriteLine(sepBar); return snapshot; } /// <summary> /// Delete an RDS DB instance. /// </summary> /// <param name="instance">The DB instance to delete.</param> /// <returns>Async task.</returns> public static async Task DeleteRdsInstance(DBInstance newInstance) { Console.WriteLine(sepBar); // Delete the DB instance. Console.WriteLine($"15. Delete the DB instance {newInstance.DBInstanceIdentifier}."); await rdsWrapper.DeleteDBInstance(newInstance.DBInstanceIdentifier); // Wait for the DB instance to delete. Console.WriteLine($"16. Waiting for the DB instance to delete..."); bool isInstanceDeleted = false; while (!isInstanceDeleted) { var instance = await rdsWrapper.DescribeDBInstances(); isInstanceDeleted = instance.All(i => i.DBInstanceIdentifier != newInstance.DBInstanceIdentifier); Thread.Sleep(30000); } Console.WriteLine("DB instance deleted."); Console.WriteLine(sepBar); } /// <summary> /// Delete a DB parameter group. /// </summary> /// <param name="parameterGroup">The parameter group to delete.</param> /// <returns>Async task.</returns> public static async Task DeleteParameterGroup(DBParameterGroup parameterGroup) { Console.WriteLine(sepBar); // Delete the parameter group. Console.WriteLine($"17. Delete the DB parameter group {parameterGroup.DBParameterGroupName}."); await rdsWrapper.DeleteDBParameterGroup(parameterGroup.DBParameterGroupName); Console.WriteLine(sepBar); }

Wrapper methods used by the scenario for DB instance actions.

/// <summary> /// Wrapper methods to use Amazon Relational Database Service (Amazon RDS) with DB instance operations. /// </summary> public partial class RDSWrapper { private readonly IAmazonRDS _amazonRDS; public RDSWrapper(IAmazonRDS amazonRDS) { _amazonRDS = amazonRDS; } /// <summary> /// Get a list of DB engine versions for a particular DB engine. /// </summary> /// <param name="engine">Name of the engine.</param> /// <param name="dbParameterGroupFamily">Optional parameter group family name.</param> /// <returns>List of DBEngineVersions.</returns> public async Task<List<DBEngineVersion>> DescribeDBEngineVersions(string engine, string dbParameterGroupFamily = null) { var response = await _amazonRDS.DescribeDBEngineVersionsAsync( new DescribeDBEngineVersionsRequest() { Engine = engine, DBParameterGroupFamily = dbParameterGroupFamily }); return response.DBEngineVersions; } /// <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>> DescribeOrderableDBInstanceOptions(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> /// 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>> DescribeDBInstances(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> /// Create an 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="dbName">Name for the DB instance.</param> /// <param name="dbInstanceIdentifier">DB instance identifier.</param> /// <param name="parameterGroupName">DB parameter group to associate with the instance.</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> /// <param name="allocatedStorage">The amount of storage in gibibytes (GiB) to allocate to the DB instance.</param> /// <param name="adminName">Admin user name.</param> /// <param name="adminPassword">Admin user password.</param> /// <returns>DB instance object.</returns> public async Task<DBInstance> CreateDBInstance(string dbName, string dbInstanceIdentifier, string parameterGroupName, string dbEngine, string dbEngineVersion, string instanceClass, int allocatedStorage, string adminName, string adminPassword) { var response = await _amazonRDS.CreateDBInstanceAsync( new CreateDBInstanceRequest() { DBName = dbName, DBInstanceIdentifier = dbInstanceIdentifier, DBParameterGroupName = parameterGroupName, Engine = dbEngine, EngineVersion = dbEngineVersion, DBInstanceClass = instanceClass, AllocatedStorage = allocatedStorage, MasterUsername = adminName, MasterUserPassword = adminPassword }); return response.DBInstance; } /// <summary> /// Delete a particular DB instance. /// </summary> /// <param name="dbInstanceIdentifier">DB instance identifier.</param> /// <returns>DB instance object.</returns> public async Task<DBInstance> DeleteDBInstance(string dbInstanceIdentifier) { var response = await _amazonRDS.DeleteDBInstanceAsync( new DeleteDBInstanceRequest() { DBInstanceIdentifier = dbInstanceIdentifier, SkipFinalSnapshot = true, DeleteAutomatedBackups = true }); return response.DBInstance; }

Wrapper methods used by the scenario for DB parameter groups.

/// <summary> /// Wrapper methods to use Amazon Relational Database Service (Amazon RDS) with parameter groups. /// </summary> public partial class RDSWrapper { /// <summary> /// Get descriptions of DB parameter groups. /// </summary> /// <param name="name">Optional name of the DB parameter group to describe.</param> /// <returns>The list of DB parameter group descriptions.</returns> public async Task<List<DBParameterGroup>> DescribeDBParameterGroups(string name = null) { var response = await _amazonRDS.DescribeDBParameterGroupsAsync( new DescribeDBParameterGroupsRequest() { DBParameterGroupName = name }); return response.DBParameterGroups; } /// <summary> /// Create a new DB parameter group. Use the action DescribeDBParameterGroupsAsync /// to determine when the DB parameter group is ready to use. /// </summary> /// <param name="name">Name of the DB parameter group.</param> /// <param name="family">Family of the DB parameter group.</param> /// <param name="description">Description of the DB parameter group.</param> /// <returns>The new DB parameter group.</returns> public async Task<DBParameterGroup> CreateDBParameterGroup( string name, string family, string description) { var response = await _amazonRDS.CreateDBParameterGroupAsync( new CreateDBParameterGroupRequest() { DBParameterGroupName = name, DBParameterGroupFamily = family, Description = description }); return response.DBParameterGroup; } /// <summary> /// Update a DB parameter group. Use the action DescribeDBParameterGroupsAsync /// to determine when the DB parameter group is ready to use. /// </summary> /// <param name="name">Name of the DB parameter group.</param> /// <param name="parameters">List of parameters. Maximum of 20 per request.</param> /// <returns>The updated DB parameter group name.</returns> public async Task<string> ModifyDBParameterGroup( string name, List<Parameter> parameters) { var response = await _amazonRDS.ModifyDBParameterGroupAsync( new ModifyDBParameterGroupRequest() { DBParameterGroupName = name, Parameters = parameters, }); return response.DBParameterGroupName; } /// <summary> /// Delete a DB parameter group. The group cannot be a default DB parameter group /// or be associated with any DB instances. /// </summary> /// <param name="name">Name of the DB parameter group.</param> /// <returns>True if successful.</returns> public async Task<bool> DeleteDBParameterGroup(string name) { var response = await _amazonRDS.DeleteDBParameterGroupAsync( new DeleteDBParameterGroupRequest() { DBParameterGroupName = name, }); return response.HttpStatusCode == HttpStatusCode.OK; } /// <summary> /// Get a list of DB parameters from a specific parameter group. /// </summary> /// <param name="dbParameterGroupName">Name of a specific DB parameter group.</param> /// <param name="source">Optional source for selecting parameters.</param> /// <returns>List of parameter values.</returns> public async Task<List<Parameter>> DescribeDBParameters(string dbParameterGroupName, string source = null) { var results = new List<Parameter>(); var paginateParameters = _amazonRDS.Paginators.DescribeDBParameters( new DescribeDBParametersRequest() { DBParameterGroupName = dbParameterGroupName, Source = source }); // Get the entire list using the paginator. await foreach (var parameters in paginateParameters.Parameters) { results.Add(parameters); } return results; }

Wrapper methods used by the scenario for DB snapshot actions.

/// <summary> /// Wrapper methods to use Amazon Relational Database Service (Amazon RDS) with snapshots. /// </summary> public partial class RDSWrapper { /// <summary> /// Create a snapshot of a DB instance. /// </summary> /// <param name="dbInstanceIdentifier">DB instance identifier.</param> /// <param name="snapshotIdentifier">Identifier for the snapshot.</param> /// <returns>DB snapshot object.</returns> public async Task<DBSnapshot> CreateDBSnapshot(string dbInstanceIdentifier, string snapshotIdentifier) { var response = await _amazonRDS.CreateDBSnapshotAsync( new CreateDBSnapshotRequest() { DBSnapshotIdentifier = snapshotIdentifier, DBInstanceIdentifier = dbInstanceIdentifier }); return response.DBSnapshot; } /// <summary> /// Return a list of DB snapshots for a particular DB instance. /// </summary> /// <param name="dbInstanceIdentifier">DB instance identifier.</param> /// <returns>List of DB snapshots.</returns> public async Task<List<DBSnapshot>> DescribeDBSnapshots(string dbInstanceIdentifier) { var results = new List<DBSnapshot>(); var snapshotsPaginator = _amazonRDS.Paginators.DescribeDBSnapshots( new DescribeDBSnapshotsRequest() { DBInstanceIdentifier = dbInstanceIdentifier }); // Get the entire list using the paginator. await foreach (var snapshots in snapshotsPaginator.DBSnapshots) { results.Add(snapshots); } return results; }