SDK for C++ を使用した自動スケーリングの例 - AWS SDK コード例

Doc AWS SDK Examples リポジトリには、他にも SDK の例があります。 AWS GitHub

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

SDK for C++ を使用した自動スケーリングの例

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

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

「シナリオ」は、同じサービス内で複数の関数を呼び出して、特定のタスクを実行する方法を示すコード例です。

各例には、 へのリンクが含まれています。このリンクには GitHub、コンテキスト内でコードをセットアップして実行する方法の手順が記載されています。

開始方法

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

SDK for C++
注記

には他にもがあります GitHub。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

C MakeLists.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 autoscaling) # Set this project's name. project("hello_autoscaling") # 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_autoscaling.cpp) target_link_libraries(${PROJECT_NAME} ${AWSSDK_LINK_LIBRARIES})

hello_autoscaling.cpp ソースファイルのコード。

#include <aws/core/Aws.h> #include <aws/autoscaling/AutoScalingClient.h> #include <aws/autoscaling/model/DescribeAutoScalingGroupsRequest.h> #include <iostream> /* * A "Hello Autoscaling" starter application which initializes an Amazon EC2 Auto Scaling client and describes the * Amazon EC2 Auto Scaling groups. * * main function * * Usage: 'hello_autoscaling' * */ 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::AutoScaling::AutoScalingClient autoscalingClient(clientConfig); std::vector<Aws::String> groupNames; Aws::String nextToken; // Used for pagination. do { Aws::AutoScaling::Model::DescribeAutoScalingGroupsRequest request; if (!nextToken.empty()) { request.SetNextToken(nextToken); } Aws::AutoScaling::Model::DescribeAutoScalingGroupsOutcome outcome = autoscalingClient.DescribeAutoScalingGroups(request); if (outcome.IsSuccess()) { const Aws::Vector<Aws::AutoScaling::Model::AutoScalingGroup> &autoScalingGroups = outcome.GetResult().GetAutoScalingGroups(); for (auto &group: autoScalingGroups) { groupNames.push_back(group.GetAutoScalingGroupName()); } nextToken = outcome.GetResult().GetNextToken(); } else { std::cerr << "Error with AutoScaling::DescribeAutoScalingGroups. " << outcome.GetError().GetMessage() << std::endl; result = 1; break; } } while (!nextToken.empty()); std::cout << "Found " << groupNames.size() << " AutoScaling groups." << std::endl; for (auto &groupName: groupNames) { std::cout << "AutoScaling group: " << groupName << std::endl; } } Aws::ShutdownAPI(options); // Should only be called once. return result; }
  • API の詳細については、「 API リファレンスDescribeAutoScalingGroups」の「」を参照してください。 AWS SDK for C++

アクション

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

SDK for C++
注記

には他にもがあります GitHub。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; Aws::AutoScaling::AutoScalingClient autoScalingClient(clientConfig); Aws::AutoScaling::Model::CreateAutoScalingGroupRequest request; request.SetAutoScalingGroupName(groupName); Aws::Vector<Aws::String> availabilityGroupZones; availabilityGroupZones.push_back( availabilityZones[availabilityZoneChoice - 1].GetZoneName()); request.SetAvailabilityZones(availabilityGroupZones); request.SetMaxSize(1); request.SetMinSize(1); Aws::AutoScaling::Model::LaunchTemplateSpecification launchTemplateSpecification; launchTemplateSpecification.SetLaunchTemplateName(templateName); request.SetLaunchTemplate(launchTemplateSpecification); Aws::AutoScaling::Model::CreateAutoScalingGroupOutcome outcome = autoScalingClient.CreateAutoScalingGroup(request); if (outcome.IsSuccess()) { std::cout << "Created Auto Scaling group '" << groupName << "'..." << std::endl; } else if (outcome.GetError().GetErrorType() == Aws::AutoScaling::AutoScalingErrors::ALREADY_EXISTS_FAULT) { std::cout << "Auto Scaling group '" << groupName << "' already exists." << std::endl; } else { std::cerr << "Error with AutoScaling::CreateAutoScalingGroup. " << outcome.GetError().GetMessage() << std::endl; }
  • API の詳細については、「 API リファレンスCreateAutoScalingGroup」の「」を参照してください。 AWS SDK for C++

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

SDK for C++
注記

