SIGN IN SIGN UP

Welcome to the AWS Code Examples Repository. This repo contains code examples used in the AWS documentation, AWS SDK Developer Guides, and more. For more information, see the Readme.md file below.

0 0 12 Java
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
/**
* Before running this C++ code example, set up your development environment, including your credentials.
*
* For more information, see the following documentation topic:
*
* https://docs.aws.amazon.com/sdk-for-cpp/v1/developer-guide/getting-started.html
*
* For information on the structure of the code examples and how to build and run the examples, see
* https://docs.aws.amazon.com/sdk-for-cpp/v1/developer-guide/getting-started-code-examples.html.
*
**/
2021-11-15 13:15:43 +00:00
// snippet-start:[ec2.cpp.describe_instances.inc]
#include <aws/ec2/EC2Client.h>
#include <aws/ec2/model/DescribeInstancesRequest.h>
#include <aws/ec2/model/DescribeInstancesResponse.h>
#include <iomanip>
#include <iostream>
// snippet-end:[ec2.cpp.describe_instances.inc]
#include "ec2_samples.h"
2021-11-15 13:15:43 +00:00
2024-08-02 10:24:50 -04:00
// snippet-start:[cpp.example_code.ec2.DescribeInstances]
//! Describe all Amazon Elastic Compute Cloud (Amazon EC2) instances associated with an account.
/*!
\param clientConfiguration: AWS client configuration.
\return bool: Function succeeded.
2021-11-15 13:15:43 +00:00
*/
2024-08-02 10:24:50 -04:00
bool AwsDoc::EC2::describeInstances(
const Aws::Client::ClientConfiguration &clientConfiguration) {
// snippet-start:[ec2.cpp.describe_instances.code]
Aws::EC2::EC2Client ec2Client(clientConfiguration);
Aws::EC2::Model::DescribeInstancesRequest request;
bool header = false;
bool done = false;
while (!done) {
2024-08-02 10:24:50 -04:00
Aws::EC2::Model::DescribeInstancesOutcome 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;
}
2021-11-15 13:15:43 +00:00
const std::vector<Aws::EC2::Model::Reservation> &reservations =
2021-11-15 13:15:43 +00:00
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 =
2021-11-15 13:15:43 +00:00
Aws::EC2::Model::InstanceStateNameMapper::GetNameForInstanceStateName(
instance.GetState().GetName());
2021-11-15 13:15:43 +00:00
Aws::String typeString =
2021-11-15 13:15:43 +00:00
Aws::EC2::Model::InstanceTypeMapper::GetNameForInstanceType(
instance.GetInstanceType());
2021-11-15 13:15:43 +00:00
Aws::String monitorString =
2021-11-15 13:15:43 +00:00
Aws::EC2::Model::MonitoringStateMapper::GetNameForMonitoringState(
instance.GetMonitoring().GetState());
Aws::String name = "Unknown";
2021-11-15 13:15:43 +00:00
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) {
2024-08-02 10:24:50 -04:00
return tag.GetKey() == "Name";
});
if (nameIter != tags.cend()) {
name = nameIter->GetValue();
2021-11-15 13:15:43 +00:00
}
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;
2021-11-15 13:15:43 +00:00
}
}
2021-11-15 13:15:43 +00:00
if (!outcome.GetResult().GetNextToken().empty()) {
request.SetNextToken(outcome.GetResult().GetNextToken());
2024-08-02 10:24:50 -04:00
} else {
2021-11-15 13:15:43 +00:00
done = true;
}
2024-08-02 10:24:50 -04:00
} else {
std::cerr << "Failed to describe EC2 instances:" <<
outcome.GetError().GetMessage() << std::endl;
return false;
}
}
// snippet-end:[ec2.cpp.describe_instances.code]
return true;
}
2024-08-02 10:24:50 -04:00
// snippet-end:[cpp.example_code.ec2.DescribeInstances]
/*
*
* main function
*
* Usage: 'run_describe_instances'
*
*/
#ifndef TESTING_BUILD
int main(int argc, char **argv) {
(void) argc;
(void) argv;
Aws::SDKOptions options;
Aws::InitAPI(options);
{
Aws::Client::ClientConfiguration clientConfig;
// Optional: Set to the AWS Region (overrides config file).
// clientConfig.region = "us-east-1";
2024-08-02 10:24:50 -04:00
AwsDoc::EC2::describeInstances(clientConfig);
2021-11-15 13:15:43 +00:00
}
Aws::ShutdownAPI(options);
return 0;
}
#endif // TESTING_BUILD