Managing Amazon EC2 Instances - Amazon SDK for C++
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Managing Amazon EC2 Instances

Prerequisites

Before you begin, we recommend you read Getting started using the Amazon SDK for C++.

Download the example code and build the solution as described in Get started on code examples.

To run the examples, the user profile your code uses to make the requests must have proper permissions in Amazon (for the service and the action). For more information, see Providing Amazon credentials.

Create an Instance

Create a new Amazon EC2 instance by calling the EC2Client’s RunInstances function, providing it with a RunInstancesRequest containing the Amazon Machine Image (AMI) to use and an instance type.

Includes

#include <aws/core/Aws.h> #include <aws/ec2/EC2Client.h> #include <aws/ec2/model/CreateTagsRequest.h> #include <aws/ec2/model/RunInstancesRequest.h> #include <aws/ec2/model/RunInstancesResponse.h> #include <iostream>

Code

Aws::EC2::EC2Client ec2Client(clientConfiguration); Aws::EC2::Model::RunInstancesRequest runRequest; runRequest.SetImageId(amiId); runRequest.SetInstanceType(Aws::EC2::Model::InstanceType::t1_micro); runRequest.SetMinCount(1); runRequest.SetMaxCount(1); Aws::EC2::Model::RunInstancesOutcome runOutcome = ec2Client.RunInstances( runRequest); if (!runOutcome.IsSuccess()) { std::cerr << "Failed to launch EC2 instance " << instanceName << " based on ami " << amiId << ":" << runOutcome.GetError().GetMessage() << std::endl; return false; } const Aws::Vector<Aws::EC2::Model::Instance> &instances = runOutcome.GetResult().GetInstances(); if (instances.empty()) { std::cerr << "Failed to launch EC2 instance " << instanceName << " based on ami " << amiId << ":" << runOutcome.GetError().GetMessage() << std::endl; return false; }

See the complete example.

Start an Instance

To start an Amazon EC2 instance, call the EC2Client’s StartInstances function, providing it with a StartInstancesRequest containing the ID of the instance to start.

Includes

#include <aws/core/Aws.h> #include <aws/ec2/EC2Client.h> #include <aws/ec2/model/StartInstancesRequest.h> #include <aws/ec2/model/StartInstancesResponse.h>

Code

Aws::EC2::EC2Client ec2Client(clientConfiguration); Aws::EC2::Model::StartInstancesRequest start_request; start_request.AddInstanceIds(instanceId); start_request.SetDryRun(true); auto dry_run_outcome = ec2Client.StartInstances(start_request); if (dry_run_outcome.IsSuccess()) { std::cerr << "Failed dry run to start instance. A dry run should trigger an error." << std::endl; return false; } else if (dry_run_outcome.GetError().GetErrorType() != Aws::EC2::EC2Errors::DRY_RUN_OPERATION) { std::cout << "Failed dry run to start instance " << instanceId << ": " << dry_run_outcome.GetError().GetMessage() << std::endl; return false; } start_request.SetDryRun(false); auto start_instancesOutcome = ec2Client.StartInstances(start_request); if (!start_instancesOutcome.IsSuccess()) { std::cout << "Failed to start instance " << instanceId << ": " << start_instancesOutcome.GetError().GetMessage() << std::endl; } else { std::cout << "Successfully started instance " << instanceId << std::endl; }

See the complete example.

Stop an Instance

To stop an Amazon EC2 instance, call the EC2Client’s StopInstances function, providing it with a StopInstancesRequest containing the ID of the instance to stop.

Includes

#include <aws/ec2/model/StopInstancesRequest.h> #include <aws/ec2/model/StopInstancesResponse.h>

Code

Aws::EC2::EC2Client ec2Client(clientConfiguration); Aws::EC2::Model::StopInstancesRequest request; request.AddInstanceIds(instanceId); request.SetDryRun(true); auto dry_run_outcome = ec2Client.StopInstances(request); if (dry_run_outcome.IsSuccess()) { std::cerr << "Failed dry run to stop instance. A dry run should trigger an error." << std::endl; return false; } else if (dry_run_outcome.GetError().GetErrorType() != Aws::EC2::EC2Errors::DRY_RUN_OPERATION) { std::cout << "Failed dry run to stop instance " << instanceId << ": " << dry_run_outcome.GetError().GetMessage() << std::endl; return false; } request.SetDryRun(false); auto outcome = ec2Client.StopInstances(request); if (!outcome.IsSuccess()) { std::cout << "Failed to stop instance " << instanceId << ": " << outcome.GetError().GetMessage() << std::endl; } else { std::cout << "Successfully stopped instance " << instanceId << std::endl; }

See the complete example.

Reboot an Instance

To reboot an Amazon EC2 instance, call the EC2Client’s RebootInstances function, providing it with a RebootInstancesRequest containing the ID of the instance to reboot.

Includes

#include <aws/core/Aws.h> #include <aws/ec2/EC2Client.h> #include <aws/ec2/model/RebootInstancesRequest.h> #include <iostream>

Code

Aws::EC2::EC2Client ec2Client(clientConfiguration); Aws::EC2::Model::RebootInstancesRequest request; request.AddInstanceIds(instanceId); request.SetDryRun(true); auto dry_run_outcome = ec2Client.RebootInstances(request); if (dry_run_outcome.IsSuccess()) { std::cerr << "Failed dry run to reboot on instance. A dry run should trigger an error." << std::endl; return false; } else 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 false; } request.SetDryRun(false); auto outcome = ec2Client.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; }

See the complete example.

Describe Instances

To list your instances, create a DescribeInstancesRequest and call the EC2Client’s DescribeInstances function. It will return a DescribeInstancesResponse object that you can use to list the Amazon EC2 instances for your Amazon Web Services account and Amazon Web Services Region.

Instances are grouped by reservation. Each reservation corresponds to the call to StartInstances that launched the instance. To list your instances, you must first call the DescribeInstancesResponse class’ GetReservations function, and then call getInstances on each returned Reservation object.

Includes

#include <aws/core/Aws.h> #include <aws/ec2/EC2Client.h> #include <aws/ec2/model/DescribeInstancesRequest.h> #include <aws/ec2/model/DescribeInstancesResponse.h> #include <iomanip> #include <iostream>

Code

Aws::EC2::EC2Client ec2Client(clientConfiguration); Aws::EC2::Model::DescribeInstancesRequest request; bool header = false; bool done = false; while (!done) { auto outcome = ec2Client.DescribeInstances(request); if (outcome.IsSuccess()) { if (!header) { std::cout << std::left << std::setw(48) << "Name" << std::setw(20) << "ID" << std::setw(25) << "Ami" << std::setw(15) << "Type" << std::setw(15) << "State" << std::setw(15) << "Monitoring" << std::endl; header = true; } const std::vector<Aws::EC2::Model::Reservation> &reservations = outcome.GetResult().GetReservations(); for (const auto &reservation: reservations) { const std::vector<Aws::EC2::Model::Instance> &instances = reservation.GetInstances(); for (const auto &instance: instances) { Aws::String instanceStateString = Aws::EC2::Model::InstanceStateNameMapper::GetNameForInstanceStateName( instance.GetState().GetName()); Aws::String typeString = Aws::EC2::Model::InstanceTypeMapper::GetNameForInstanceType( instance.GetInstanceType()); Aws::String monitorString = Aws::EC2::Model::MonitoringStateMapper::GetNameForMonitoringState( instance.GetMonitoring().GetState()); Aws::String name = "Unknown"; const std::vector<Aws::EC2::Model::Tag> &tags = instance.GetTags(); auto nameIter = std::find_if(tags.cbegin(), tags.cend(), [](const Aws::EC2::Model::Tag &tag) { return tag.GetKey() == "Name"; }); if (nameIter != tags.cend()) { name = nameIter->GetValue(); } std::cout << std::setw(48) << name << std::setw(20) << instance.GetInstanceId() << std::setw(25) << instance.GetImageId() << std::setw(15) << typeString << std::setw(15) << instanceStateString << std::setw(15) << monitorString << std::endl; } } if (!outcome.GetResult().GetNextToken().empty()) { request.SetNextToken(outcome.GetResult().GetNextToken()); } else { done = true; } } else { std::cerr << "Failed to describe EC2 instances:" << outcome.GetError().GetMessage() << std::endl; return false; } }

