Weitere AWS SDK Beispiele sind im Repo AWS Doc SDK Examples
Die vorliegende Übersetzung wurde maschinell erstellt. Im Falle eines Konflikts oder eines Widerspruchs zwischen dieser übersetzten Fassung und der englischen Fassung (einschließlich infolge von Verzögerungen bei der Übersetzung) ist die englische Fassung maßgeblich.
Auto Scaling Scaling-Beispiele SDK für die Verwendung von für C++
Die folgenden Codebeispiele zeigen Ihnen, wie Sie AWS SDK for C++ mit Auto Scaling Aktionen ausführen und allgemeine Szenarien implementieren.
Basics sind Codebeispiele, die Ihnen zeigen, wie Sie die wesentlichen Operationen innerhalb eines Dienstes ausführen.
Aktionen sind Codeauszüge aus größeren Programmen und müssen im Kontext ausgeführt werden. Aktionen zeigen Ihnen zwar, wie Sie einzelne Servicefunktionen aufrufen, aber Sie können Aktionen im Kontext der zugehörigen Szenarien sehen.
Jedes Beispiel enthält einen Link zum vollständigen Quellcode, in dem Sie Anweisungen zum Einrichten und Ausführen des Codes im Kontext finden.
Erste Schritte
Die folgenden Codebeispiele zeigen, wie Sie mit Auto Scaling beginnen können.
- SDKfür C++
-
Anmerkung
Es gibt noch mehr dazu GitHub. Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository
einrichten und ausführen. Code für die CMakeLists CMake .txt-Datei.
# 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})
Code für die Quelldatei 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; }
-
APIEinzelheiten finden Sie DescribeAutoScalingGroupsunter AWS SDK for C++ APIReferenz.
-
Themen
Grundlagen
Wie das aussehen kann, sehen Sie am nachfolgenden Beispielcode:
Erstellen Sie eine Amazon EC2 Auto Scaling Scaling-Gruppe mit einer Startvorlage und Availability Zones und erhalten Sie Informationen über laufende Instances.
Aktivieren Sie die Erfassung von CloudWatch Amazon-Metriken.
Aktualisieren Sie die gewünschte Kapazität der Gruppe und warten Sie, bis eine Instance gestartet wird.
Beenden Sie eine Instanz in der Gruppe.
Listet Skalierungsaktivitäten auf, die als Reaktion auf Benutzeranfragen und Kapazitätsänderungen erfolgen.
Holen Sie sich Statistiken für CloudWatch Metriken und bereinigen Sie dann Ressourcen.
- SDKfür C++
-
Anmerkung
Es gibt noch mehr dazu GitHub. Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository
einrichten und ausführen. //! 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(); }
-
APIEinzelheiten finden Sie unter den folgenden Themen in der AWS SDK for C++ APIReferenz.
-
Aktionen
Das folgende Codebeispiel zeigt die VerwendungCreateAutoScalingGroup
.
- SDKfür C++
-
Anmerkung
Es gibt noch mehr dazu GitHub. Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository
einrichten und ausführen. 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; }
-
APIEinzelheiten finden Sie CreateAutoScalingGroupunter AWS SDK for C++ APIReferenz.
-
Das folgende Codebeispiel zeigt die VerwendungDeleteAutoScalingGroup
.
- SDKfür C++
-
Anmerkung
Es gibt noch mehr dazu GitHub. Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository
einrichten und ausführen. 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; } }
-
APIEinzelheiten finden Sie DeleteAutoScalingGroupunter AWS SDK for C++ APIReferenz.
-
Das folgende Codebeispiel zeigt die VerwendungDescribeAutoScalingGroups
.
- SDKfür C++
-
Anmerkung
Es gibt noch mehr dazu GitHub. Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository
einrichten und ausführen. 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; }
-
APIEinzelheiten finden Sie DescribeAutoScalingGroupsunter AWS SDK for C++ APIReferenz.
-
Das folgende Codebeispiel zeigt die VerwendungDescribeAutoScalingInstances
.
- SDKfür C++
-
Anmerkung
Es gibt noch mehr dazu GitHub. Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository
einrichten und ausführen. 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; }
-
APIEinzelheiten finden Sie DescribeAutoScalingInstancesunter AWS SDK for C++ APIReferenz.
-
Das folgende Codebeispiel zeigt die VerwendungDescribeScalingActivities
.
- SDKfür C++
-
Anmerkung
Es gibt noch mehr dazu GitHub. Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository
einrichten und ausführen. 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; }
-
APIEinzelheiten finden Sie DescribeScalingActivitiesunter AWS SDK for C++ APIReferenz.
-
Das folgende Codebeispiel zeigt die VerwendungDisableMetricsCollection
.
- SDKfür C++
-
Anmerkung
Es gibt noch mehr dazu GitHub. Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository
einrichten und ausführen. 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; }
-
APIEinzelheiten finden Sie DisableMetricsCollectionunter AWS SDK for C++ APIReferenz.
-
Das folgende Codebeispiel zeigt die VerwendungEnableMetricsCollection
.
- SDKfür C++
-
Anmerkung
Es gibt noch mehr dazu GitHub. Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository
einrichten und ausführen. 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; }
-
APIEinzelheiten finden Sie EnableMetricsCollectionunter AWS SDK for C++ APIReferenz.
-
Das folgende Codebeispiel zeigt die VerwendungSetDesiredCapacity
.
- SDKfür C++
-
Anmerkung
Es gibt noch mehr dazu GitHub. Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository
einrichten und ausführen. 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; }
-
APIEinzelheiten finden Sie SetDesiredCapacityunter AWS SDK for C++ APIReferenz.
-
Das folgende Codebeispiel zeigt die VerwendungTerminateInstanceInAutoScalingGroup
.
- SDKfür C++
-
Anmerkung
Es gibt noch mehr dazu GitHub. Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository
einrichten und ausführen. 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; }
-
APIEinzelheiten finden Sie TerminateInstanceInAutoScalingGroupunter AWS SDK for C++ APIReferenz.
-
Das folgende Codebeispiel zeigt die VerwendungUpdateAutoScalingGroup
.
- SDKfür C++
-
Anmerkung
Es gibt noch mehr dazu GitHub. Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository
einrichten und ausführen. 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; }
-
APIEinzelheiten finden Sie UpdateAutoScalingGroupunter AWS SDK for C++ APIReferenz.
-