Scopri le nozioni di base di Amazon RDS con un AWS SDK - Amazon Relational Database Service

Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.

Scopri le nozioni di base di Amazon RDS con un AWS SDK

Gli esempi di codice seguenti mostrano come:

  • Creare un gruppo di parametri database personalizzati e imposta i relativi valori.

  • Creare un'istanza database configurata per utilizzare il gruppo di parametri. L'istanza DB contiene anche un database.

  • Acquisire uno snapshot dell'istanza.

  • Eliminare l'istanza e il gruppo di parametri.

.NET
AWS SDK for .NET
Nota

C'è altro su. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS.

Esegui uno scenario interattivo al prompt dei comandi.

/// <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); }

Metodi wrapper utilizzati dallo scenario per operazioni delle istanze DB.

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

Metodi wrapper utilizzati dallo scenario per gruppi di parametri database.

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

Metodi wrapper utilizzati dallo scenario per operazioni degli snapshot database.

/// <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; }
C++
SDKper C++
Nota

C'è di più su. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS.

Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; //! Routine which creates an Amazon RDS instance and demonstrates several operations //! on that instance. /*! \sa gettingStartedWithDBInstances() \param clientConfiguration: AWS client configuration. \return bool: Successful completion. */ bool AwsDoc::RDS::gettingStartedWithDBInstances( const Aws::Client::ClientConfiguration &clientConfig) { Aws::RDS::RDSClient client(clientConfig); printAsterisksLine(); std::cout << "Welcome to the Amazon Relational Database Service (Amazon RDS)" << std::endl; std::cout << "get started with DB instances demo." << std::endl; printAsterisksLine(); std::cout << "Checking for an existing DB parameter group named '" << PARAMETER_GROUP_NAME << "'." << std::endl; Aws::String dbParameterGroupFamily("Undefined"); bool parameterGroupFound = true; { // 1. Check if the DB parameter group already exists. Aws::RDS::Model::DescribeDBParameterGroupsRequest request; request.SetDBParameterGroupName(PARAMETER_GROUP_NAME); Aws::RDS::Model::DescribeDBParameterGroupsOutcome outcome = client.DescribeDBParameterGroups(request); if (outcome.IsSuccess()) { std::cout << "DB parameter group named '" << PARAMETER_GROUP_NAME << "' already exists." << std::endl; dbParameterGroupFamily = outcome.GetResult().GetDBParameterGroups()[0].GetDBParameterGroupFamily(); } else if (outcome.GetError().GetErrorType() == Aws::RDS::RDSErrors::D_B_PARAMETER_GROUP_NOT_FOUND_FAULT) { std::cout << "DB parameter group named '" << PARAMETER_GROUP_NAME << "' does not exist." << std::endl; parameterGroupFound = false; } else { std::cerr << "Error with RDS::DescribeDBParameterGroups. " << outcome.GetError().GetMessage() << std::endl; return false; } } if (!parameterGroupFound) { Aws::Vector<Aws::RDS::Model::DBEngineVersion> engineVersions; // 2. Get available engine versions for the specified engine. if (!getDBEngineVersions(DB_ENGINE, NO_PARAMETER_GROUP_FAMILY, engineVersions, client)) { return false; } std::cout << "Getting available database engine versions 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 parameter group. Aws::RDS::Model::CreateDBParameterGroupRequest request; request.SetDBParameterGroupName(PARAMETER_GROUP_NAME); request.SetDBParameterGroupFamily(dbParameterGroupFamily); request.SetDescription("Example parameter group."); Aws::RDS::Model::CreateDBParameterGroupOutcome outcome = client.CreateDBParameterGroup(request); if (outcome.IsSuccess()) { std::cout << "The DB parameter group was successfully created." << std::endl; } else { std::cerr << "Error with RDS::CreateDBParameterGroup. " << outcome.GetError().GetMessage() << std::endl; return false; } } printAsterisksLine(); std::cout << "Let's set some parameter values in your parameter group." << std::endl; Aws::String marker; Aws::Vector<Aws::RDS::Model::Parameter> autoIncrementParameters; // 4. Get the parameters in the DB parameter group. if (!getDBParameters(PARAMETER_GROUP_NAME, AUTO_INCREMENT_PREFIX, NO_SOURCE, autoIncrementParameters, client)) { cleanUpResources(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 in the range ") + 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 group. Aws::RDS::Model::ModifyDBParameterGroupRequest request; request.SetDBParameterGroupName(PARAMETER_GROUP_NAME); request.SetParameters(updateParameters); Aws::RDS::Model::ModifyDBParameterGroupOutcome outcome = client.ModifyDBParameterGroup(request); if (outcome.IsSuccess()) { std::cout << "The DB parameter group was successfully modified." << std::endl; } else { std::cerr << "Error with RDS::ModifyDBParameterGroup. " << 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 group. if (!getDBParameters(PARAMETER_GROUP_NAME, NO_NAME_PREFIX, "user", userParameters, client)) { cleanUpResources(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 instance." << std::endl; Aws::RDS::Model::DBInstance dbInstance; // 7. Check if the DB instance already exists. if (!describeDBInstance(DB_INSTANCE_IDENTIFIER, dbInstance, client)) { cleanUpResources(PARAMETER_GROUP_NAME, "", 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; 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 available engine versions. if (!getDBEngineVersions(DB_ENGINE, dbParameterGroupFamily, engineVersions, client)) { cleanUpResources(PARAMETER_GROUP_NAME, "", client); return false; } std::cout << "The available engines for your parameter group 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]; Aws::String dbInstanceClass; // 9. Get a list of micro instance classes. if (!chooseMicroDBInstanceClass(engineVersion.GetEngine(), engineVersion.GetEngineVersion(), dbInstanceClass, client)) { cleanUpResources(PARAMETER_GROUP_NAME, "", client); return false; } std::cout << "Creating a DB instance named '" << DB_INSTANCE_IDENTIFIER << "' and database '" << DB_NAME << "'.\n" << "The DB instance is configured to use your custom parameter group '" << PARAMETER_GROUP_NAME << "',\n" << "selected engine version " << engineVersion.GetEngineVersion() << ",\n" << "selected DB instance class '" << dbInstanceClass << "'," << " and " << DB_ALLOCATED_STORAGE << " GiB of " << DB_STORAGE_TYPE << " storage.\nThis typically takes several minutes." << std::endl; Aws::RDS::Model::CreateDBInstanceRequest request; request.SetDBName(DB_NAME); request.SetDBInstanceIdentifier(DB_INSTANCE_IDENTIFIER); request.SetDBParameterGroupName(PARAMETER_GROUP_NAME); request.SetEngine(engineVersion.GetEngine()); request.SetEngineVersion(engineVersion.GetEngineVersion()); request.SetDBInstanceClass(dbInstanceClass); request.SetStorageType(DB_STORAGE_TYPE); request.SetAllocatedStorage(DB_ALLOCATED_STORAGE); request.SetMasterUsername(administratorName); request.SetMasterUserPassword(administratorPassword); 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(PARAMETER_GROUP_NAME, "", client); return false; } } std::cout << "Waiting for the DB instance to become available." << std::endl; int counter = 0; // 11. 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(PARAMETER_GROUP_NAME, DB_INSTANCE_IDENTIFIER, client); return false; } dbInstance = Aws::RDS::Model::DBInstance(); if (!describeDBInstance(DB_INSTANCE_IDENTIFIER, dbInstance, client)) { cleanUpResources(PARAMETER_GROUP_NAME, 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; } printAsterisksLine(); // 12. Display the connection string that can be used to connect a 'mysql' shell to the database. displayConnection(dbInstance); printAsterisksLine(); if (askYesNoQuestion( "Do you want to create a snapshot of your DB instance (y/n)? ")) { Aws::String snapshotID(DB_INSTANCE_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; // 13. Create a snapshot of the DB instance. Aws::RDS::Model::CreateDBSnapshotRequest request; request.SetDBInstanceIdentifier(DB_INSTANCE_IDENTIFIER); request.SetDBSnapshotIdentifier(snapshotID); Aws::RDS::Model::CreateDBSnapshotOutcome outcome = client.CreateDBSnapshot(request); if (outcome.IsSuccess()) { std::cout << "Snapshot creation has started." << std::endl; } else { std::cerr << "Error with RDS::CreateDBSnapshot. " << outcome.GetError().GetMessage() << std::endl; cleanUpResources(PARAMETER_GROUP_NAME, DB_INSTANCE_IDENTIFIER, client); return false; } } std::cout << "Waiting for snapshot to become available." << std::endl; Aws::RDS::Model::DBSnapshot 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(PARAMETER_GROUP_NAME, DB_INSTANCE_IDENTIFIER, client); return false; } // 14. Wait for the snapshot to become available. Aws::RDS::Model::DescribeDBSnapshotsRequest request; request.SetDBSnapshotIdentifier(snapshotID); Aws::RDS::Model::DescribeDBSnapshotsOutcome outcome = client.DescribeDBSnapshots(request); if (outcome.IsSuccess()) { snapshot = outcome.GetResult().GetDBSnapshots()[0]; } else { std::cerr << "Error with RDS::DescribeDBSnapshots. " << outcome.GetError().GetMessage() << std::endl; cleanUpResources(PARAMETER_GROUP_NAME, 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 instance and parameter group (y/n)? ")) { result = cleanUpResources(PARAMETER_GROUP_NAME, DB_INSTANCE_IDENTIFIER, client); } return result; } //! Routine which gets DB parameters using the 'DescribeDBParameters' api. /*! \sa getDBParameters() \param parameterGroupName: The name of the 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::RDS::getDBParameters(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; do { Aws::RDS::Model::DescribeDBParametersRequest request; request.SetDBParameterGroupName(PARAMETER_GROUP_NAME); if (!marker.empty()) { request.SetMarker(marker); } if (!source.empty()) { request.SetSource(source); } Aws::RDS::Model::DescribeDBParametersOutcome outcome = client.DescribeDBParameters(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 RDS::DescribeDBParameters. " << 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::RDS::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); } engineVersionsResult.clear(); Aws::String marker; // Used for pagination. do { if (!marker.empty()) { request.SetMarker(marker); } Aws::RDS::Model::DescribeDBEngineVersionsOutcome outcome = client.DescribeDBEngineVersions(request); if (outcome.IsSuccess()) { auto &engineVersions = outcome.GetResult().GetDBEngineVersions(); engineVersionsResult.insert(engineVersionsResult.end(), engineVersions.begin(), engineVersions.end()); marker = outcome.GetResult().GetMarker(); } else { std::cerr << "Error with RDS::DescribeDBEngineVersionsRequest. " << outcome.GetError().GetMessage() << std::endl; return false; } } while (!marker.empty()); return true; } //! Routine which gets a DB instance description. /*! \sa describeDBInstance() \param dbInstanceIdentifier: A DB instance identifier. \param instanceResult: The 'DBInstance' object containing the description. \param client: 'RDSClient' instance. \return bool: Successful completion. */ bool AwsDoc::RDS::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]; } else if (outcome.GetError().GetErrorType() != Aws::RDS::RDSErrors::D_B_INSTANCE_NOT_FOUND_FAULT) { result = false; std::cerr << "Error with RDS::DescribeDBInstances. " << outcome.GetError().GetMessage() << std::endl; } // This example does not log an error if the DB instance does not exist. // Instead, instanceResult is set to empty. else { instanceResult = Aws::RDS::Model::DBInstance(); } return result; } //! Routine which gets available 'micro' DB instance classes, displays the list //! to the user, and returns the user selection. /*! \sa chooseMicroDBInstanceClass() \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::RDS::chooseMicroDBInstanceClass(const Aws::String &engine, const Aws::String &engineVersion, Aws::String &dbInstanceClass, const Aws::RDS::RDSClient &client) { std::vector<Aws::String> instanceClasses; Aws::String marker; 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(); if (instanceClass.find("micro") != std::string::npos) { if (std::find(instanceClasses.begin(), instanceClasses.end(), instanceClass) == instanceClasses.end()) { instanceClasses.push_back(instanceClass); } } } marker = outcome.GetResult().GetMarker(); } else { std::cerr << "Error with RDS::DescribeOrderableDBInstanceOptions. " << outcome.GetError().GetMessage() << std::endl; return false; } } while (!marker.empty()); std::cout << "The available micro 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 micro 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::RDS::cleanUpResources(const Aws::String &parameterGroupName, const Aws::String &dbInstanceIdentifier, const Aws::RDS::RDSClient &client) { bool result = true; if (!dbInstanceIdentifier.empty()) { { // 15. 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; } else { std::cerr << "Error with RDS::DeleteDBInstance. " << outcome.GetError().GetMessage() << std::endl; result = false; } } std::cout << "Waiting for DB instance to delete before deleting the parameter group." << std::endl; std::cout << "This may take a while." << std::endl; int counter = 0; Aws::RDS::Model::DBInstance dbInstance; do { 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; } dbInstance = Aws::RDS::Model::DBInstance(); // 16. Wait for the DB instance to be deleted. if (!describeDBInstance(dbInstanceIdentifier, dbInstance, client)) { return false; } if (dbInstance.DBInstanceIdentifierHasBeenSet() && (counter % 20) == 0) { std::cout << "Current DB instance status is '" << dbInstance.GetDBInstanceStatus() << "' after " << counter << " seconds." << std::endl; } } while (dbInstance.DBInstanceIdentifierHasBeenSet()); } if (!parameterGroupName.empty()) { // 17. Delete the parameter group. Aws::RDS::Model::DeleteDBParameterGroupRequest request; request.SetDBParameterGroupName(parameterGroupName); Aws::RDS::Model::DeleteDBParameterGroupOutcome outcome = client.DeleteDBParameterGroup(request); if (outcome.IsSuccess()) { std::cout << "The DB parameter group was successfully deleted." << std::endl; } else { std::cerr << "Error with RDS::DeleteDBParameterGroup. " << outcome.GetError().GetMessage() << std::endl; result = false; } } return result; }
Go
SDKper Go V2
Nota

