RDS for C++ を使用した Amazon SDK の例 - AWS SDKコードの例

Doc AWS SDK ExamplesWord リポジトリには、さらに多くの GitHub の例があります。 AWS SDK

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

RDS for C++ を使用した Amazon SDK の例

次のコード例は、Amazon RDS AWS SDK for C++ で を使用してアクションを実行し、一般的なシナリオを実装する方法を示しています。

「基本」は、重要なオペレーションをサービス内で実行する方法を示すコード例です。

アクションはより大きなプログラムからのコードの抜粋であり、コンテキスト内で実行する必要があります。アクションは個々のサービス機能を呼び出す方法を示していますが、コンテキスト内のアクションは、関連するシナリオで確認できます。

「シナリオ」は、1 つのサービス内から、または他の AWS のサービスと組み合わせて複数の関数を呼び出し、特定のタスクを実行する方法を示すコード例です。

各例には、完全なソースコードへのリンクが含まれています。このリンクでは、コンテキストでコードを設定および実行する手順を確認できます。

開始方法

次のコード例は、Amazon RDS の使用を開始する方法を示しています。

C++ のSDK
注記

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; }
  • API の詳細については、AWS SDK for C++ API リファレンス「DescribeDBInstances」を参照してください。

基本

次のコードサンプルは、以下の操作方法を示しています。

  • カスタム DB パラメータグループを作成し、パラメータ値を設定します。

  • パラメータグループを使用するように設定した DB インスタンスを作成します。DB インスタンスにはデータベースも含まれています。

  • インスタンスのスナップショットを取得します。

  • インスタンスとパラメータグループを削除します。

C++ のSDK
注記

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
注記

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; }
  • API の詳細については、AWS SDK for C++ API リファレンス「CreateDBInstance」を参照してください。

次の例は、CreateDBParameterGroup を使用する方法を説明しています。

C++ のSDK
注記

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
注記

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; }
  • API の詳細については、AWS SDK for C++ API リファレンス「CreateDBSnapshot」を参照してください。

次の例は、DeleteDBInstance を使用する方法を説明しています。

C++ のSDK
注記

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; }
  • API の詳細については、AWS SDK for C++ API リファレンス「DeleteDBInstance」を参照してください。

次の例は、DeleteDBParameterGroup を使用する方法を説明しています。

C++ のSDK
注記

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
注記

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
注記

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; }
  • API の詳細については、AWS SDK for C++ API リファレンス「DescribeDBInstances」を参照してください。

次の例は、DescribeDBParameterGroups を使用する方法を説明しています。

C++ のSDK
注記

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
注記

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; }
  • API の詳細については、AWS SDK for C++ API リファレンス「DescribeDBParameters」を参照してください。

次のコード例は、DescribeDBSnapshots を使用する方法を示しています。

C++ のSDK
注記

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; }
  • API の詳細については、AWS SDK for C++ API リファレンス「DescribeDBSnapshots」を参照してください。

次の例は、DescribeOrderableDBInstanceOptions を使用する方法を説明しています。

C++ のSDK
注記

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
注記

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

シナリオ

次のコード例は、Amazon Aurora Serverless データベース内の作業項目を追跡し、Amazon Simple Email Service (Amazon SES) を使用してレポートを送信するウェブアプリケーションを作成する方法を示しています。

C++ のSDK

Amazon Aurora Serverless データベースに保存されている作業項目を追跡して報告するウェブアプリケーションを作成する方法を説明します。

Amazon Aurora Serverless データをクエリAPIする C++ REST をセットアップする方法と React アプリケーションで使用する方法については、GitHub の完全な例を参照してください。

この例で使用されているサービス
  • Aurora

  • Amazon RDS

  • Amazon RDS Data Service

  • Amazon SES