の詳細については、「」を参照してください GitHub。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; Aws::AutoScaling::AutoScalingClient autoScalingClient(clientConfig); Aws::AutoScaling::Model::DeleteAutoScalingGroupRequest request; request.SetAutoScalingGroupName(groupName); Aws::AutoScaling::Model::DeleteAutoScalingGroupOutcome outcome = autoScalingClient.DeleteAutoScalingGroup(request); if (outcome.IsSuccess()) { std::cout << "Auto Scaling group '" << groupName << "' was deleted." << std::endl; } else { std::cerr << "Error with AutoScaling::DeleteAutoScalingGroup. " << outcome.GetError().GetMessage() << std::endl; result = false; } }
  • API の詳細については、「 API リファレンスDeleteAutoScalingGroup」の「」を参照してください。 AWS SDK for C++

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

SDK for C++
注記

の詳細については、「」を参照してください GitHub。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; Aws::AutoScaling::AutoScalingClient autoScalingClient(clientConfig); Aws::AutoScaling::Model::DescribeAutoScalingGroupsRequest request; Aws::Vector<Aws::String> groupNames; groupNames.push_back(groupName); request.SetAutoScalingGroupNames(groupNames); Aws::AutoScaling::Model::DescribeAutoScalingGroupsOutcome outcome = client.DescribeAutoScalingGroups(request); if (outcome.IsSuccess()) { autoScalingGroup = outcome.GetResult().GetAutoScalingGroups(); } else { std::cerr << "Error with AutoScaling::DescribeAutoScalingGroups. " << outcome.GetError().GetMessage() << std::endl; }
  • API の詳細については、「 API リファレンスDescribeAutoScalingGroups」の「」を参照してください。 AWS SDK for C++

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

SDK for C++
注記

の詳細については、「」を参照してください GitHub。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; Aws::AutoScaling::AutoScalingClient autoScalingClient(clientConfig); Aws::AutoScaling::Model::DescribeAutoScalingInstancesRequest request; request.SetInstanceIds(instanceIDs); Aws::AutoScaling::Model::DescribeAutoScalingInstancesOutcome outcome = client.DescribeAutoScalingInstances(request); if (outcome.IsSuccess()) { const Aws::Vector<Aws::AutoScaling::Model::AutoScalingInstanceDetails> &instancesDetails = outcome.GetResult().GetAutoScalingInstances(); } else { std::cerr << "Error with AutoScaling::DescribeAutoScalingInstances. " << outcome.GetError().GetMessage() << std::endl; return false; }
  • API の詳細については、「 API リファレンスDescribeAutoScalingInstances」の「」を参照してください。 AWS SDK for C++

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

SDK for C++
注記

の詳細については、「」を参照してください GitHub。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; Aws::AutoScaling::AutoScalingClient autoScalingClient(clientConfig); Aws::AutoScaling::Model::DescribeScalingActivitiesRequest request; request.SetAutoScalingGroupName(groupName); Aws::Vector<Aws::AutoScaling::Model::Activity> allActivities; Aws::String nextToken; // Used for pagination; do { if (!nextToken.empty()) { request.SetNextToken(nextToken); } Aws::AutoScaling::Model::DescribeScalingActivitiesOutcome outcome = autoScalingClient.DescribeScalingActivities(request); if (outcome.IsSuccess()) { const Aws::Vector<Aws::AutoScaling::Model::Activity> &activities = outcome.GetResult().GetActivities(); allActivities.insert(allActivities.end(), activities.begin(), activities.end()); nextToken = outcome.GetResult().GetNextToken(); } else { std::cerr << "Error with AutoScaling::DescribeScalingActivities. " << outcome.GetError().GetMessage() << std::endl; } } while (!nextToken.empty()); std::cout << "Found " << allActivities.size() << " activities." << std::endl; std::cout << "Activities are ordered with the most recent first." << std::endl; for (const Aws::AutoScaling::Model::Activity &activity: allActivities) { std::cout << activity.GetDescription() << std::endl; std::cout << activity.GetDetails() << std::endl; }
  • API の詳細については、「 API リファレンスDescribeScalingActivities」の「」を参照してください。 AWS SDK for C++

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

SDK for C++
注記

の詳細については、「」を参照してください GitHub。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; Aws::AutoScaling::AutoScalingClient autoScalingClient(clientConfig); Aws::AutoScaling::Model::DisableMetricsCollectionRequest request; request.SetAutoScalingGroupName(groupName); Aws::AutoScaling::Model::DisableMetricsCollectionOutcome outcome = autoScalingClient.DisableMetricsCollection(request); if (outcome.IsSuccess()) { std::cout << "Metrics collection has been disabled." << std::endl; } else { std::cerr << "Error with AutoScaling::DisableMetricsCollection. " << outcome.GetError().GetMessage() << std::endl; }
  • API の詳細については、「 API リファレンスDisableMetricsCollection」の「」を参照してください。 AWS SDK for C++

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

SDK for C++
注記

の詳細については、「」を参照してください GitHub。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; Aws::AutoScaling::AutoScalingClient autoScalingClient(clientConfig); Aws::AutoScaling::Model::EnableMetricsCollectionRequest request; request.SetAutoScalingGroupName(groupName); request.AddMetrics("GroupMinSize"); request.AddMetrics("GroupMaxSize"); request.AddMetrics("GroupDesiredCapacity"); request.AddMetrics("GroupInServiceInstances"); request.AddMetrics("GroupTotalInstances"); request.SetGranularity("1Minute"); Aws::AutoScaling::Model::EnableMetricsCollectionOutcome outcome = autoScalingClient.EnableMetricsCollection(request); if (outcome.IsSuccess()) { std::cout << "Auto Scaling metrics have been enabled." << std::endl; } else { std::cerr << "Error with AutoScaling::EnableMetricsCollection. " << outcome.GetError().GetMessage() << std::endl; }
  • API の詳細については、「 API リファレンスEnableMetricsCollection」の「」を参照してください。 AWS SDK for C++

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

SDK for C++
注記

の詳細については、「」を参照してください GitHub。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; Aws::AutoScaling::AutoScalingClient autoScalingClient(clientConfig); Aws::AutoScaling::Model::SetDesiredCapacityRequest request; request.SetAutoScalingGroupName(groupName); request.SetDesiredCapacity(2); Aws::AutoScaling::Model::SetDesiredCapacityOutcome outcome = autoScalingClient.SetDesiredCapacity(request); if (!outcome.IsSuccess()) { std::cerr << "Error with AutoScaling::SetDesiredCapacityRequest. " << outcome.GetError().GetMessage() << std::endl; }
  • API の詳細については、「 API リファレンスSetDesiredCapacity」の「」を参照してください。 AWS SDK for C++

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

SDK for C++
注記

の詳細については、「」を参照してください GitHub。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; Aws::AutoScaling::AutoScalingClient autoScalingClient(clientConfig); Aws::AutoScaling::Model::TerminateInstanceInAutoScalingGroupRequest request; request.SetInstanceId(instanceIDs[instanceNumber - 1]); request.SetShouldDecrementDesiredCapacity(false); Aws::AutoScaling::Model::TerminateInstanceInAutoScalingGroupOutcome outcome = autoScalingClient.TerminateInstanceInAutoScalingGroup(request); if (outcome.IsSuccess()) { std::cout << "Waiting for EC2 instance with ID '" << instanceIDs[instanceNumber - 1] << "' to terminate..." << std::endl; } else { std::cerr << "Error with AutoScaling::TerminateInstanceInAutoScalingGroup. " << outcome.GetError().GetMessage() << std::endl; }

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

SDK for C++
注記

の詳細については、「」を参照してください GitHub。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; Aws::AutoScaling::AutoScalingClient autoScalingClient(clientConfig); Aws::AutoScaling::Model::UpdateAutoScalingGroupRequest request; request.SetAutoScalingGroupName(groupName); request.SetMaxSize(3); Aws::AutoScaling::Model::UpdateAutoScalingGroupOutcome outcome = autoScalingClient.UpdateAutoScalingGroup(request); if (!outcome.IsSuccess()) { std::cerr << "Error with AutoScaling::UpdateAutoScalingGroup. " << outcome.GetError().GetMessage() << std::endl; }
  • API の詳細については、「 API リファレンスUpdateAutoScalingGroup」の「」を参照してください。 AWS SDK for C++

シナリオ

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

  • 起動テンプレートとアベイラビリティーゾーンを使用して、Amazon EC2 Auto Scaling グループを作成し、実行中のインスタンスに関する情報を取得します。

  • Amazon CloudWatch メトリクスの収集を有効にします。

  • グループの希望するキャパシティを更新し、インスタンスが起動するのを待ちます。

  • グループ内の最も古いインスタンスを削除します。

  • ユーザーのリクエストやキャパシティの変更に応じて発生するスケーリングアクティビティを一覧表示します。

  • CloudWatch メトリクスの統計を取得し、リソースをクリーンアップします。

SDK for C++
注記

の詳細については、「」を参照してください GitHub。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