Results are paged; you can get further results by passing the value returned from the result object’s GetNextToken function to your original request object’s SetNextToken function, then using the same request object in your next call to DescribeInstances.

See the complete example.

Enable Instance Monitoring

You can monitor various aspects of your Amazon EC2 instances, such as CPU and network utilization, available memory, and disk space remaining. To learn more about instance monitoring, see Monitoring Amazon EC2 in the Amazon EC2 User Guide.

To start monitoring an instance, you must create a MonitorInstancesRequest with the ID of the instance to monitor, and pass it to the EC2Client’s MonitorInstances function.

Includes

#include <aws/core/Aws.h> #include <aws/ec2/EC2Client.h> #include <aws/ec2/model/MonitorInstancesRequest.h> #include <aws/ec2/model/UnmonitorInstancesRequest.h> #include <aws/ec2/model/UnmonitorInstancesResponse.h> #include <iostream>

Code

Aws::EC2::EC2Client ec2Client(clientConfiguration); Aws::EC2::Model::MonitorInstancesRequest request; request.AddInstanceIds(instanceId); request.SetDryRun(true); auto dry_run_outcome = ec2Client.MonitorInstances(request); if (dry_run_outcome.IsSuccess()) { std::cerr << "Failed dry run to enable monitoring on instance. A dry run should trigger an error." << std::endl; return false; } else if (dry_run_outcome.GetError().GetErrorType() != Aws::EC2::EC2Errors::DRY_RUN_OPERATION) { std::cerr << "Failed dry run to enable monitoring on instance " << instanceId << ": " << dry_run_outcome.GetError().GetMessage() << std::endl; return false; } request.SetDryRun(false); auto monitorInstancesOutcome = ec2Client.MonitorInstances(request); if (!monitorInstancesOutcome.IsSuccess()) { std::cerr << "Failed to enable monitoring on instance " << instanceId << ": " << monitorInstancesOutcome.GetError().GetMessage() << std::endl; } else { std::cout << "Successfully enabled monitoring on instance " << instanceId << std::endl; }

See the complete example.

Disable Instance Monitoring

To stop monitoring an instance, create an UnmonitorInstancesRequest with the ID of the instance to stop monitoring, and pass it to the EC2Client’s UnmonitorInstances function.

Includes

#include <aws/core/Aws.h> #include <aws/ec2/EC2Client.h> #include <aws/ec2/model/MonitorInstancesRequest.h> #include <aws/ec2/model/UnmonitorInstancesRequest.h> #include <aws/ec2/model/UnmonitorInstancesResponse.h> #include <iostream>

Code

Aws::EC2::EC2Client ec2Client(clientConfiguration); Aws::EC2::Model::UnmonitorInstancesRequest unrequest; unrequest.AddInstanceIds(instanceId); unrequest.SetDryRun(true); auto undryRunOutcome = ec2Client.UnmonitorInstances(unrequest); if (undryRunOutcome.IsSuccess()) { std::cerr << "Failed dry run to disable monitoring on instance. A dry run should trigger an error." << std::endl; return false; } else if (undryRunOutcome.GetError().GetErrorType() != Aws::EC2::EC2Errors::DRY_RUN_OPERATION) { std::cout << "Failed dry run to disable monitoring on instance " << instanceId << ": " << undryRunOutcome.GetError().GetMessage() << std::endl; return false; } unrequest.SetDryRun(false); auto unmonitorInstancesOutcome = ec2Client.UnmonitorInstances(unrequest); if (!unmonitorInstancesOutcome.IsSuccess()) { std::cout << "Failed to disable monitoring on instance " << instanceId << ": " << unmonitorInstancesOutcome.GetError().GetMessage() << std::endl; } else { std::cout << "Successfully disable monitoring on instance " << instanceId << std::endl; }

See the complete example.

More Information