C'è altro da fare. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS.

Esegui uno scenario interattivo al prompt dei comandi.

// GetStartedInstances is an interactive example that shows you how to use the AWS SDK for Go // with Amazon Relation Database Service (Amazon RDS) to do the following: // // 1. Create a custom DB parameter group and set parameter values. // 2. Create a DB instance that is configured to use the parameter group. The DB instance // also contains a database. // 3. Take a snapshot of the DB instance. // 4. Delete the DB instance and parameter group. type GetStartedInstances struct { sdkConfig aws.Config instances actions.DbInstances questioner demotools.IQuestioner helper IScenarioHelper isTestRun bool } // NewGetStartedInstances constructs a GetStartedInstances instance from a configuration. // It uses the specified config to get an Amazon RDS // client and create wrappers for the actions used in the scenario. func NewGetStartedInstances(sdkConfig aws.Config, questioner demotools.IQuestioner, helper IScenarioHelper) GetStartedInstances { rdsClient := rds.NewFromConfig(sdkConfig) return GetStartedInstances{ sdkConfig: sdkConfig, instances: actions.DbInstances{RdsClient: rdsClient}, questioner: questioner, helper: helper, } } // Run runs the interactive scenario. func (scenario GetStartedInstances) Run(ctx context.Context, dbEngine string, parameterGroupName string, instanceName string, dbName string) { defer func() { if r := recover(); r != nil { log.Println("Something went wrong with the demo.") } }() log.Println(strings.Repeat("-", 88)) log.Println("Welcome to the Amazon Relational Database Service (Amazon RDS) DB Instance demo.") log.Println(strings.Repeat("-", 88)) parameterGroup := scenario.CreateParameterGroup(ctx, dbEngine, parameterGroupName) scenario.SetUserParameters(ctx, parameterGroupName) instance := scenario.CreateInstance(ctx, instanceName, dbEngine, dbName, parameterGroup) scenario.DisplayConnection(instance) scenario.CreateSnapshot(ctx, instance) scenario.Cleanup(ctx, instance, parameterGroup) log.Println(strings.Repeat("-", 88)) log.Println("Thanks for watching!") log.Println(strings.Repeat("-", 88)) } // CreateParameterGroup shows how to get available engine versions for a specified // database engine and create a DB parameter group that is compatible with a // selected engine family. func (scenario GetStartedInstances) CreateParameterGroup(ctx context.Context, dbEngine string, parameterGroupName string) *types.DBParameterGroup { log.Printf("Checking for an existing DB parameter group named %v.\n", parameterGroupName) parameterGroup, err := scenario.instances.GetParameterGroup(ctx, parameterGroupName) if err != nil { panic(err) } if parameterGroup == nil { log.Printf("Getting available database engine versions for %v.\n", dbEngine) engineVersions, err := scenario.instances.GetEngineVersions(ctx, dbEngine, "") if err != nil { panic(err) } familySet := map[string]struct{}{} for _, family := range engineVersions { familySet[*family.DBParameterGroupFamily] = struct{}{} } var families []string for family := range familySet { families = append(families, family) } sort.Strings(families) familyIndex := scenario.questioner.AskChoice("Which family do you want to use?\n", families) log.Println("Creating a DB parameter group.") _, err = scenario.instances.CreateParameterGroup( ctx, parameterGroupName, families[familyIndex], "Example parameter group.") if err != nil { panic(err) } parameterGroup, err = scenario.instances.GetParameterGroup(ctx, parameterGroupName) if err != nil { panic(err) } } log.Printf("Parameter group %v:\n", *parameterGroup.DBParameterGroupFamily) log.Printf("\tName: %v\n", *parameterGroup.DBParameterGroupName) log.Printf("\tARN: %v\n", *parameterGroup.DBParameterGroupArn) log.Printf("\tFamily: %v\n", *parameterGroup.DBParameterGroupFamily) log.Printf("\tDescription: %v\n", *parameterGroup.Description) log.Println(strings.Repeat("-", 88)) return parameterGroup } // SetUserParameters shows how to get the parameters contained in a custom parameter // group and update some of the parameter values in the group. func (scenario GetStartedInstances) SetUserParameters(ctx context.Context, parameterGroupName string) { log.Println("Let's set some parameter values in your parameter group.") dbParameters, err := scenario.instances.GetParameters(ctx, parameterGroupName, "") if err != nil { panic(err) } var updateParams []types.Parameter for _, dbParam := range dbParameters { if strings.HasPrefix(*dbParam.ParameterName, "auto_increment") && *dbParam.IsModifiable && *dbParam.DataType == "integer" { log.Printf("The %v parameter is described as:\n\t%v", *dbParam.ParameterName, *dbParam.Description) rangeSplit := strings.Split(*dbParam.AllowedValues, "-") lower, _ := strconv.Atoi(rangeSplit[0]) upper, _ := strconv.Atoi(rangeSplit[1]) newValue := scenario.questioner.AskInt( fmt.Sprintf("Enter a value between %v and %v:", lower, upper), demotools.InIntRange{Lower: lower, Upper: upper}) dbParam.ParameterValue = aws.String(strconv.Itoa(newValue)) updateParams = append(updateParams, dbParam) } } err = scenario.instances.UpdateParameters(ctx, parameterGroupName, updateParams) if err != nil { panic(err) } log.Println("To get a list of parameters that you set previously, specify a source of 'user'.") userParameters, err := scenario.instances.GetParameters(ctx, parameterGroupName, "user") if err != nil { panic(err) } log.Println("Here are the parameters you set:") for _, param := range userParameters { log.Printf("\t%v: %v\n", *param.ParameterName, *param.ParameterValue) } log.Println(strings.Repeat("-", 88)) } // CreateInstance shows how to create a DB instance that contains a database of a // specified type. The database is also configured to use a custom DB parameter group. func (scenario GetStartedInstances) CreateInstance(ctx context.Context, instanceName string, dbEngine string, dbName string, parameterGroup *types.DBParameterGroup) *types.DBInstance { log.Println("Checking for an existing DB instance.") instance, err := scenario.instances.GetInstance(ctx, instanceName) if err != nil { panic(err) } if instance == nil { adminUsername := scenario.questioner.Ask( "Enter an administrator username for the database: ", demotools.NotEmpty{}) adminPassword := scenario.questioner.AskPassword( "Enter a password for the administrator (at least 8 characters): ", 7) engineVersions, err := scenario.instances.GetEngineVersions(ctx, dbEngine, *parameterGroup.DBParameterGroupFamily) if err != nil { panic(err) } var engineChoices []string for _, engine := range engineVersions { engineChoices = append(engineChoices, *engine.EngineVersion) } engineIndex := scenario.questioner.AskChoice( "The available engines for your parameter group are:\n", engineChoices) engineSelection := engineVersions[engineIndex] instOpts, err := scenario.instances.GetOrderableInstances(ctx, *engineSelection.Engine, *engineSelection.EngineVersion) if err != nil { panic(err) } optSet := map[string]struct{}{} for _, opt := range instOpts { if strings.Contains(*opt.DBInstanceClass, "micro") { optSet[*opt.DBInstanceClass] = struct{}{} } } var optChoices []string for opt := range optSet { optChoices = append(optChoices, opt) } sort.Strings(optChoices) optIndex := scenario.questioner.AskChoice( "The available micro DB instance classes for your database engine are:\n", optChoices) storageType := "standard" allocatedStorage := int32(5) log.Printf("Creating a DB instance named %v and database %v.\n"+ "The DB instance is configured to use your custom parameter group %v,\n"+ "selected engine %v,\n"+ "selected DB instance class %v,"+ "and %v GiB of %v storage.\n"+ "This typically takes several minutes.", instanceName, dbName, *parameterGroup.DBParameterGroupName, *engineSelection.EngineVersion, optChoices[optIndex], allocatedStorage, storageType) instance, err = scenario.instances.CreateInstance( ctx, instanceName, dbName, *engineSelection.Engine, *engineSelection.EngineVersion, *parameterGroup.DBParameterGroupName, optChoices[optIndex], storageType, allocatedStorage, adminUsername, adminPassword) if err != nil { panic(err) } for *instance.DBInstanceStatus != "available" { scenario.helper.Pause(30) instance, err = scenario.instances.GetInstance(ctx, instanceName) if err != nil { panic(err) } } log.Println("Instance created and available.") } log.Println("Instance data:") log.Printf("\tDBInstanceIdentifier: %v\n", *instance.DBInstanceIdentifier) log.Printf("\tARN: %v\n", *instance.DBInstanceArn) log.Printf("\tStatus: %v\n", *instance.DBInstanceStatus) log.Printf("\tEngine: %v\n", *instance.Engine) log.Printf("\tEngine version: %v\n", *instance.EngineVersion) log.Println(strings.Repeat("-", 88)) return instance } // DisplayConnection displays connection information about a DB instance and tips // on how to connect to it. func (scenario GetStartedInstances) DisplayConnection(instance *types.DBInstance) { log.Println( "You can now connect to your database by 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 DB instance. Pass the endpoint,\n" + "port, and administrator username to 'mysql'. Then, enter your password\n" + "when prompted:") log.Printf("\n\tmysql -h %v -P %v -u %v -p\n", *instance.Endpoint.Address, instance.Endpoint.Port, *instance.MasterUsername) log.Println("For more information, see the User Guide for RDS:\n" + "\thttps://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_GettingStarted.CreatingConnecting.MySQL.html#CHAP_GettingStarted.Connecting.MySQL") log.Println(strings.Repeat("-", 88)) } // CreateSnapshot shows how to create a DB instance snapshot and wait until it's available. func (scenario GetStartedInstances) CreateSnapshot(ctx context.Context, instance *types.DBInstance) { if scenario.questioner.AskBool( "Do you want to create a snapshot of your DB instance (y/n)? ", "y") { snapshotId := fmt.Sprintf("%v-%v", *instance.DBInstanceIdentifier, scenario.helper.UniqueId()) log.Printf("Creating a snapshot named %v. This typically takes a few minutes.\n", snapshotId) snapshot, err := scenario.instances.CreateSnapshot(ctx, *instance.DBInstanceIdentifier, snapshotId) if err != nil { panic(err) } for *snapshot.Status != "available" { scenario.helper.Pause(30) snapshot, err = scenario.instances.GetSnapshot(ctx, snapshotId) if err != nil { panic(err) } } log.Println("Snapshot data:") log.Printf("\tDBSnapshotIdentifier: %v\n", *snapshot.DBSnapshotIdentifier) log.Printf("\tARN: %v\n", *snapshot.DBSnapshotArn) log.Printf("\tStatus: %v\n", *snapshot.Status) log.Printf("\tEngine: %v\n", *snapshot.Engine) log.Printf("\tEngine version: %v\n", *snapshot.EngineVersion) log.Printf("\tDBInstanceIdentifier: %v\n", *snapshot.DBInstanceIdentifier) log.Printf("\tSnapshotCreateTime: %v\n", *snapshot.SnapshotCreateTime) log.Println(strings.Repeat("-", 88)) } } // Cleanup shows how to clean up a DB instance and DB parameter group. // Before the DB parameter group can be deleted, all associated DB instances must first be deleted. func (scenario GetStartedInstances) Cleanup( ctx context.Context, instance *types.DBInstance, parameterGroup *types.DBParameterGroup) { if scenario.questioner.AskBool( "\nDo you want to delete the database instance and parameter group (y/n)? ", "y") { log.Printf("Deleting database instance %v.\n", *instance.DBInstanceIdentifier) err := scenario.instances.DeleteInstance(ctx, *instance.DBInstanceIdentifier) if err != nil { panic(err) } log.Println( "Waiting for the DB instance to delete. This typically takes several minutes.") for instance != nil { scenario.helper.Pause(30) instance, err = scenario.instances.GetInstance(ctx, *instance.DBInstanceIdentifier) if err != nil { panic(err) } } log.Printf("Deleting parameter group %v.", *parameterGroup.DBParameterGroupName) err = scenario.instances.DeleteParameterGroup(ctx, *parameterGroup.DBParameterGroupName) if err != nil { panic(err) } } }