//! Routine which demonstrates using an Auto Scaling group //! to manage Amazon EC2 instances. /*! \sa groupsAndInstancesScenario() \param clientConfig: AWS client configuration. \return bool: Successful completion. */ bool AwsDoc::AutoScaling::groupsAndInstancesScenario( const Aws::Client::ClientConfiguration &clientConfig) { Aws::String templateName; Aws::EC2::EC2Client ec2Client(clientConfig); std::cout << std::setfill('*') << std::setw(ASTERISK_FILL_WIDTH) << " " << std::endl; std::cout << "Welcome to the Amazon Elastic Compute Cloud (Amazon EC2) Auto Scaling " << "demo for managing groups and instances." << std::endl; std::cout << std::setfill('*') << std::setw(ASTERISK_FILL_WIDTH) << " \n" << std::endl; std::cout << "This example requires an EC2 launch template." << std::endl; if (askYesNoQuestion( "Would you like to use an existing EC2 launch template (y/n)? ")) { // 1. Specify the name of an existing EC2 launch template. templateName = askQuestion( "Enter the name of the existing EC2 launch template. "); Aws::EC2::Model::DescribeLaunchTemplatesRequest request; request.AddLaunchTemplateNames(templateName); Aws::EC2::Model::DescribeLaunchTemplatesOutcome outcome = ec2Client.DescribeLaunchTemplates(request); if (outcome.IsSuccess()) { std::cout << "Validated the EC2 launch template '" << templateName << "' exists by calling DescribeLaunchTemplate." << std::endl; } else { std::cerr << "Error validating the existence of the launch template. " << outcome.GetError().GetMessage() << std::endl; } } else { // 2. Or create a new EC2 launch template. templateName = askQuestion("Enter the name for a new EC2 launch template: "); Aws::EC2::Model::CreateLaunchTemplateRequest request; request.SetLaunchTemplateName(templateName); Aws::EC2::Model::RequestLaunchTemplateData requestLaunchTemplateData; requestLaunchTemplateData.SetInstanceType(EC2_LAUNCH_TEMPLATE_INSTANCE_TYPE); requestLaunchTemplateData.SetImageId(EC2_LAUNCH_TEMPLATE_IMAGE_ID); request.SetLaunchTemplateData(requestLaunchTemplateData); Aws::EC2::Model::CreateLaunchTemplateOutcome outcome = ec2Client.CreateLaunchTemplate(request); if (outcome.IsSuccess()) { std::cout << "The EC2 launch template '" << templateName << " was created." << std::endl; } else if (outcome.GetError().GetExceptionName() == "InvalidLaunchTemplateName.AlreadyExistsException") { std::cout << "The EC2 template '" << templateName << "' already exists" << std::endl; } else { std::cerr << "Error with EC2::CreateLaunchTemplate. " << outcome.GetError().GetMessage() << std::endl; } } Aws::AutoScaling::AutoScalingClient autoScalingClient(clientConfig); std::cout << "Let's create an Auto Scaling group." << std::endl; Aws::String groupName = askQuestion( "Enter a name for the Auto Scaling group: "); // 3. Retrieve a list of EC2 Availability Zones. Aws::Vector<Aws::EC2::Model::AvailabilityZone> availabilityZones; { Aws::EC2::Model::DescribeAvailabilityZonesRequest request; Aws::EC2::Model::DescribeAvailabilityZonesOutcome outcome = ec2Client.DescribeAvailabilityZones(request); if (outcome.IsSuccess()) { std::cout << "EC2 instances can be created in the following Availability Zones:" << std::endl; availabilityZones = outcome.GetResult().GetAvailabilityZones(); for (size_t i = 0; i < availabilityZones.size(); ++i) { std::cout << " " << i + 1 << ". " << availabilityZones[i].GetZoneName() << std::endl; } } else { std::cerr << "Error with EC2::DescribeAvailabilityZones. " << outcome.GetError().GetMessage() << std::endl; cleanupResources("", templateName, autoScalingClient, ec2Client); return false; } } int availabilityZoneChoice = askQuestionForIntRange( "Choose an Availability Zone: ", 1, static_cast<int>(availabilityZones.size())); // 4. Create an Auto Scaling group with the specified Availability Zone. { Aws::AutoScaling::Model::CreateAutoScalingGroupRequest request; request.SetAutoScalingGroupName(groupName); Aws::Vector<Aws::String> availabilityGroupZones; availabilityGroupZones.push_back( availabilityZones[availabilityZoneChoice - 1].GetZoneName()); request.SetAvailabilityZones(availabilityGroupZones); request.SetMaxSize(1); request.SetMinSize(1); Aws::AutoScaling::Model::LaunchTemplateSpecification launchTemplateSpecification; launchTemplateSpecification.SetLaunchTemplateName(templateName); request.SetLaunchTemplate(launchTemplateSpecification); Aws::AutoScaling::Model::CreateAutoScalingGroupOutcome outcome = autoScalingClient.CreateAutoScalingGroup(request); if (outcome.IsSuccess()) { std::cout << "Created Auto Scaling group '" << groupName << "'..." << std::endl; } else if (outcome.GetError().GetErrorType() == Aws::AutoScaling::AutoScalingErrors::ALREADY_EXISTS_FAULT) { std::cout << "Auto Scaling group '" << groupName << "' already exists." << std::endl; } else { std::cerr << "Error with AutoScaling::CreateAutoScalingGroup. " << outcome.GetError().GetMessage() << std::endl; cleanupResources("", templateName, autoScalingClient, ec2Client); return false; } } Aws::Vector<Aws::AutoScaling::Model::AutoScalingGroup> autoScalingGroups; if (AwsDoc::AutoScaling::describeGroup(groupName, autoScalingGroups, autoScalingClient)) { std::cout << "Here is the Auto Scaling group description." << std::endl; if (!autoScalingGroups.empty()) { logAutoScalingGroupInfo(autoScalingGroups); } } else { cleanupResources(groupName, templateName, autoScalingClient, ec2Client); return false; } std::cout << "Waiting for the EC2 instance in the Auto Scaling group to become active..." << std::endl; if (!waitForInstances(groupName, autoScalingGroups, autoScalingClient)) { cleanupResources(groupName, templateName, autoScalingClient, ec2Client); return false; } bool enableMetrics = askYesNoQuestion( "Do you want to collect metrics about the A" "Auto Scaling group during this demo (y/n)? "); // 7. Optionally enable metrics collection for the Auto Scaling group. if (enableMetrics) { Aws::AutoScaling::Model::EnableMetricsCollectionRequest request; request.SetAutoScalingGroupName(groupName); request.AddMetrics("GroupMinSize"); request.AddMetrics("GroupMaxSize"); request.AddMetrics("GroupDesiredCapacity"); request.AddMetrics("GroupInServiceInstances"); request.AddMetrics("GroupTotalInstances"); request.SetGranularity("1Minute"); Aws::AutoScaling::Model::EnableMetricsCollectionOutcome outcome = autoScalingClient.EnableMetricsCollection(request); if (outcome.IsSuccess()) { std::cout << "Auto Scaling metrics have been enabled." << std::endl; } else { std::cerr << "Error with AutoScaling::EnableMetricsCollection. " << outcome.GetError().GetMessage() << std::endl; cleanupResources(groupName, templateName, autoScalingClient, ec2Client); return false; } } std::cout << "Let's update the maximum number of EC2 instances in '" << groupName << "' from 1 to 3." << std::endl; askQuestion("Press enter to continue: ", alwaysTrueTest); // 8. Update the Auto Scaling group, setting a new maximum size. { Aws::AutoScaling::Model::UpdateAutoScalingGroupRequest request; request.SetAutoScalingGroupName(groupName); request.SetMaxSize(3); Aws::AutoScaling::Model::UpdateAutoScalingGroupOutcome outcome = autoScalingClient.UpdateAutoScalingGroup(request); if (!outcome.IsSuccess()) { std::cerr << "Error with AutoScaling::UpdateAutoScalingGroup. " << outcome.GetError().GetMessage() << std::endl; cleanupResources(groupName, templateName, autoScalingClient, ec2Client); return false; } } if (AwsDoc::AutoScaling::describeGroup(groupName, autoScalingGroups, autoScalingClient)) { if (!autoScalingGroups.empty()) { const auto &instances = autoScalingGroups[0].GetInstances(); std::cout << "The group still has one running EC2 instance, but it can have up to 3.\n" << std::endl; logAutoScalingGroupInfo(autoScalingGroups); } else { std::cerr << "No EC2 launch groups were retrieved from DescribeGroup request." << std::endl; cleanupResources(groupName, templateName, autoScalingClient, ec2Client); return false; } } std::cout << "\n" << std::setfill('*') << std::setw(ASTERISK_FILL_WIDTH) << "\n" << std::endl; std::cout << "Let's update the desired capacity in '" << groupName << "' from 1 to 2." << std::endl; askQuestion("Press enter to continue: ", alwaysTrueTest); // 9. Update the Auto Scaling group, setting a new desired capacity. { Aws::AutoScaling::Model::SetDesiredCapacityRequest request; request.SetAutoScalingGroupName(groupName); request.SetDesiredCapacity(2); Aws::AutoScaling::Model::SetDesiredCapacityOutcome outcome = autoScalingClient.SetDesiredCapacity(request); if (!outcome.IsSuccess()) { std::cerr << "Error with AutoScaling::SetDesiredCapacityRequest. " << outcome.GetError().GetMessage() << std::endl; cleanupResources(groupName, templateName, autoScalingClient, ec2Client); return false; } } if (AwsDoc::AutoScaling::describeGroup(groupName, autoScalingGroups, autoScalingClient)) { if (!autoScalingGroups.empty()) { std::cout << "Here is the current state of the group." << std::endl; logAutoScalingGroupInfo(autoScalingGroups); } else { std::cerr << "No EC2 launch groups were retrieved from DescribeGroup request." << std::endl; cleanupResources(groupName, templateName, autoScalingClient, ec2Client); return false; } } std::cout << "Waiting for the new EC2 instance to start..." << std::endl; waitForInstances(groupName, autoScalingGroups, autoScalingClient); std::cout << "\n" << std::setfill('*') << std::setw(ASTERISK_FILL_WIDTH) << "\n" << std::endl; std::cout << "Let's terminate one of the EC2 instances in " << groupName << "." << std::endl; std::cout << "Because the desired capacity is 2, another EC2 instance will start " << "to replace the terminated EC2 instance." << std::endl; std::cout << "The currently running EC2 instances are:" << std::endl; if (autoScalingGroups.empty()) { std::cerr << "Error describing groups. No groups returned." << std::endl; cleanupResources(groupName, templateName, autoScalingClient, ec2Client); return false; } int instanceNumber = 1; Aws::Vector<Aws::String> instanceIDs = instancesToInstanceIDs( autoScalingGroups[0].GetInstances()); for (const Aws::String &instanceID: instanceIDs) { std::cout << " " << instanceNumber << ". " << instanceID << std::endl; ++instanceNumber; } instanceNumber = askQuestionForIntRange("Which EC2 instance do you want to stop? ", 1, static_cast<int>(instanceIDs.size())); // 10. Terminate an EC2 instance in the Auto Scaling group. { Aws::AutoScaling::Model::TerminateInstanceInAutoScalingGroupRequest request; request.SetInstanceId(instanceIDs[instanceNumber - 1]); request.SetShouldDecrementDesiredCapacity(false); Aws::AutoScaling::Model::TerminateInstanceInAutoScalingGroupOutcome outcome = autoScalingClient.TerminateInstanceInAutoScalingGroup(request); if (outcome.IsSuccess()) { std::cout << "Waiting for EC2 instance with ID '" << instanceIDs[instanceNumber - 1] << "' to terminate..." << std::endl; } else { std::cerr << "Error with AutoScaling::TerminateInstanceInAutoScalingGroup. " << outcome.GetError().GetMessage() << std::endl; cleanupResources(groupName, templateName, autoScalingClient, ec2Client); return false; } } waitForInstances(groupName, autoScalingGroups, autoScalingClient); std::cout << "\n" << std::setfill('*') << std::setw(ASTERISK_FILL_WIDTH) << "\n" << std::endl; std::cout << "Let's get a report of scaling activities for EC2 launch group '" << groupName << "'." << std::endl; askQuestion("Press enter to continue: ", alwaysTrueTest); // 11. Get a description of activities for the Auto Scaling group. { Aws::AutoScaling::Model::DescribeScalingActivitiesRequest request; request.SetAutoScalingGroupName(groupName); Aws::Vector<Aws::AutoScaling::Model::Activity> allActivities; Aws::String nextToken; // Used for pagination; do { if (!nextToken.empty()) { request.SetNextToken(nextToken); } Aws::AutoScaling::Model::DescribeScalingActivitiesOutcome outcome = autoScalingClient.DescribeScalingActivities(request); if (outcome.IsSuccess()) { const Aws::Vector<Aws::AutoScaling::Model::Activity> &activities = outcome.GetResult().GetActivities(); allActivities.insert(allActivities.end(), activities.begin(), activities.end()); nextToken = outcome.GetResult().GetNextToken(); } else { std::cerr << "Error with AutoScaling::DescribeScalingActivities. " << outcome.GetError().GetMessage() << std::endl; cleanupResources(groupName, templateName, autoScalingClient, ec2Client); return false; } } while (!nextToken.empty()); std::cout << "Found " << allActivities.size() << " activities." << std::endl; std::cout << "Activities are ordered with the most recent first." << std::endl; for (const Aws::AutoScaling::Model::Activity &activity: allActivities) { std::cout << activity.GetDescription() << std::endl; std::cout << activity.GetDetails() << std::endl; } } if (enableMetrics) { if (!logAutoScalingMetrics(groupName, clientConfig)) { cleanupResources(groupName, templateName, autoScalingClient, ec2Client); return false; } } std::cout << "Let's clean up." << std::endl; askQuestion("Press enter to continue: ", alwaysTrueTest); // 13. Disable metrics collection if enabled. if (enableMetrics) { Aws::AutoScaling::Model::DisableMetricsCollectionRequest request; request.SetAutoScalingGroupName(groupName); Aws::AutoScaling::Model::DisableMetricsCollectionOutcome outcome = autoScalingClient.DisableMetricsCollection(request); if (outcome.IsSuccess()) { std::cout << "Metrics collection has been disabled." << std::endl; } else { std::cerr << "Error with AutoScaling::DisableMetricsCollection. " << outcome.GetError().GetMessage() << std::endl; cleanupResources(groupName, templateName, autoScalingClient, ec2Client); return false; } } return cleanupResources(groupName, templateName, autoScalingClient, ec2Client); } //! Routine which waits for EC2 instances in an Auto Scaling group to //! complete startup or shutdown. /*! \sa waitForInstances() \param groupName: An Auto Scaling group name. \param autoScalingGroups: Vector to receive 'AutoScalingGroup' records. \param client: 'AutoScalingClient' instance. \return bool: Successful completion. */ bool AwsDoc::AutoScaling::waitForInstances(const Aws::String &groupName, Aws::Vector<Aws::AutoScaling::Model::AutoScalingGroup> &autoScalingGroups, const Aws::AutoScaling::AutoScalingClient &client) { bool ready = false; const std::vector<Aws::String> READY_STATES = {"InService", "Terminated"}; int count = 0; int desiredCapacity = 0; std::this_thread::sleep_for(std::chrono::seconds(4)); while (!ready) { if (WAIT_FOR_INSTANCES_TIMEOUT < count) { std::cerr << "Wait for instance timed out." << std::endl; return false; } std::this_thread::sleep_for(std::chrono::seconds(1)); ++count; if (!describeGroup(groupName, autoScalingGroups, client)) { return false; } Aws::Vector<Aws::String> instanceIDs; if (!autoScalingGroups.empty()) { instanceIDs = instancesToInstanceIDs(autoScalingGroups[0].GetInstances()); desiredCapacity = autoScalingGroups[0].GetDesiredCapacity(); } if (instanceIDs.empty()) { if (desiredCapacity == 0) { break; } else { if ((count % 5) == 0) { std::cout << "No instance IDs returned for group." << std::endl; } continue; } } // 6. Check lifecycle state of the instances using DescribeAutoScalingInstances. Aws::AutoScaling::Model::DescribeAutoScalingInstancesRequest request; request.SetInstanceIds(instanceIDs); Aws::AutoScaling::Model::DescribeAutoScalingInstancesOutcome outcome = client.DescribeAutoScalingInstances(request); if (outcome.IsSuccess()) { const Aws::Vector<Aws::AutoScaling::Model::AutoScalingInstanceDetails> &instancesDetails = outcome.GetResult().GetAutoScalingInstances(); ready = instancesDetails.size() >= desiredCapacity; for (const Aws::AutoScaling::Model::AutoScalingInstanceDetails &details: instancesDetails) { if (!stringInVector(details.GetLifecycleState(), READY_STATES)) { ready = false; break; } } // Log the status while waiting. if (((count % 5) == 1) || ready) { logInstancesLifecycleState(instancesDetails); } } else { std::cerr << "Error with AutoScaling::DescribeAutoScalingInstances. " << outcome.GetError().GetMessage() << std::endl; return false; } } if (!describeGroup(groupName, autoScalingGroups, client)) { return false; } return true; } //! Routine to cleanup resources created in 'groupsAndInstancesScenario'. /*! \sa cleanupResources() \param groupName: Optional Auto Scaling group name. \param templateName: Optional EC2 launch template name. \param autoScalingClient: 'AutoScalingClient' instance. \param ec2Client: 'EC2Client' instance. \return bool: Successful completion. */ bool AwsDoc::AutoScaling::cleanupResources(const Aws::String &groupName, const Aws::String &templateName, const Aws::AutoScaling::AutoScalingClient &autoScalingClient, const Aws::EC2::EC2Client &ec2Client) { bool result = true; // 14. Delete the Auto Scaling group. if (!groupName.empty() && (askYesNoQuestion( Aws::String("Delete the Auto Scaling group '") + groupName + "' (y/n)?"))) { { Aws::AutoScaling::Model::UpdateAutoScalingGroupRequest request; request.SetAutoScalingGroupName(groupName); request.SetMinSize(0); request.SetDesiredCapacity(0); Aws::AutoScaling::Model::UpdateAutoScalingGroupOutcome outcome = autoScalingClient.UpdateAutoScalingGroup(request); if (outcome.IsSuccess()) { std::cout << "The minimum size and desired capacity of the Auto Scaling group " << "was set to zero before terminating the instances." << std::endl; } else { std::cerr << "Error with AutoScaling::UpdateAutoScalingGroup. " << outcome.GetError().GetMessage() << std::endl; result = false; } } Aws::Vector<Aws::AutoScaling::Model::AutoScalingGroup> autoScalingGroups; if (AwsDoc::AutoScaling::describeGroup(groupName, autoScalingGroups, autoScalingClient)) { if (!autoScalingGroups.empty()) { Aws::Vector<Aws::String> instanceIDs = instancesToInstanceIDs( autoScalingGroups[0].GetInstances()); for (const Aws::String &instanceID: instanceIDs) { Aws::AutoScaling::Model::TerminateInstanceInAutoScalingGroupRequest request; request.SetInstanceId(instanceID); request.SetShouldDecrementDesiredCapacity(true); Aws::AutoScaling::Model::TerminateInstanceInAutoScalingGroupOutcome outcome = autoScalingClient.TerminateInstanceInAutoScalingGroup( request); if (outcome.IsSuccess()) { std::cout << "Initiating termination of EC2 instance '" << instanceID << "'." << std::endl; } else { std::cerr << "Error with AutoScaling::TerminateInstanceInAutoScalingGroup. " << outcome.GetError().GetMessage() << std::endl; result = false; } } } std::cout << "Waiting for the EC2 instances to terminate before deleting the " << "Auto Scaling group..." << std::endl; waitForInstances(groupName, autoScalingGroups, autoScalingClient); } { Aws::AutoScaling::Model::DeleteAutoScalingGroupRequest request; request.SetAutoScalingGroupName(groupName); Aws::AutoScaling::Model::DeleteAutoScalingGroupOutcome outcome = autoScalingClient.DeleteAutoScalingGroup(request); if (outcome.IsSuccess()) { std::cout << "Auto Scaling group '" << groupName << "' was deleted." << std::endl; } else { std::cerr << "Error with AutoScaling::DeleteAutoScalingGroup. " << outcome.GetError().GetMessage() << std::endl; result = false; } } } // 15. Delete the EC2 launch template. if (!templateName.empty() && (askYesNoQuestion( Aws::String("Delete the EC2 launch template '") + templateName + "' (y/n)?"))) { Aws::EC2::Model::DeleteLaunchTemplateRequest request; request.SetLaunchTemplateName(templateName); Aws::EC2::Model::DeleteLaunchTemplateOutcome outcome = ec2Client.DeleteLaunchTemplate(request); if (outcome.IsSuccess()) { std::cout << "EC2 launch template '" << templateName << "' was deleted." << std::endl; } else { std::cerr << "Error with EC2::DeleteLaunchTemplate. " << outcome.GetError().GetMessage() << std::endl; result = false; } } return result; } //! Routine which retrieves Auto Scaling group descriptions. /*! \sa describeGroup() \param groupName: An Auto Scaling group name. \param autoScalingGroups: Vector to receive 'AutoScalingGroup' records. \param client: 'AutoScalingClient' instance. \return bool: Successful completion. */ bool AwsDoc::AutoScaling::describeGroup(const Aws::String &groupName, Aws::Vector<Aws::AutoScaling::Model::AutoScalingGroup> &autoScalingGroup, const Aws::AutoScaling::AutoScalingClient &client) { // 5. Retrieve a description of the Auto Scaling group. Aws::AutoScaling::Model::DescribeAutoScalingGroupsRequest request; Aws::Vector<Aws::String> groupNames; groupNames.push_back(groupName); request.SetAutoScalingGroupNames(groupNames); Aws::AutoScaling::Model::DescribeAutoScalingGroupsOutcome outcome = client.DescribeAutoScalingGroups(request); if (outcome.IsSuccess()) { autoScalingGroup = outcome.GetResult().GetAutoScalingGroups(); } else { std::cerr << "Error with AutoScaling::DescribeAutoScalingGroups. " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }