AWS Code Sample
Catalog

reboot_instance.cpp

reboot_instance.cpp demonstrates how to reboot an Amazon EC2 instance.

/* Copyright 2010-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. This file is licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at http://aws.amazon.com/apache2.0/ This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #include <aws/core/Aws.h> #include <aws/ec2/EC2Client.h> #include <aws/ec2/model/RebootInstancesRequest.h> #include <iostream> void RebootInstance(const Aws::String& instanceId) { Aws::EC2::EC2Client ec2; Aws::EC2::Model::RebootInstancesRequest request; request.AddInstanceIds(instanceId); request.SetDryRun(true); auto dry_run_outcome = ec2.RebootInstances(request); assert(!dry_run_outcome.IsSuccess()); if (dry_run_outcome.GetError().GetErrorType() != Aws::EC2::EC2Errors::DRY_RUN_OPERATION) { std::cout << "Failed dry run to reboot instance " << instanceId << ": " << dry_run_outcome.GetError().GetMessage() << std::endl; return; } request.SetDryRun(false); auto outcome = ec2.RebootInstances(request); if (!outcome.IsSuccess()) { std::cout << "Failed to reboot instance " << instanceId << ": " << outcome.GetError().GetMessage() << std::endl; } else { std::cout << "Successfully rebooted instance " << instanceId << std::endl; } } /** * Reboots an ec2 instance based on command line input */ int main(int argc, char** argv) { if (argc != 2) { std::cout << "Usage: reboot_instance <instance_id>" << std::endl; return 1; } Aws::SDKOptions options; Aws::InitAPI(options); { Aws::String instanceId = argv[1]; RebootInstance(instanceId); } Aws::ShutdownAPI(options); return 0; }

Sample Details

Service: ec2

Author: AWS

Type: full-example

On this page: