使用 RDS for C++ 的 Amazon SDK 範例 - AWS SDK 程式碼範例

文件 AWS SDK AWS 範例 SDK 儲存庫中有更多可用的 GitHub 範例。

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

使用 RDS for C++ 的 Amazon SDK 範例

下列程式碼範例示範如何搭配 Amazon RDS AWS SDK for C++ 使用 來執行動作和實作常見案例。

基本概念是程式碼範例,示範如何在服務中執行基本操作。

Actions 是大型程式的程式碼摘錄,必須在內容中執行。雖然 動作會示範如何呼叫個別服務函數,但您可以在其相關案例中查看內容中的動作。

案例是程式碼範例,示範如何透過呼叫服務內的多個函數或與其他函數結合來完成特定任務 AWS 服務。

每個範例都包含完整原始程式碼的連結,您可以在其中找到如何在內容中設定和執行程式碼的指示。

開始使用

下列程式碼範例示範如何開始使用 Amazon RDS。

C++ 的 SDK
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

CMakeLists.txt CMake 檔案的程式碼。

# Set the minimum required version of CMake for this project. cmake_minimum_required(VERSION 3.13) # Set the AWS service components used by this project. set(SERVICE_COMPONENTS rds) # Set this project's name. project("hello_rds") # Set the C++ standard to use to build this target. # At least C++ 11 is required for the AWS SDK for C++. set(CMAKE_CXX_STANDARD 11) # Use the MSVC variable to determine if this is a Windows build. set(WINDOWS_BUILD ${MSVC}) if (WINDOWS_BUILD) # Set the location where CMake can find the installed libraries for the AWS SDK. string(REPLACE ";" "/aws-cpp-sdk-all;" SYSTEM_MODULE_PATH "${CMAKE_SYSTEM_PREFIX_PATH}/aws-cpp-sdk-all") list(APPEND CMAKE_PREFIX_PATH ${SYSTEM_MODULE_PATH}) endif () # Find the AWS SDK for C++ package. find_package(AWSSDK REQUIRED COMPONENTS ${SERVICE_COMPONENTS}) if (WINDOWS_BUILD AND AWSSDK_INSTALL_AS_SHARED_LIBS) # Copy relevant AWS SDK for C++ libraries into the current binary directory for running and debugging. # set(BIN_SUB_DIR "/Debug") # If you are building from the command line, you may need to uncomment this # and set the proper subdirectory to the executables' location. AWSSDK_CPY_DYN_LIBS(SERVICE_COMPONENTS "" ${CMAKE_CURRENT_BINARY_DIR}${BIN_SUB_DIR}) endif () add_executable(${PROJECT_NAME} hello_rds.cpp) target_link_libraries(${PROJECT_NAME} ${AWSSDK_LINK_LIBRARIES})

hello_rds.cpp 來源檔案的程式碼。

#include <aws/core/Aws.h> #include <aws/rds/RDSClient.h> #include <aws/rds/model/DescribeDBInstancesRequest.h> #include <iostream> /* * A "Hello Rds" starter application which initializes an Amazon Relational Database Service (Amazon RDS) client and * describes the Amazon RDS instances. * * main function * * Usage: 'hello_rds' * */ int main(int argc, char **argv) { Aws::SDKOptions options; // Optionally change the log level for debugging. // options.loggingOptions.logLevel = Utils::Logging::LogLevel::Debug; Aws::InitAPI(options); // Should only be called once. int result = 0; { Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; Aws::RDS::RDSClient rdsClient(clientConfig); Aws::String marker; std::vector<Aws::String> instanceDBIDs; do { Aws::RDS::Model::DescribeDBInstancesRequest request; if (!marker.empty()) { request.SetMarker(marker); } Aws::RDS::Model::DescribeDBInstancesOutcome outcome = rdsClient.DescribeDBInstances(request); if (outcome.IsSuccess()) { for (auto &instance: outcome.GetResult().GetDBInstances()) { instanceDBIDs.push_back(instance.GetDBInstanceIdentifier()); } marker = outcome.GetResult().GetMarker(); } else { result = 1; std::cerr << "Error with RDS::DescribeDBInstances. " << outcome.GetError().GetMessage() << std::endl; break; } } while (!marker.empty()); std::cout << instanceDBIDs.size() << " RDS instances found." << std::endl; for (auto &instanceDBID: instanceDBIDs) { std::cout << " Instance: " << instanceDBID << std::endl; } } Aws::ShutdownAPI(options); // Should only be called once. return result; }

基本概念

以下程式碼範例顯示做法:

  • 建立自訂資料庫參數群組並設定參數值。

  • 建立資料庫執行個體,設定為使用參數群組。資料庫執行個體也包含資料庫。

  • 擷取執行個體的快照。

  • 刪除執行個體和參數群組。

C++ 的 SDK
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 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; }

動作

下列程式碼範例示範如何使用 CreateDBInstance

C++ 的 SDK
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; Aws::RDS::RDSClient client(clientConfig); 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; }

下列程式碼範例示範如何使用 CreateDBParameterGroup

C++ 的 SDK
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; Aws::RDS::RDSClient client(clientConfig); 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; }

下列程式碼範例示範如何使用 CreateDBSnapshot

C++ 的 SDK
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; Aws::RDS::RDSClient client(clientConfig); 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; }

下列程式碼範例示範如何使用 DeleteDBInstance

C++ 的 SDK
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; Aws::RDS::RDSClient client(clientConfig); 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; }

下列程式碼範例示範如何使用 DeleteDBParameterGroup

C++ 的 SDK
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; Aws::RDS::RDSClient client(clientConfig); 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; }

下列程式碼範例示範如何使用 DescribeDBEngineVersions

C++ 的 SDK
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; Aws::RDS::RDSClient client(clientConfig); //! 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; }

下列程式碼範例示範如何使用 DescribeDBInstances

C++ 的 SDK
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; Aws::RDS::RDSClient client(clientConfig); //! 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; }

下列程式碼範例示範如何使用 DescribeDBParameterGroups

C++ 的 SDK
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; Aws::RDS::RDSClient client(clientConfig); 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 { std::cerr << "Error with RDS::DescribeDBParameterGroups. " << outcome.GetError().GetMessage() << std::endl; return false; }

下列程式碼範例示範如何使用 DescribeDBParameters

C++ 的 SDK
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; Aws::RDS::RDSClient client(clientConfig); //! 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; }

下列程式碼範例示範如何使用 DescribeDBSnapshots

C++ 的 SDK
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; Aws::RDS::RDSClient client(clientConfig); 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; }

下列程式碼範例示範如何使用 DescribeOrderableDBInstanceOptions

C++ 的 SDK
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; Aws::RDS::RDSClient client(clientConfig); //! 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; }

下列程式碼範例示範如何使用 ModifyDBParameterGroup

C++ 的 SDK
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; Aws::RDS::RDSClient client(clientConfig); 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; }

案例

下列程式碼範例示範如何建立 Web 應用程式,追蹤 Amazon Aurora Serverless 資料庫中的工作項目,並使用 Amazon Simple Email Service (Amazon SES) 傳送報告。

C++ 的 SDK

說明如何建立可追蹤和報告存放在 Amazon Aurora Serverless 資料庫中的工作項目的 Web 應用程式。

如需如何設定查詢 Amazon Aurora Serverless 資料並供 React 應用程式使用之 C++ API REST 的完整原始程式碼和指示,請參閱 GitHub 上的完整範例。

此範例中使用的服務
  • Aurora

  • Amazon RDS

  • Amazon RDS Data Service

  • Amazon SES