Definisci le funzioni che vengono richiamate dallo scenario per gestire RDS le azioni di Amazon.

type DbInstances struct { RdsClient *rds.Client } // GetParameterGroup gets a DB parameter group by name. func (instances *DbInstances) GetParameterGroup(ctx context.Context, parameterGroupName string) ( *types.DBParameterGroup, error) { output, err := instances.RdsClient.DescribeDBParameterGroups( ctx, &rds.DescribeDBParameterGroupsInput{ DBParameterGroupName: aws.String(parameterGroupName), }) if err != nil { var notFoundError *types.DBParameterGroupNotFoundFault if errors.As(err, &notFoundError) { log.Printf("Parameter group %v does not exist.\n", parameterGroupName) err = nil } else { log.Printf("Error getting parameter group %v: %v\n", parameterGroupName, err) } return nil, err } else { return &output.DBParameterGroups[0], err } } // CreateParameterGroup creates a DB parameter group that is based on the specified // parameter group family. func (instances *DbInstances) CreateParameterGroup( ctx context.Context, parameterGroupName string, parameterGroupFamily string, description string) ( *types.DBParameterGroup, error) { output, err := instances.RdsClient.CreateDBParameterGroup(ctx, &rds.CreateDBParameterGroupInput{ DBParameterGroupName: aws.String(parameterGroupName), DBParameterGroupFamily: aws.String(parameterGroupFamily), Description: aws.String(description), }) if err != nil { log.Printf("Couldn't create parameter group %v: %v\n", parameterGroupName, err) return nil, err } else { return output.DBParameterGroup, err } } // DeleteParameterGroup deletes the named DB parameter group. func (instances *DbInstances) DeleteParameterGroup(ctx context.Context, parameterGroupName string) error { _, err := instances.RdsClient.DeleteDBParameterGroup(ctx, &rds.DeleteDBParameterGroupInput{ DBParameterGroupName: aws.String(parameterGroupName), }) if err != nil { log.Printf("Couldn't delete parameter group %v: %v\n", parameterGroupName, err) return err } else { return nil } } // GetParameters gets the parameters that are contained in a DB parameter group. func (instances *DbInstances) GetParameters(ctx context.Context, parameterGroupName string, source string) ( []types.Parameter, error) { var output *rds.DescribeDBParametersOutput var params []types.Parameter var err error parameterPaginator := rds.NewDescribeDBParametersPaginator(instances.RdsClient, &rds.DescribeDBParametersInput{ DBParameterGroupName: aws.String(parameterGroupName), Source: aws.String(source), }) for parameterPaginator.HasMorePages() { output, err = parameterPaginator.NextPage(ctx) if err != nil { log.Printf("Couldn't get parameters for %v: %v\n", parameterGroupName, err) break } else { params = append(params, output.Parameters...) } } return params, err } // UpdateParameters updates parameters in a named DB parameter group. func (instances *DbInstances) UpdateParameters(ctx context.Context, parameterGroupName string, params []types.Parameter) error { _, err := instances.RdsClient.ModifyDBParameterGroup(ctx, &rds.ModifyDBParameterGroupInput{ DBParameterGroupName: aws.String(parameterGroupName), Parameters: params, }) if err != nil { log.Printf("Couldn't update parameters in %v: %v\n", parameterGroupName, err) return err } else { return nil } } // CreateSnapshot creates a snapshot of a DB instance. func (instances *DbInstances) CreateSnapshot(ctx context.Context, instanceName string, snapshotName string) ( *types.DBSnapshot, error) { output, err := instances.RdsClient.CreateDBSnapshot(ctx, &rds.CreateDBSnapshotInput{ DBInstanceIdentifier: aws.String(instanceName), DBSnapshotIdentifier: aws.String(snapshotName), }) if err != nil { log.Printf("Couldn't create snapshot %v: %v\n", snapshotName, err) return nil, err } else { return output.DBSnapshot, nil } } // GetSnapshot gets a DB instance snapshot. func (instances *DbInstances) GetSnapshot(ctx context.Context, snapshotName string) (*types.DBSnapshot, error) { output, err := instances.RdsClient.DescribeDBSnapshots(ctx, &rds.DescribeDBSnapshotsInput{ DBSnapshotIdentifier: aws.String(snapshotName), }) if err != nil { log.Printf("Couldn't get snapshot %v: %v\n", snapshotName, err) return nil, err } else { return &output.DBSnapshots[0], nil } } // CreateInstance creates a DB instance. func (instances *DbInstances) CreateInstance(ctx context.Context, instanceName string, dbName string, dbEngine string, dbEngineVersion string, parameterGroupName string, dbInstanceClass string, storageType string, allocatedStorage int32, adminName string, adminPassword string) ( *types.DBInstance, error) { output, err := instances.RdsClient.CreateDBInstance(ctx, &rds.CreateDBInstanceInput{ DBInstanceIdentifier: aws.String(instanceName), DBName: aws.String(dbName), DBParameterGroupName: aws.String(parameterGroupName), Engine: aws.String(dbEngine), EngineVersion: aws.String(dbEngineVersion), DBInstanceClass: aws.String(dbInstanceClass), StorageType: aws.String(storageType), AllocatedStorage: aws.Int32(allocatedStorage), MasterUsername: aws.String(adminName), MasterUserPassword: aws.String(adminPassword), }) if err != nil { log.Printf("Couldn't create instance %v: %v\n", instanceName, err) return nil, err } else { return output.DBInstance, nil } } // GetInstance gets data about a DB instance. func (instances *DbInstances) GetInstance(ctx context.Context, instanceName string) ( *types.DBInstance, error) { output, err := instances.RdsClient.DescribeDBInstances(ctx, &rds.DescribeDBInstancesInput{ DBInstanceIdentifier: aws.String(instanceName), }) if err != nil { var notFoundError *types.DBInstanceNotFoundFault if errors.As(err, &notFoundError) { log.Printf("DB instance %v does not exist.\n", instanceName) err = nil } else { log.Printf("Couldn't get instance %v: %v\n", instanceName, err) } return nil, err } else { return &output.DBInstances[0], nil } } // DeleteInstance deletes a DB instance. func (instances *DbInstances) DeleteInstance(ctx context.Context, instanceName string) error { _, err := instances.RdsClient.DeleteDBInstance(ctx, &rds.DeleteDBInstanceInput{ DBInstanceIdentifier: aws.String(instanceName), SkipFinalSnapshot: aws.Bool(true), DeleteAutomatedBackups: aws.Bool(true), }) if err != nil { log.Printf("Couldn't delete instance %v: %v\n", instanceName, err) return err } else { return nil } } // GetEngineVersions gets database engine versions that are available for the specified engine // and parameter group family. func (instances *DbInstances) GetEngineVersions(ctx context.Context, engine string, parameterGroupFamily string) ( []types.DBEngineVersion, error) { output, err := instances.RdsClient.DescribeDBEngineVersions(ctx, &rds.DescribeDBEngineVersionsInput{ Engine: aws.String(engine), DBParameterGroupFamily: aws.String(parameterGroupFamily), }) if err != nil { log.Printf("Couldn't get engine versions for %v: %v\n", engine, err) return nil, err } else { return output.DBEngineVersions, nil } } // GetOrderableInstances uses a paginator to get DB instance options that can be used to create DB instances that are // compatible with a set of specifications. func (instances *DbInstances) GetOrderableInstances(ctx context.Context, engine string, engineVersion string) ( []types.OrderableDBInstanceOption, error) { var output *rds.DescribeOrderableDBInstanceOptionsOutput var instanceOptions []types.OrderableDBInstanceOption var err error orderablePaginator := rds.NewDescribeOrderableDBInstanceOptionsPaginator(instances.RdsClient, &rds.DescribeOrderableDBInstanceOptionsInput{ Engine: aws.String(engine), EngineVersion: aws.String(engineVersion), }) for orderablePaginator.HasMorePages() { output, err = orderablePaginator.NextPage(ctx) if err != nil { log.Printf("Couldn't get orderable DB instance options: %v\n", err) break } else { instanceOptions = append(instanceOptions, output.OrderableDBInstanceOptions...) } } return instanceOptions, err }
Java
SDKper Java 2.x
Nota

