使用 SDK for C++ 的 Auto Scaling 範例 - AWS SDK 程式碼範例

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

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

使用 SDK for C++ 的 Auto Scaling 範例

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

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

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

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

開始使用

下列程式碼範例示範如何開始使用 Auto Scaling。

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

基本概念

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

  • 使用啟動範本和可用區域建立 Amazon EC2 Auto Scaling 群組,並取得執行執行個體的相關資訊。

  • 啟用 Amazon CloudWatch 指標集合。

  • 更新群組所需的容量,並等待執行個體啟動。

  • 終止群組中的執行個體。

  • 列出因應使用者請求和容量變更而發生的擴展活動。

  • 取得 CloudWatch 指標的統計資料,然後清除資源。

C++ 的 SDK
注意

還有更多 on 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(); }

動作

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

C++ 的 SDK
注意

還有更多 on 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; }

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

C++ 的 SDK
注意

還有更多 on 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; } }

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

C++ 的 SDK
注意

還有更多 on 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; }

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

C++ 的 SDK
注意

還有更多 on 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; }

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

C++ 的 SDK
注意

還有更多 on 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; }

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

C++ 的 SDK
注意

還有更多 on 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; }

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

C++ 的 SDK
注意

還有更多 on 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; }

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

C++ 的 SDK
注意

還有更多 on 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; }

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

C++ 的 SDK
注意

還有更多 on 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

C++ 的 SDK
注意

還有更多 on 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; }