C'è di più su. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS.

Esegui più operazioni.

import com.google.gson.Gson; import software.amazon.awssdk.auth.credentials.EnvironmentVariableCredentialsProvider; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.rds.RdsClient; import software.amazon.awssdk.services.rds.model.CreateDbInstanceRequest; import software.amazon.awssdk.services.rds.model.CreateDbInstanceResponse; import software.amazon.awssdk.services.rds.model.CreateDbParameterGroupResponse; import software.amazon.awssdk.services.rds.model.CreateDbSnapshotRequest; import software.amazon.awssdk.services.rds.model.CreateDbSnapshotResponse; import software.amazon.awssdk.services.rds.model.DBEngineVersion; import software.amazon.awssdk.services.rds.model.DBInstance; import software.amazon.awssdk.services.rds.model.DBParameterGroup; import software.amazon.awssdk.services.rds.model.DBSnapshot; import software.amazon.awssdk.services.rds.model.DeleteDbInstanceRequest; import software.amazon.awssdk.services.rds.model.DeleteDbInstanceResponse; import software.amazon.awssdk.services.rds.model.DescribeDbEngineVersionsRequest; import software.amazon.awssdk.services.rds.model.DescribeDbEngineVersionsResponse; import software.amazon.awssdk.services.rds.model.DescribeDbInstancesRequest; import software.amazon.awssdk.services.rds.model.DescribeDbInstancesResponse; import software.amazon.awssdk.services.rds.model.DescribeDbParameterGroupsResponse; import software.amazon.awssdk.services.rds.model.DescribeDbParametersResponse; import software.amazon.awssdk.services.rds.model.DescribeDbSnapshotsRequest; import software.amazon.awssdk.services.rds.model.DescribeDbSnapshotsResponse; import software.amazon.awssdk.services.rds.model.DescribeOrderableDbInstanceOptionsResponse; import software.amazon.awssdk.services.rds.model.ModifyDbParameterGroupResponse; import software.amazon.awssdk.services.rds.model.OrderableDBInstanceOption; import software.amazon.awssdk.services.rds.model.Parameter; import software.amazon.awssdk.services.rds.model.RdsException; import software.amazon.awssdk.services.rds.model.CreateDbParameterGroupRequest; import software.amazon.awssdk.services.rds.model.DescribeDbParameterGroupsRequest; import software.amazon.awssdk.services.rds.model.DescribeDbParametersRequest; import software.amazon.awssdk.services.rds.model.ModifyDbParameterGroupRequest; import software.amazon.awssdk.services.rds.model.DescribeOrderableDbInstanceOptionsRequest; import software.amazon.awssdk.services.rds.model.DeleteDbParameterGroupRequest; import software.amazon.awssdk.services.secretsmanager.SecretsManagerClient; import software.amazon.awssdk.services.secretsmanager.model.GetSecretValueRequest; import software.amazon.awssdk.services.secretsmanager.model.GetSecretValueResponse; import java.util.ArrayList; import java.util.List; /** * 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 example requires an AWS Secrets Manager secret that contains the * database credentials. If you do not create a * secret, this example will not work. For details, see: * * https://docs.aws.amazon.com/secretsmanager/latest/userguide/integrating_how-services-use-secrets_RS.html * * This Java example performs these tasks: * * 1. Returns a list of the available DB engines. * 2. Selects an engine family and create a custom DB parameter group. * 3. Gets the parameter groups. * 4. Gets parameters in the group. * 5. Modifies the auto_increment_offset parameter. * 6. Gets and displays the updated parameters. * 7. Gets a list of allowed engine versions. * 8. Gets a list of micro instance classes available for the selected engine. * 9. Creates an RDS database instance that contains a MySql database and uses * the parameter group. * 10. Waits for the DB instance to be ready and prints out the connection * endpoint value. * 11. Creates a snapshot of the DB instance. * 12. Waits for an RDS DB snapshot to be ready. * 13. Deletes the RDS DB instance. * 14. Deletes the parameter group. */ public class RDSScenario { 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 = """ Usage: <dbGroupName> <dbParameterGroupFamily> <dbInstanceIdentifier> <dbName> <dbSnapshotIdentifier> <secretName> Where: dbGroupName - The database group name.\s dbParameterGroupFamily - The database parameter group name (for example, mysql8.0). dbInstanceIdentifier - The database instance identifier\s dbName - The database name.\s dbSnapshotIdentifier - The snapshot identifier.\s secretName - The name of the AWS Secrets Manager secret that contains the database credentials" """; if (args.length != 6) { System.out.println(usage); System.exit(1); } String dbGroupName = args[0]; String dbParameterGroupFamily = args[1]; String dbInstanceIdentifier = args[2]; String dbName = args[3]; String dbSnapshotIdentifier = args[4]; String secretName = args[5]; Gson gson = new Gson(); User user = gson.fromJson(String.valueOf(getSecretValues(secretName)), User.class); String masterUsername = user.getUsername(); String masterUserPassword = user.getPassword(); Region region = Region.US_WEST_2; RdsClient rdsClient = RdsClient.builder() .region(region) .build(); System.out.println(DASHES); System.out.println("Welcome to the Amazon RDS 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"); createDBParameterGroup(rdsClient, dbGroupName, dbParameterGroupFamily); System.out.println(DASHES); System.out.println(DASHES); System.out.println("3. Get the parameter group"); describeDbParameterGroups(rdsClient, dbGroupName); System.out.println(DASHES); System.out.println(DASHES); System.out.println("4. Get the parameters in the group"); describeDbParameters(rdsClient, dbGroupName, 0); System.out.println(DASHES); System.out.println(DASHES); System.out.println("5. Modify the auto_increment_offset parameter"); modifyDBParas(rdsClient, dbGroupName); System.out.println(DASHES); System.out.println(DASHES); System.out.println("6. Display the updated value"); describeDbParameters(rdsClient, dbGroupName, -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. Get a list of micro instance classes available for the selected engine"); getMicroInstances(rdsClient); System.out.println(DASHES); System.out.println(DASHES); System.out.println( "9. Create an RDS database instance that contains a MySql database and uses the parameter group"); String dbARN = createDatabaseInstance(rdsClient, dbGroupName, dbInstanceIdentifier, dbName, masterUsername, masterUserPassword); System.out.println("The ARN of the new database is " + dbARN); System.out.println(DASHES); System.out.println(DASHES); System.out.println("10. Wait for DB instance to be ready"); waitForInstanceReady(rdsClient, dbInstanceIdentifier); System.out.println(DASHES); System.out.println(DASHES); System.out.println("11. Create a snapshot of the DB instance"); createSnapshot(rdsClient, dbInstanceIdentifier, dbSnapshotIdentifier); System.out.println(DASHES); System.out.println(DASHES); System.out.println("12. Wait for DB snapshot to be ready"); waitForSnapshotReady(rdsClient, dbInstanceIdentifier, dbSnapshotIdentifier); System.out.println(DASHES); System.out.println(DASHES); System.out.println("13. Delete the DB instance"); deleteDatabaseInstance(rdsClient, dbInstanceIdentifier); System.out.println(DASHES); System.out.println(DASHES); System.out.println("14. Delete the parameter group"); deleteParaGroup(rdsClient, dbGroupName, dbARN); System.out.println(DASHES); System.out.println(DASHES); System.out.println("The Scenario has successfully completed."); System.out.println(DASHES); rdsClient.close(); } private static SecretsManagerClient getSecretClient() { Region region = Region.US_WEST_2; return SecretsManagerClient.builder() .region(region) .credentialsProvider(EnvironmentVariableCredentialsProvider.create()) .build(); } public static String getSecretValues(String secretName) { SecretsManagerClient secretClient = getSecretClient(); GetSecretValueRequest valueRequest = GetSecretValueRequest.builder() .secretId(secretName) .build(); GetSecretValueResponse valueResponse = secretClient.getSecretValue(valueRequest); return valueResponse.secretString(); } // Delete the parameter group after database has been deleted. // An exception is thrown if you attempt to delete the para group while database // exists. public static void deleteParaGroup(RdsClient rdsClient, String dbGroupName, String dbARN) 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(); didFind = false; int index = 1; for (DBInstance instance : instanceList) { instanceARN = instance.dbInstanceArn(); if (instanceARN.compareTo(dbARN) == 0) { System.out.println(dbARN + " 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++; } } // Delete the para group. DeleteDbParameterGroupRequest parameterGroupRequest = DeleteDbParameterGroupRequest.builder() .dbParameterGroupName(dbGroupName) .build(); rdsClient.deleteDBParameterGroup(parameterGroupRequest); System.out.println(dbGroupName + " was deleted."); } catch (RdsException e) { System.out.println(e.getLocalizedMessage()); System.exit(1); } } // Delete the DB instance. 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.print("The status of the database is " + response.dbInstance().dbInstanceStatus()); } catch (RdsException e) { System.out.println(e.getLocalizedMessage()); System.exit(1); } } // Waits until the snapshot instance is available. public static void waitForSnapshotReady(RdsClient rdsClient, String dbInstanceIdentifier, String dbSnapshotIdentifier) { try { boolean snapshotReady = false; String snapshotReadyStr; System.out.println("Waiting for the snapshot to become available."); DescribeDbSnapshotsRequest snapshotsRequest = DescribeDbSnapshotsRequest.builder() .dbSnapshotIdentifier(dbSnapshotIdentifier) .dbInstanceIdentifier(dbInstanceIdentifier) .build(); while (!snapshotReady) { DescribeDbSnapshotsResponse response = rdsClient.describeDBSnapshots(snapshotsRequest); List<DBSnapshot> snapshotList = response.dbSnapshots(); for (DBSnapshot snapshot : snapshotList) { snapshotReadyStr = snapshot.status(); if (snapshotReadyStr.contains("available")) { snapshotReady = true; } else { System.out.print("."); Thread.sleep(sleepTime * 1000); } } } System.out.println("The Snapshot is available!"); } catch (RdsException | InterruptedException e) { System.out.println(e.getLocalizedMessage()); System.exit(1); } } // Create an Amazon RDS snapshot. public static void createSnapshot(RdsClient rdsClient, String dbInstanceIdentifier, String dbSnapshotIdentifier) { try { CreateDbSnapshotRequest snapshotRequest = CreateDbSnapshotRequest.builder() .dbInstanceIdentifier(dbInstanceIdentifier) .dbSnapshotIdentifier(dbSnapshotIdentifier) .build(); CreateDbSnapshotResponse response = rdsClient.createDBSnapshot(snapshotRequest); System.out.println("The Snapshot id is " + response.dbSnapshot().dbiResourceId()); } catch (RdsException e) { System.out.println(e.getLocalizedMessage()); System.exit(1); } } // Waits until the database instance is available. public static void waitForInstanceReady(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); } } // Create a database instance and return the ARN of the database. public static String createDatabaseInstance(RdsClient rdsClient, String dbGroupName, String dbInstanceIdentifier, String dbName, String masterUsername, String masterUserPassword) { try { CreateDbInstanceRequest instanceRequest = CreateDbInstanceRequest.builder() .dbInstanceIdentifier(dbInstanceIdentifier) .allocatedStorage(100) .dbName(dbName) .dbParameterGroupName(dbGroupName) .engine("mysql") .dbInstanceClass("db.m4.large") .engineVersion("8.0") .storageType("standard") .masterUsername(masterUsername) .masterUserPassword(masterUserPassword) .build(); CreateDbInstanceResponse response = rdsClient.createDBInstance(instanceRequest); System.out.print("The status is " + response.dbInstance().dbInstanceStatus()); return response.dbInstance().dbInstanceArn(); } catch (RdsException e) { System.out.println(e.getLocalizedMessage()); System.exit(1); } return ""; } // Get a list of micro instances. public static void getMicroInstances(RdsClient rdsClient) { try { DescribeOrderableDbInstanceOptionsRequest dbInstanceOptionsRequest = DescribeOrderableDbInstanceOptionsRequest .builder() .engine("mysql") .build(); DescribeOrderableDbInstanceOptionsResponse response = rdsClient .describeOrderableDBInstanceOptions(dbInstanceOptionsRequest); List<OrderableDBInstanceOption> orderableDBInstances = response.orderableDBInstanceOptions(); for (OrderableDBInstanceOption dbInstanceOption : orderableDBInstances) { System.out.println("The engine version is " + dbInstanceOption.engineVersion()); System.out.println("The engine description is " + dbInstanceOption.engine()); } } catch (RdsException e) { System.out.println(e.getLocalizedMessage()); System.exit(1); } } // Get a list of allowed engine versions. public static void getAllowedEngines(RdsClient rdsClient, String dbParameterGroupFamily) { try { DescribeDbEngineVersionsRequest versionsRequest = DescribeDbEngineVersionsRequest.builder() .dbParameterGroupFamily(dbParameterGroupFamily) .engine("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 auto_increment_offset and auto_increment_increment parameters. public static void modifyDBParas(RdsClient rdsClient, String dbGroupName) { try { Parameter parameter1 = Parameter.builder() .parameterName("auto_increment_offset") .applyMethod("immediate") .parameterValue("5") .build(); List<Parameter> paraList = new ArrayList<>(); paraList.add(parameter1); ModifyDbParameterGroupRequest groupRequest = ModifyDbParameterGroupRequest.builder() .dbParameterGroupName(dbGroupName) .parameters(paraList) .build(); ModifyDbParameterGroupResponse response = rdsClient.modifyDBParameterGroup(groupRequest); System.out.println("The parameter group " + response.dbParameterGroupName() + " was successfully modified"); } catch (RdsException e) { System.out.println(e.getLocalizedMessage()); System.exit(1); } } // Retrieve parameters in the group. public static void describeDbParameters(RdsClient rdsClient, String dbGroupName, int flag) { try { DescribeDbParametersRequest dbParameterGroupsRequest; if (flag == 0) { dbParameterGroupsRequest = DescribeDbParametersRequest.builder() .dbParameterGroupName(dbGroupName) .build(); } else { dbParameterGroupsRequest = DescribeDbParametersRequest.builder() .dbParameterGroupName(dbGroupName) .source("user") .build(); } DescribeDbParametersResponse response = rdsClient.describeDBParameters(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 describeDbParameterGroups(RdsClient rdsClient, String dbGroupName) { try { DescribeDbParameterGroupsRequest groupsRequest = DescribeDbParameterGroupsRequest.builder() .dbParameterGroupName(dbGroupName) .maxRecords(20) .build(); DescribeDbParameterGroupsResponse response = rdsClient.describeDBParameterGroups(groupsRequest); List<DBParameterGroup> groups = response.dbParameterGroups(); for (DBParameterGroup group : groups) { System.out.println("The group name is " + group.dbParameterGroupName()); System.out.println("The group description is " + group.description()); } } catch (RdsException e) { System.out.println(e.getLocalizedMessage()); System.exit(1); } } public static void createDBParameterGroup(RdsClient rdsClient, String dbGroupName, String dbParameterGroupFamily) { try { CreateDbParameterGroupRequest groupRequest = CreateDbParameterGroupRequest.builder() .dbParameterGroupName(dbGroupName) .dbParameterGroupFamily(dbParameterGroupFamily) .description("Created by using the AWS SDK for Java") .build(); CreateDbParameterGroupResponse response = rdsClient.createDBParameterGroup(groupRequest); System.out.println("The group name is " + response.dbParameterGroup().dbParameterGroupName()); } catch (RdsException e) { System.out.println(e.getLocalizedMessage()); System.exit(1); } } public static void describeDBEngines(RdsClient rdsClient) { try { DescribeDbEngineVersionsRequest engineVersionsRequest = DescribeDbEngineVersionsRequest.builder() .defaultOnly(true) .engine("mysql") .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
SDKper Kotlin
Nota

c'è altro da fare. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS.

/** Before running this 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 example requires an AWS Secrets Manager secret that contains the database credentials. If you do not create a secret, this example will not work. For more details, see: https://docs.aws.amazon.com/secretsmanager/latest/userguide/integrating_how-services-use-secrets_RS.html This example performs the following tasks: 1. Returns a list of the available DB engines by invoking the DescribeDbEngineVersions method. 2. Selects an engine family and create a custom DB parameter group by invoking the createDBParameterGroup method. 3. Gets the parameter groups by invoking the DescribeDbParameterGroups method. 4. Gets parameters in the group by invoking the DescribeDbParameters method. 5. Modifies both the auto_increment_offset and auto_increment_increment parameters by invoking the modifyDbParameterGroup method. 6. Gets and displays the updated parameters. 7. Gets a list of allowed engine versions by invoking the describeDbEngineVersions method. 8. Gets a list of micro instance classes available for the selected engine. 9. Creates an Amazon Relational Database Service (Amazon RDS) database instance that contains a MySQL database and uses the parameter group. 10. Waits for DB instance to be ready and prints out the connection endpoint value. 11. Creates a snapshot of the DB instance. 12. Waits for the DB snapshot to be ready. 13. Deletes the DB instance. 14. Deletes the parameter group. */ var sleepTime: Long = 20 suspend fun main(args: Array<String>) { val usage = """ Usage: <dbGroupName> <dbParameterGroupFamily> <dbInstanceIdentifier> <dbName> <dbSnapshotIdentifier><secretName> Where: dbGroupName - The database group name. dbParameterGroupFamily - The database parameter group name. dbInstanceIdentifier - The database instance identifier. dbName - The database name. dbSnapshotIdentifier - The snapshot identifier. secretName - The name of the AWS Secrets Manager secret that contains the database credentials. """ if (args.size != 6) { println(usage) exitProcess(1) } val dbGroupName = args[0] val dbParameterGroupFamily = args[1] val dbInstanceIdentifier = args[2] val dbName = args[3] val dbSnapshotIdentifier = args[4] val secretName = args[5] val gson = Gson() val user = gson.fromJson(getSecretValues(secretName).toString(), User::class.java) val username = user.username val userPassword = user.password println("1. Return a list of the available DB engines") describeDBEngines() println("2. Create a custom parameter group") createDBParameterGroup(dbGroupName, dbParameterGroupFamily) println("3. Get the parameter groups") describeDbParameterGroups(dbGroupName) println("4. Get the parameters in the group") describeDbParameters(dbGroupName, 0) println("5. Modify the auto_increment_offset parameter") modifyDBParas(dbGroupName) println("6. Display the updated value") describeDbParameters(dbGroupName, -1) println("7. Get a list of allowed engine versions") getAllowedEngines(dbParameterGroupFamily) println("8. Get a list of micro instance classes available for the selected engine") getMicroInstances() println("9. Create an RDS database instance that contains a MySql database and uses the parameter group") val dbARN = createDatabaseInstance(dbGroupName, dbInstanceIdentifier, dbName, username, userPassword) println("The ARN of the new database is $dbARN") println("10. Wait for DB instance to be ready") waitForDbInstanceReady(dbInstanceIdentifier) println("11. Create a snapshot of the DB instance") createDbSnapshot(dbInstanceIdentifier, dbSnapshotIdentifier) println("12. Wait for DB snapshot to be ready") waitForSnapshotReady(dbInstanceIdentifier, dbSnapshotIdentifier) println("13. Delete the DB instance") deleteDbInstance(dbInstanceIdentifier) println("14. Delete the parameter group") if (dbARN != null) { deleteParaGroup(dbGroupName, dbARN) } println("The Scenario has successfully completed.") } suspend fun deleteParaGroup( dbGroupName: String, dbARN: 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 // Reset this value. didFind = false // Reset this value. var index = 1 if (instanceList != null) { for (instance in instanceList) { instanceARN = instance.dbInstanceArn.toString() if (instanceARN.compareTo(dbARN) == 0) { println("$dbARN still exists") didFind = true } if (index == listSize && !didFind) { // Went through the entire list and did not find the database name. isDataDel = true } index++ } } } // Delete the para group. val parameterGroupRequest = DeleteDbParameterGroupRequest { dbParameterGroupName = dbGroupName } rdsClient.deleteDbParameterGroup(parameterGroupRequest) println("$dbGroupName 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}") } } // Waits until the snapshot instance is available. suspend fun waitForSnapshotReady( dbInstanceIdentifierVal: String?, dbSnapshotIdentifierVal: String?, ) { var snapshotReady = false var snapshotReadyStr: String println("Waiting for the snapshot to become available.") val snapshotsRequest = DescribeDbSnapshotsRequest { dbSnapshotIdentifier = dbSnapshotIdentifierVal dbInstanceIdentifier = dbInstanceIdentifierVal } while (!snapshotReady) { RdsClient { region = "us-west-2" }.use { rdsClient -> val response = rdsClient.describeDbSnapshots(snapshotsRequest) val snapshotList: List<DbSnapshot>? = response.dbSnapshots if (snapshotList != null) { for (snapshot in snapshotList) { snapshotReadyStr = snapshot.status.toString() if (snapshotReadyStr.contains("available")) { snapshotReady = true } else { print(".") delay(sleepTime * 1000) } } } } } println("The Snapshot is available!") } // Create an Amazon RDS snapshot. suspend fun createDbSnapshot( dbInstanceIdentifierVal: String?, dbSnapshotIdentifierVal: String?, ) { val snapshotRequest = CreateDbSnapshotRequest { dbInstanceIdentifier = dbInstanceIdentifierVal dbSnapshotIdentifier = dbSnapshotIdentifierVal } RdsClient { region = "us-west-2" }.use { rdsClient -> val response = rdsClient.createDbSnapshot(snapshotRequest) print("The Snapshot id is ${response.dbSnapshot?.dbiResourceId}") } } // Waits until the database instance is available. suspend fun waitForDbInstanceReady(dbInstanceIdentifierVal: String?) { var instanceReady = false var instanceReadyStr: String println("Waiting for instance to become available.") val instanceRequest = DescribeDbInstancesRequest { dbInstanceIdentifier = dbInstanceIdentifierVal } var endpoint = "" while (!instanceReady) { RdsClient { region = "us-west-2" }.use { rdsClient -> val response = rdsClient.describeDbInstances(instanceRequest) val instanceList = response.dbInstances if (instanceList != null) { for (instance in instanceList) { 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") } // Create a database instance and return the ARN of the database. suspend fun createDatabaseInstance( dbGroupNameVal: String?, dbInstanceIdentifierVal: String?, dbNameVal: String?, masterUsernameVal: String?, masterUserPasswordVal: String?, ): String? { val instanceRequest = CreateDbInstanceRequest { dbInstanceIdentifier = dbInstanceIdentifierVal allocatedStorage = 100 dbName = dbNameVal dbParameterGroupName = dbGroupNameVal engine = "mysql" dbInstanceClass = "db.m4.large" engineVersion = "8.0" storageType = "standard" masterUsername = masterUsernameVal masterUserPassword = masterUserPasswordVal } RdsClient { region = "us-west-2" }.use { rdsClient -> val response = rdsClient.createDbInstance(instanceRequest) print("The status is ${response.dbInstance?.dbInstanceStatus}") return response.dbInstance?.dbInstanceArn } } // Get a list of micro instances. suspend fun getMicroInstances() { val dbInstanceOptionsRequest = DescribeOrderableDbInstanceOptionsRequest { engine = "mysql" } RdsClient { region = "us-west-2" }.use { rdsClient -> val response = rdsClient.describeOrderableDbInstanceOptions(dbInstanceOptionsRequest) val orderableDBInstances = response.orderableDbInstanceOptions if (orderableDBInstances != null) { for (dbInstanceOption in orderableDBInstances) { println("The engine version is ${dbInstanceOption.engineVersion}") println("The engine description is ${dbInstanceOption.engine}") } } } } // Get a list of allowed engine versions. suspend fun getAllowedEngines(dbParameterGroupFamilyVal: String?) { val versionsRequest = DescribeDbEngineVersionsRequest { dbParameterGroupFamily = dbParameterGroupFamilyVal engine = "mysql" } RdsClient { region = "us-west-2" }.use { rdsClient -> val response = rdsClient.describeDbEngineVersions(versionsRequest) val dbEngines: List<DbEngineVersion>? = response.dbEngineVersions if (dbEngines != null) { for (dbEngine in dbEngines) { println("The engine version is ${dbEngine.engineVersion}") println("The engine description is ${dbEngine.dbEngineDescription}") } } } } // Modify the auto_increment_offset parameter. suspend fun modifyDBParas(dbGroupName: String) { val parameter1 = Parameter { parameterName = "auto_increment_offset" applyMethod = ApplyMethod.Immediate parameterValue = "5" } val paraList: ArrayList<Parameter> = ArrayList() paraList.add(parameter1) val groupRequest = ModifyDbParameterGroupRequest { dbParameterGroupName = dbGroupName parameters = paraList } RdsClient { region = "us-west-2" }.use { rdsClient -> val response = rdsClient.modifyDbParameterGroup(groupRequest) println("The parameter group ${response.dbParameterGroupName} was successfully modified") } } // Retrieve parameters in the group. suspend fun describeDbParameters( dbGroupName: String?, flag: Int, ) { val dbParameterGroupsRequest: DescribeDbParametersRequest dbParameterGroupsRequest = if (flag == 0) { DescribeDbParametersRequest { dbParameterGroupName = dbGroupName } } else { DescribeDbParametersRequest { dbParameterGroupName = dbGroupName source = "user" } } RdsClient { region = "us-west-2" }.use { rdsClient -> val response = rdsClient.describeDbParameters(dbParameterGroupsRequest) val dbParameters: List<Parameter>? = response.parameters var paraName: String if (dbParameters != null) { for (para in dbParameters) { // Only print out information about either auto_increment_offset or auto_increment_increment. paraName = para.parameterName.toString() if (paraName.compareTo("auto_increment_offset") == 0 || paraName.compareTo("auto_increment_increment ") == 0) { 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}") } } } } } suspend fun describeDbParameterGroups(dbGroupName: String?) { val groupsRequest = DescribeDbParameterGroupsRequest { dbParameterGroupName = dbGroupName maxRecords = 20 } RdsClient { region = "us-west-2" }.use { rdsClient -> val response = rdsClient.describeDbParameterGroups(groupsRequest) val groups = response.dbParameterGroups if (groups != null) { for (group in groups) { println("The group name is ${group.dbParameterGroupName}") println("The group description is ${group.description}") } } } } // Create a parameter group. suspend fun createDBParameterGroup( dbGroupName: String?, dbParameterGroupFamilyVal: String?, ) { val groupRequest = CreateDbParameterGroupRequest { dbParameterGroupName = dbGroupName dbParameterGroupFamily = dbParameterGroupFamilyVal description = "Created by using the AWS SDK for Kotlin" } RdsClient { region = "us-west-2" }.use { rdsClient -> val response = rdsClient.createDbParameterGroup(groupRequest) println("The group name is ${response.dbParameterGroup?.dbParameterGroupName}") } } // Returns a list of the available DB engines. suspend fun describeDBEngines() { val engineVersionsRequest = DescribeDbEngineVersionsRequest { defaultOnly = true engine = "mysql" maxRecords = 20 } RdsClient { region = "us-west-2" }.use { rdsClient -> val response = rdsClient.describeDbEngineVersions(engineVersionsRequest) val engines: List<DbEngineVersion>? = response.dbEngineVersions // Get all DbEngineVersion objects. if (engines != null) { for (engineOb in engines) { 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}") } } } } suspend fun getSecretValues(secretName: String?): String? { val valueRequest = GetSecretValueRequest { secretId = secretName } SecretsManagerClient { region = "us-west-2" }.use { secretsClient -> val valueResponse = secretsClient.getSecretValue(valueRequest) return valueResponse.secretString } }
Python
SDKper Python (Boto3)
Nota

C'è di più su. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS.

Esegui uno scenario interattivo al prompt dei comandi.

class RdsInstanceScenario: """Runs a scenario that shows how to get started using Amazon RDS DB instances.""" def __init__(self, instance_wrapper): """ :param instance_wrapper: An object that wraps Amazon RDS DB instance actions. """ self.instance_wrapper = instance_wrapper def create_parameter_group(self, parameter_group_name, db_engine): """ Shows how to get available engine versions for a specified database engine and create a DB parameter group that is compatible with a selected engine family. :param parameter_group_name: The name given to the newly created parameter group. :param db_engine: The database engine to use as a basis. :return: The newly created parameter group. """ print( f"Checking for an existing DB instance parameter group named {parameter_group_name}." ) parameter_group = self.instance_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.instance_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 parameter group.") self.instance_wrapper.create_parameter_group( parameter_group_name, families[family_index], "Example parameter group." ) parameter_group = self.instance_wrapper.get_parameter_group( parameter_group_name ) print(f"Parameter group {parameter_group['DBParameterGroupName']}:") pp(parameter_group) print("-" * 88) return parameter_group def update_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.instance_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.instance_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.instance_wrapper.get_parameters( parameter_group_name, source="user" ) pp(user_parameters) print("-" * 88) def create_instance(self, instance_name, db_name, db_engine, parameter_group): """ Shows how to create a DB instance that contains a database of a specified type and is configured to use a custom DB parameter group. :param instance_name: The name given to the newly created DB instance. :param db_name: The name given to the created database. :param db_engine: The engine of the created database. :param parameter_group: The parameter group that is associated with the DB instance. :return: The newly created DB instance. """ print("Checking for an existing DB instance.") db_inst = self.instance_wrapper.get_db_instance(instance_name) if db_inst is None: print("Let's create a DB instance.") 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.instance_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) engine_selection = engine_versions[engine_index] print( "The available micro DB instance classes for your database engine are:" ) inst_opts = self.instance_wrapper.get_orderable_instances( engine_selection["Engine"], engine_selection["EngineVersion"] ) inst_choices = list( { opt["DBInstanceClass"] for opt in inst_opts if "micro" in opt["DBInstanceClass"] } ) inst_index = q.choose( "Which micro DB instance class do you want to use? ", inst_choices ) group_name = parameter_group["DBParameterGroupName"] storage_type = "standard" allocated_storage = 5 print( f"Creating a DB instance named {instance_name} and database {db_name}.\n" f"The DB instance is configured to use your custom parameter group {group_name},\n" f"selected engine {engine_selection['EngineVersion']},\n" f"selected DB instance class {inst_choices[inst_index]}," f"and {allocated_storage} GiB of {storage_type} storage.\n" f"This typically takes several minutes." ) db_inst = self.instance_wrapper.create_db_instance( db_name, instance_name, group_name, engine_selection["Engine"], engine_selection["EngineVersion"], inst_choices[inst_index], storage_type, allocated_storage, admin_username, admin_password, ) while db_inst.get("DBInstanceStatus") != "available": wait(10) db_inst = self.instance_wrapper.get_db_instance(instance_name) print("Instance data:") pp(db_inst) print("-" * 88) return db_inst @staticmethod def display_connection(db_inst): """ Displays connection information about a DB instance and tips on how to connect to it. :param db_inst: The DB instance 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 DB instance. Pass the endpoint,\n" "port, and administrator user name to 'mysql' and enter your password\n" "when prompted:\n" ) print( f"\n\tmysql -h {db_inst['Endpoint']['Address']} -P {db_inst['Endpoint']['Port']} " f"-u {db_inst['MasterUsername']} -p\n" ) print( "For more information, see the User Guide for Amazon RDS:\n" "\thttps://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_GettingStarted.CreatingConnecting.MySQL.html#CHAP_GettingStarted.Connecting.MySQL" ) print("-" * 88) def create_snapshot(self, instance_name): """ Shows how to create a DB instance snapshot and wait until it's available. :param instance_name: The name of a DB instance to snapshot. """ if q.ask( "Do you want to create a snapshot of your DB instance (y/n)? ", q.is_yesno ): snapshot_id = f"{instance_name}-{uuid.uuid4()}" print( f"Creating a snapshot named {snapshot_id}. This typically takes a few minutes." ) snapshot = self.instance_wrapper.create_snapshot(snapshot_id, instance_name) while snapshot.get("Status") != "available": wait(10) snapshot = self.instance_wrapper.get_snapshot(snapshot_id) pp(snapshot) print("-" * 88) def cleanup(self, db_inst, parameter_group_name): """ Shows how to clean up a DB instance and parameter group. Before the parameter group can be deleted, all associated DB instances must first be deleted. :param db_inst: The DB instance to delete. :param parameter_group_name: The DB parameter group to delete. """ if q.ask( "\nDo you want to delete the DB instance and parameter group (y/n)? ", q.is_yesno, ): print(f"Deleting DB instance {db_inst['DBInstanceIdentifier']}.") self.instance_wrapper.delete_db_instance(db_inst["DBInstanceIdentifier"]) print( "Waiting for the DB instance to delete. This typically takes several minutes." ) while db_inst is not None: wait(10) db_inst = self.instance_wrapper.get_db_instance( db_inst["DBInstanceIdentifier"] ) print(f"Deleting parameter group {parameter_group_name}.") self.instance_wrapper.delete_parameter_group(parameter_group_name) def run_scenario(self, db_engine, parameter_group_name, instance_name, db_name): logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") print("-" * 88) print( "Welcome to the Amazon Relational Database Service (Amazon RDS)\n" "get started with DB instances demo." ) print("-" * 88) parameter_group = self.create_parameter_group(parameter_group_name, db_engine) self.update_parameters(parameter_group_name) db_inst = self.create_instance( instance_name, db_name, db_engine, parameter_group ) self.display_connection(db_inst) self.create_snapshot(instance_name) self.cleanup(db_inst, parameter_group_name) print("\nThanks for watching!") print("-" * 88) if __name__ == "__main__": try: scenario = RdsInstanceScenario(InstanceWrapper.from_client()) scenario.run_scenario( "mysql", "doc-example-parameter-group", "doc-example-instance", "docexampledb", ) except Exception: logging.exception("Something went wrong with the demo.")

Definisci le funzioni che vengono richiamate dallo scenario per gestire RDS le azioni di Amazon.

class InstanceWrapper: """Encapsulates Amazon RDS DB instance actions.""" def __init__(self, rds_client): """ :param rds_client: A Boto3 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 parameter group. :param parameter_group_name: The name of the parameter group to retrieve. :return: The parameter group. """ try: response = self.rds_client.describe_db_parameter_groups( DBParameterGroupName=parameter_group_name ) parameter_group = response["DBParameterGroups"][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 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_parameter_group( DBParameterGroupName=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 parameter group. :param parameter_group_name: The name of the parameter group to delete. :return: Data about the parameter group. """ try: self.rds_client.delete_db_parameter_group( DBParameterGroupName=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 def get_parameters(self, parameter_group_name, name_prefix="", source=None): """ Gets the parameters that are contained in a DB 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 = {"DBParameterGroupName": parameter_group_name} if source is not None: kwargs["Source"] = source parameters = [] paginator = self.rds_client.get_paginator("describe_db_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 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_parameter_group( DBParameterGroupName=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 create_snapshot(self, snapshot_id, instance_id): """ Creates a snapshot of a DB instance. :param snapshot_id: The ID to give the created snapshot. :param instance_id: The ID of the DB instance to snapshot. :return: Data about the newly created snapshot. """ try: response = self.rds_client.create_db_snapshot( DBSnapshotIdentifier=snapshot_id, DBInstanceIdentifier=instance_id ) snapshot = response["DBSnapshot"] except ClientError as err: logger.error( "Couldn't create snapshot of %s. Here's why: %s: %s", instance_id, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return snapshot def get_snapshot(self, snapshot_id): """ Gets a DB instance snapshot. :param snapshot_id: The ID of the snapshot to retrieve. :return: The retrieved snapshot. """ try: response = self.rds_client.describe_db_snapshots( DBSnapshotIdentifier=snapshot_id ) snapshot = response["DBSnapshots"][0] except ClientError as err: logger.error( "Couldn't get snapshot %s. Here's why: %s: %s", snapshot_id, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return snapshot 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 create_db_instance( self, db_name, instance_id, parameter_group_name, db_engine, db_engine_version, instance_class, storage_type, allocated_storage, admin_name, admin_password, ): """ Creates a DB instance. :param db_name: The name of the database that is created in the DB instance. :param instance_id: The ID to give the newly created DB instance. :param parameter_group_name: A parameter group to associate with the DB instance. :param db_engine: The database engine of a database to create in the DB instance. :param db_engine_version: The engine version for the created database. :param instance_class: The DB instance class for the newly created DB instance. :param storage_type: The storage type of the DB instance. :param allocated_storage: The amount of storage allocated on the DB instance, in GiBs. :param admin_name: The name of the admin user for the created database. :param admin_password: The admin password for the created database. :return: Data about the newly created DB instance. """ try: response = self.rds_client.create_db_instance( DBName=db_name, DBInstanceIdentifier=instance_id, DBParameterGroupName=parameter_group_name, Engine=db_engine, EngineVersion=db_engine_version, DBInstanceClass=instance_class, StorageType=storage_type, AllocatedStorage=allocated_storage, MasterUsername=admin_name, MasterUserPassword=admin_password, ) 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 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

Per un elenco completo di guide per AWS SDK sviluppatori ed esempi di codice, consultaUtilizzo di questo servizio con un AWS SDK. Questo argomento include anche informazioni su come iniziare e dettagli sulle SDK versioni precedenti.