使用 C++ SDK 的亚马逊EC2示例 - AWS SDK代码示例

AWS 文档 AWS SDK示例 GitHub 存储库中还有更多SDK示例

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

使用 C++ SDK 的亚马逊EC2示例

以下代码示例向您展示了如何通过 AWS SDK for C++ 与 Amazon 一起使用来执行操作和实现常见场景EC2。

操作是大型程序的代码摘录,必须在上下文中运行。您可以通过操作了解如何调用单个服务函数,还可以通过函数相关场景的上下文查看操作。

每个示例都包含一个指向完整源代码的链接,您可以在其中找到有关如何在上下文中设置和运行代码的说明。

开始使用

以下代码示例展示了如何开始使用 Amazon EC2。

SDK对于 C++
注意

还有更多相关信息 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 ec2) # Set this project's name. project("hello_ec2") # 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_ec2.cpp) target_link_libraries(${PROJECT_NAME} ${AWSSDK_LINK_LIBRARIES})

hello_ec2.cpp 源文件的代码。

#include <aws/core/Aws.h> #include <aws/ec2/EC2Client.h> #include <aws/ec2/model/DescribeInstancesRequest.h> #include <iomanip> #include <iostream> /* * A "Hello EC2" starter application which initializes an Amazon Elastic Compute Cloud (Amazon EC2) client and describes * the Amazon EC2 instances. * * main function * * Usage: 'hello_ec2' * */ int main(int argc, char **argv) { (void)argc; (void)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::EC2::EC2Client ec2Client(clientConfig); Aws::EC2::Model::DescribeInstancesRequest request; bool header = false; bool done = false; while (!done) { 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; } 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; result = 1; break; } } } Aws::ShutdownAPI(options); // Should only be called once. return result; }
主题

操作

以下代码示例显示了如何使用AllocateAddress

SDK对于 C++
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

//! Allocate an Elastic IP address and associate it with an Amazon Elastic Compute Cloud //! (Amazon EC2) instance. /*! \param instanceID: An EC2 instance ID. \param[out] publicIPAddress: String to return the public IP address. \param[out] allocationID: String to return the allocation ID. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::EC2::allocateAndAssociateAddress(const Aws::String &instanceId, Aws::String &publicIPAddress, Aws::String &allocationID, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::EC2::EC2Client ec2Client(clientConfiguration); Aws::EC2::Model::AllocateAddressRequest request; request.SetDomain(Aws::EC2::Model::DomainType::vpc); const Aws::EC2::Model::AllocateAddressOutcome outcome = ec2Client.AllocateAddress(request); if (!outcome.IsSuccess()) { std::cerr << "Failed to allocate Elastic IP address:" << outcome.GetError().GetMessage() << std::endl; return false; } const Aws::EC2::Model::AllocateAddressResponse &response = outcome.GetResult(); allocationID = response.GetAllocationId(); publicIPAddress = response.GetPublicIp(); return true; }
  • 有关API详细信息,请参阅 “AWS SDK for C++ API参考 AllocateAddress” 中的。

以下代码示例显示了如何使用AssociateAddress

SDK对于 C++
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

Aws::EC2::EC2Client ec2Client(clientConfiguration); //! Associate an Elastic IP address with an EC2 instance. /*! \param instanceId: An EC2 instance ID. \param allocationId: An Elastic IP allocation ID. \param[out] associationID: String to receive the association ID. \param clientConfiguration: AWS client configuration. \return bool: True if the address was associated with the instance; otherwise, false. */ bool AwsDoc::EC2::associateAddress(const Aws::String &instanceId, const Aws::String &allocationId, Aws::String &associationID, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::EC2::EC2Client ec2Client(clientConfiguration); Aws::EC2::Model::AssociateAddressRequest request; request.SetInstanceId(instanceId); request.SetAllocationId(allocationId); Aws::EC2::Model::AssociateAddressOutcome outcome = ec2Client.AssociateAddress(request); if (!outcome.IsSuccess()) { std::cerr << "Failed to associate address " << allocationId << " with instance " << instanceId << ": " << outcome.GetError().GetMessage() << std::endl; } else { std::cout << "Successfully associated address " << allocationId << " with instance " << instanceId << std::endl; associationID = outcome.GetResult().GetAssociationId(); } return outcome.IsSuccess(); }
  • 有关API详细信息,请参阅 “AWS SDK for C++ API参考 AssociateAddress” 中的。

以下代码示例显示了如何使用AuthorizeSecurityGroupIngress

SDK对于 C++
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

//! Authorize ingress to an Amazon Elastic Compute Cloud (Amazon EC2) group. /*! \param groupID: The EC2 group ID. \param clientConfiguration: The ClientConfiguration object. \return bool: True if the operation was successful, false otherwise. */ bool AwsDoc::EC2::authorizeSecurityGroupIngress(const Aws::String &groupID, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::EC2::EC2Client ec2Client(clientConfiguration); Aws::EC2::Model::AuthorizeSecurityGroupIngressRequest authorizeSecurityGroupIngressRequest; authorizeSecurityGroupIngressRequest.SetGroupId(groupID); buildSampleIngressRule(authorizeSecurityGroupIngressRequest); Aws::EC2::Model::AuthorizeSecurityGroupIngressOutcome authorizeSecurityGroupIngressOutcome = ec2Client.AuthorizeSecurityGroupIngress(authorizeSecurityGroupIngressRequest); if (authorizeSecurityGroupIngressOutcome.IsSuccess()) { std::cout << "Successfully authorized security group ingress." << std::endl; } else { std::cerr << "Error authorizing security group ingress: " << authorizeSecurityGroupIngressOutcome.GetError().GetMessage() << std::endl; } return authorizeSecurityGroupIngressOutcome.IsSuccess(); }

用于构建入口规则的实用函数。

//! Build a sample ingress rule. /*! \param authorize_request: An 'AuthorizeSecurityGroupIngressRequest' instance. \return void: */ void buildSampleIngressRule( Aws::EC2::Model::AuthorizeSecurityGroupIngressRequest &authorize_request) { Aws::String ingressIPRange = "203.0.113.0/24"; // Configure this for your allowed IP range. Aws::EC2::Model::IpRange ip_range; ip_range.SetCidrIp(ingressIPRange); Aws::EC2::Model::IpPermission permission1; permission1.SetIpProtocol("tcp"); permission1.SetToPort(80); permission1.SetFromPort(80); permission1.AddIpRanges(ip_range); authorize_request.AddIpPermissions(permission1); Aws::EC2::Model::IpPermission permission2; permission2.SetIpProtocol("tcp"); permission2.SetToPort(22); permission2.SetFromPort(22); permission2.AddIpRanges(ip_range); authorize_request.AddIpPermissions(permission2); }

以下代码示例显示了如何使用CreateKeyPair

SDK对于 C++
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

//! Create an Amazon Elastic Compute Cloud (Amazon EC2) instance key pair. /*! \param keyPairName: A name for a key pair. \param keyFilePath: File path where the credentials are stored. Ignored if it is an empty string; \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::EC2::createKeyPair(const Aws::String &keyPairName, const Aws::String &keyFilePath, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::EC2::EC2Client ec2Client(clientConfiguration); Aws::EC2::Model::CreateKeyPairRequest request; request.SetKeyName(keyPairName); Aws::EC2::Model::CreateKeyPairOutcome outcome = ec2Client.CreateKeyPair(request); if (!outcome.IsSuccess()) { std::cerr << "Failed to create key pair - " << keyPairName << ". " << outcome.GetError().GetMessage() << std::endl; } else { std::cout << "Successfully created key pair named " << keyPairName << std::endl; if (!keyFilePath.empty()) { std::ofstream keyFile(keyFilePath.c_str()); keyFile << outcome.GetResult().GetKeyMaterial(); keyFile.close(); std::cout << "Keys written to the file " << keyFilePath << std::endl; } } return outcome.IsSuccess(); }
  • 有关API详细信息,请参阅 “AWS SDK for C++ API参考 CreateKeyPair” 中的。

以下代码示例显示了如何使用CreateSecurityGroup

SDK对于 C++
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

//! Create a security group. /*! \param groupName: A security group name. \param description: A description. \param vpcID: A virtual private cloud (VPC) ID. \param[out] groupIDResult: A string to receive the group ID. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::EC2::createSecurityGroup(const Aws::String &groupName, const Aws::String &description, const Aws::String &vpcID, Aws::String &groupIDResult, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::EC2::EC2Client ec2Client(clientConfiguration); Aws::EC2::Model::CreateSecurityGroupRequest request; request.SetGroupName(groupName); request.SetDescription(description); request.SetVpcId(vpcID); const Aws::EC2::Model::CreateSecurityGroupOutcome outcome = ec2Client.CreateSecurityGroup(request); if (!outcome.IsSuccess()) { std::cerr << "Failed to create security group:" << outcome.GetError().GetMessage() << std::endl; return false; } std::cout << "Successfully created security group named " << groupName << std::endl; groupIDResult = outcome.GetResult().GetGroupId(); return true; }
  • 有关API详细信息,请参阅 “AWS SDK for C++ API参考 CreateSecurityGroup” 中的。

以下代码示例显示了如何使用CreateTags

SDK对于 C++
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

//! Add or overwrite only the specified tags for the specified Amazon Elastic Compute Cloud (Amazon EC2) resource or resources. /*! \param resources: The resources for the tags. \param tags: Vector of tags. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::EC2::createTags(const Aws::Vector<Aws::String> &resources, const Aws::Vector<Aws::EC2::Model::Tag> &tags, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::EC2::EC2Client ec2Client(clientConfiguration); Aws::EC2::Model::CreateTagsRequest createTagsRequest; createTagsRequest.SetResources(resources); createTagsRequest.SetTags(tags); Aws::EC2::Model::CreateTagsOutcome outcome = ec2Client.CreateTags(createTagsRequest); if (outcome.IsSuccess()) { std::cout << "Successfully created tags for resources" << std::endl; } else { std::cerr << "Failed to create tags for resources, " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }
  • 有关API详细信息,请参阅 “AWS SDK for C++ API参考 CreateTags” 中的。

以下代码示例显示了如何使用DeleteKeyPair

SDK对于 C++
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

//! Delete an Amazon Elastic Compute Cloud (Amazon EC2) instance key pair. /*! \param keyPairName: A name for a key pair. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::EC2::deleteKeyPair(const Aws::String &keyPairName, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::EC2::EC2Client ec2Client(clientConfiguration); Aws::EC2::Model::DeleteKeyPairRequest request; request.SetKeyName(keyPairName); const Aws::EC2::Model::DeleteKeyPairOutcome outcome = ec2Client.DeleteKeyPair( request); if (!outcome.IsSuccess()) { std::cerr << "Failed to delete key pair " << keyPairName << ":" << outcome.GetError().GetMessage() << std::endl; } else { std::cout << "Successfully deleted key pair named " << keyPairName << std::endl; } return outcome.IsSuccess(); }
  • 有关API详细信息,请参阅 “AWS SDK for C++ API参考 DeleteKeyPair” 中的。

以下代码示例显示了如何使用DeleteSecurityGroup

SDK对于 C++
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

//! Delete a security group. /*! \param securityGroupID: A security group ID. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::EC2::deleteSecurityGroup(const Aws::String &securityGroupID, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::EC2::EC2Client ec2Client(clientConfiguration); Aws::EC2::Model::DeleteSecurityGroupRequest request; request.SetGroupId(securityGroupID); Aws::EC2::Model::DeleteSecurityGroupOutcome outcome = ec2Client.DeleteSecurityGroup(request); if (!outcome.IsSuccess()) { std::cerr << "Failed to delete security group " << securityGroupID << ":" << outcome.GetError().GetMessage() << std::endl; } else { std::cout << "Successfully deleted security group " << securityGroupID << std::endl; } return outcome.IsSuccess(); }
  • 有关API详细信息,请参阅 “AWS SDK for C++ API参考 DeleteSecurityGroup” 中的。

以下代码示例显示了如何使用DescribeAddresses

SDK对于 C++
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

//! Describe all Elastic IP addresses. /*! \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::EC2::describeAddresses( const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::EC2::EC2Client ec2Client(clientConfiguration); Aws::EC2::Model::DescribeAddressesRequest request; Aws::EC2::Model::DescribeAddressesOutcome outcome = ec2Client.DescribeAddresses(request); if (outcome.IsSuccess()) { std::cout << std::left << std::setw(20) << "InstanceId" << std::setw(15) << "Public IP" << std::setw(10) << "Domain" << std::setw(30) << "Allocation ID" << std::setw(25) << "NIC ID" << std::endl; const Aws::Vector<Aws::EC2::Model::Address> &addresses = outcome.GetResult().GetAddresses(); for (const auto &address: addresses) { Aws::String domainString = Aws::EC2::Model::DomainTypeMapper::GetNameForDomainType( address.GetDomain()); std::cout << std::left << std::setw(20) << address.GetInstanceId() << std::setw(15) << address.GetPublicIp() << std::setw(10) << domainString << std::setw(30) << address.GetAllocationId() << std::setw(25) << address.GetNetworkInterfaceId() << std::endl; } } else { std::cerr << "Failed to describe Elastic IP addresses:" << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }
  • 有关API详细信息,请参阅 “AWS SDK for C++ API参考 DescribeAddresses” 中的。

以下代码示例显示了如何使用DescribeAvailabilityZones

SDK对于 C++
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

//! DescribeAvailabilityZones /*! \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ int AwsDoc::EC2::describeAvailabilityZones(const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::EC2::EC2Client ec2Client(clientConfiguration); Aws::EC2::Model::DescribeAvailabilityZonesRequest request; Aws::EC2::Model::DescribeAvailabilityZonesOutcome outcome = ec2Client.DescribeAvailabilityZones(request); if (outcome.IsSuccess()) { std::cout << std::left << std::setw(32) << "ZoneName" << std::setw(20) << "State" << std::setw(32) << "Region" << std::endl; const auto &zones = outcome.GetResult().GetAvailabilityZones(); for (const auto &zone: zones) { Aws::String stateString = Aws::EC2::Model::AvailabilityZoneStateMapper::GetNameForAvailabilityZoneState( zone.GetState()); std::cout << std::left << std::setw(32) << zone.GetZoneName() << std::setw(20) << stateString << std::setw(32) << zone.GetRegionName() << std::endl; } } else { std::cerr << "Failed to describe availability zones:" << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }

以下代码示例显示了如何使用DescribeInstances

SDK对于 C++
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

//! Describe all Amazon Elastic Compute Cloud (Amazon EC2) instances associated with an account. /*! \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::EC2::describeInstances( const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::EC2::EC2Client ec2Client(clientConfiguration); Aws::EC2::Model::DescribeInstancesRequest request; bool header = false; bool done = false; while (!done) { 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; } 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; } } return true; }
  • 有关API详细信息,请参阅 “AWS SDK for C++ API参考 DescribeInstances” 中的。

以下代码示例显示了如何使用DescribeKeyPairs

SDK对于 C++
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

//! Describe all Amazon Elastic Compute Cloud (Amazon EC2) instance key pairs. /*! \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::EC2::describeKeyPairs( const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::EC2::EC2Client ec2Client(clientConfiguration); Aws::EC2::Model::DescribeKeyPairsRequest request; Aws::EC2::Model::DescribeKeyPairsOutcome outcome = ec2Client.DescribeKeyPairs(request); if (outcome.IsSuccess()) { std::cout << std::left << std::setw(32) << "Name" << std::setw(64) << "Fingerprint" << std::endl; const std::vector<Aws::EC2::Model::KeyPairInfo> &key_pairs = outcome.GetResult().GetKeyPairs(); for (const auto &key_pair: key_pairs) { std::cout << std::left << std::setw(32) << key_pair.GetKeyName() << std::setw(64) << key_pair.GetKeyFingerprint() << std::endl; } } else { std::cerr << "Failed to describe key pairs:" << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }
  • 有关API详细信息,请参阅 “AWS SDK for C++ API参考 DescribeKeyPairs” 中的。

以下代码示例显示了如何使用DescribeRegions

SDK对于 C++
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

//! Describe all Amazon Elastic Compute Cloud (Amazon EC2) Regions. /*! \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::EC2::describeRegions( const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::EC2::EC2Client ec2Client(clientConfiguration); Aws::EC2::Model::DescribeRegionsRequest request; Aws::EC2::Model::DescribeRegionsOutcome outcome = ec2Client.DescribeRegions(request); if (outcome.IsSuccess()) { std::cout << std::left << std::setw(32) << "RegionName" << std::setw(64) << "Endpoint" << std::endl; const auto &regions = outcome.GetResult().GetRegions(); for (const auto &region: regions) { std::cout << std::left << std::setw(32) << region.GetRegionName() << std::setw(64) << region.GetEndpoint() << std::endl; } } else { std::cerr << "Failed to describe regions:" << outcome.GetError().GetMessage() << std::endl; } std::cout << std::endl; return outcome.IsSuccess(); }
  • 有关API详细信息,请参阅 “AWS SDK for C++ API参考 DescribeRegions” 中的。

以下代码示例显示了如何使用DescribeSecurityGroups

SDK对于 C++
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

//! Describe all Amazon Elastic Compute Cloud (Amazon EC2) security groups, or a specific group. /*! \param groupID: A group ID, ignored if empty. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::EC2::describeSecurityGroups(const Aws::String &groupID, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::EC2::EC2Client ec2Client(clientConfiguration); Aws::EC2::Model::DescribeSecurityGroupsRequest request; if (!groupID.empty()) { request.AddGroupIds(groupID); } Aws::String nextToken; do { if (!nextToken.empty()) { request.SetNextToken(nextToken); } Aws::EC2::Model::DescribeSecurityGroupsOutcome outcome = ec2Client.DescribeSecurityGroups(request); if (outcome.IsSuccess()) { std::cout << std::left << std::setw(32) << "Name" << std::setw(30) << "GroupId" << std::setw(30) << "VpcId" << std::setw(64) << "Description" << std::endl; const std::vector<Aws::EC2::Model::SecurityGroup> &securityGroups = outcome.GetResult().GetSecurityGroups(); for (const auto &securityGroup: securityGroups) { std::cout << std::left << std::setw(32) << securityGroup.GetGroupName() << std::setw(30) << securityGroup.GetGroupId() << std::setw(30) << securityGroup.GetVpcId() << std::setw(64) << securityGroup.GetDescription() << std::endl; } } else { std::cerr << "Failed to describe security groups:" << outcome.GetError().GetMessage() << std::endl; return false; } nextToken = outcome.GetResult().GetNextToken(); } while (!nextToken.empty()); return true; }

以下代码示例显示了如何使用MonitorInstances

SDK对于 C++
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

//! Enable detailed monitoring for an Amazon Elastic Compute Cloud (Amazon EC2) instance. /*! \param instanceId: An EC2 instance ID. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::EC2::enableMonitoring(const Aws::String &instanceId, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::EC2::EC2Client ec2Client(clientConfiguration); Aws::EC2::Model::MonitorInstancesRequest request; request.AddInstanceIds(instanceId); request.SetDryRun(true); Aws::EC2::Model::MonitorInstancesOutcome dryRunOutcome = ec2Client.MonitorInstances(request); if (dryRunOutcome.IsSuccess()) { std::cerr << "Failed dry run to enable monitoring on instance. A dry run should trigger an error." << std::endl; return false; } else if (dryRunOutcome.GetError().GetErrorType() != Aws::EC2::EC2Errors::DRY_RUN_OPERATION) { std::cerr << "Failed dry run to enable monitoring on instance " << instanceId << ": " << dryRunOutcome.GetError().GetMessage() << std::endl; return false; } request.SetDryRun(false); Aws::EC2::Model::MonitorInstancesOutcome 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; } return monitorInstancesOutcome.IsSuccess(); }
  • 有关API详细信息,请参阅 “AWS SDK for C++ API参考 MonitorInstances” 中的。

以下代码示例显示了如何使用RebootInstances

SDK对于 C++
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

//! Reboot an Amazon Elastic Compute Cloud (Amazon EC2) instance. /*! \param instanceID: An EC2 instance ID. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::EC2::rebootInstance(const Aws::String &instanceId, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::EC2::EC2Client ec2Client(clientConfiguration); Aws::EC2::Model::RebootInstancesRequest request; request.AddInstanceIds(instanceId); request.SetDryRun(true); Aws::EC2::Model::RebootInstancesOutcome 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); Aws::EC2::Model::RebootInstancesOutcome 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; } return outcome.IsSuccess(); }
  • 有关API详细信息,请参阅 “AWS SDK for C++ API参考 RebootInstances” 中的。

以下代码示例显示了如何使用ReleaseAddress

SDK对于 C++
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

//! Release an Elastic IP address. /*! \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::EC2::releaseAddress(const Aws::String &allocationID, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::EC2::EC2Client ec2(clientConfiguration); Aws::EC2::Model::ReleaseAddressRequest request; request.SetAllocationId(allocationID); Aws::EC2::Model::ReleaseAddressOutcome outcome = ec2.ReleaseAddress(request); if (!outcome.IsSuccess()) { std::cerr << "Failed to release Elastic IP address " << allocationID << ":" << outcome.GetError().GetMessage() << std::endl; } else { std::cout << "Successfully released Elastic IP address " << allocationID << std::endl; } return outcome.IsSuccess(); }
  • 有关API详细信息,请参阅 “AWS SDK for C++ API参考 ReleaseAddress” 中的。

以下代码示例显示了如何使用RunInstances

SDK对于 C++
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

//! Launch an Amazon Elastic Compute Cloud (Amazon EC2) instance. /*! \param instanceName: A name for the EC2 instance. \param amiId: An Amazon Machine Image (AMI) identifier. \param[out] instanceID: String to return the instance ID. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::EC2::runInstance(const Aws::String &instanceName, const Aws::String &amiId, Aws::String &instanceID, const Aws::Client::ClientConfiguration &clientConfiguration) { 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; } instanceID = instances[0].GetInstanceId(); return true; }
  • 有关API详细信息,请参阅 “AWS SDK for C++ API参考 RunInstances” 中的。

以下代码示例显示了如何使用StartInstances

SDK对于 C++
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

//! Start an Amazon Elastic Compute Cloud (Amazon EC2) instance. /*! \param instanceID: An EC2 instance ID. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::EC2::startInstance(const Aws::String &instanceId, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::EC2::EC2Client ec2Client(clientConfiguration); Aws::EC2::Model::StartInstancesRequest startRequest; startRequest.AddInstanceIds(instanceId); startRequest.SetDryRun(true); Aws::EC2::Model::StartInstancesOutcome dryRunOutcome = ec2Client.StartInstances(startRequest); if (dryRunOutcome.IsSuccess()) { std::cerr << "Failed dry run to start instance. A dry run should trigger an error." << std::endl; return false; } else if (dryRunOutcome.GetError().GetErrorType() != Aws::EC2::EC2Errors::DRY_RUN_OPERATION) { std::cout << "Failed dry run to start instance " << instanceId << ": " << dryRunOutcome.GetError().GetMessage() << std::endl; return false; } startRequest.SetDryRun(false); Aws::EC2::Model::StartInstancesOutcome startInstancesOutcome = ec2Client.StartInstances(startRequest); if (!startInstancesOutcome.IsSuccess()) { std::cout << "Failed to start instance " << instanceId << ": " << startInstancesOutcome.GetError().GetMessage() << std::endl; } else { std::cout << "Successfully started instance " << instanceId << std::endl; } return startInstancesOutcome.IsSuccess(); }
  • 有关API详细信息,请参阅 “AWS SDK for C++ API参考 StartInstances” 中的。

以下代码示例显示了如何使用StopInstances

SDK对于 C++
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

//! Stop an EC2 instance. /*! \param instanceID: An EC2 instance ID. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::EC2::stopInstance(const Aws::String &instanceId, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::EC2::EC2Client ec2Client(clientConfiguration); Aws::EC2::Model::StopInstancesRequest request; request.AddInstanceIds(instanceId); request.SetDryRun(true); Aws::EC2::Model::StopInstancesOutcome dryRunOutcome = ec2Client.StopInstances(request); if (dryRunOutcome.IsSuccess()) { std::cerr << "Failed dry run to stop instance. A dry run should trigger an error." << std::endl; return false; } else if (dryRunOutcome.GetError().GetErrorType() != Aws::EC2::EC2Errors::DRY_RUN_OPERATION) { std::cout << "Failed dry run to stop instance " << instanceId << ": " << dryRunOutcome.GetError().GetMessage() << std::endl; return false; } request.SetDryRun(false); Aws::EC2::Model::StopInstancesOutcome 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; } return outcome.IsSuccess(); } void PrintUsage() { std::cout << "Usage: run_start_stop_instance <instance_id> <start|stop>" << std::endl; }
  • 有关API详细信息,请参阅 “AWS SDK for C++ API参考 StopInstances” 中的。

以下代码示例显示了如何使用TerminateInstances

SDK对于 C++
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

//! Terminate an Amazon Elastic Compute Cloud (Amazon EC2) instance. /*! \param instanceID: An EC2 instance ID. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::EC2::terminateInstances(const Aws::String &instanceID, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::EC2::EC2Client ec2Client(clientConfiguration); Aws::EC2::Model::TerminateInstancesRequest request; request.SetInstanceIds({instanceID}); Aws::EC2::Model::TerminateInstancesOutcome outcome = ec2Client.TerminateInstances(request); if (outcome.IsSuccess()) { std::cout << "Ec2 instance '" << instanceID << "' was terminated." << std::endl; } else { std::cerr << "Failed to terminate ec2 instance " << instanceID << ", " << outcome.GetError().GetMessage() << std::endl; return false; } return outcome.IsSuccess(); }
  • 有关API详细信息,请参阅 “AWS SDK for C++ API参考 TerminateInstances” 中的。

以下代码示例显示了如何使用UnmonitorInstances

SDK对于 C++
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

//! Disable monitoring for an EC2 instance. /*! \param instanceId: An EC2 instance ID. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::EC2::disableMonitoring(const Aws::String &instanceId, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::EC2::EC2Client ec2Client(clientConfiguration); Aws::EC2::Model::UnmonitorInstancesRequest unrequest; unrequest.AddInstanceIds(instanceId); unrequest.SetDryRun(true); Aws::EC2::Model::UnmonitorInstancesOutcome dryRunOutcome = ec2Client.UnmonitorInstances(unrequest); if (dryRunOutcome.IsSuccess()) { std::cerr << "Failed dry run to disable monitoring on instance. A dry run should trigger an error." << std::endl; return false; } else if (dryRunOutcome.GetError().GetErrorType() != Aws::EC2::EC2Errors::DRY_RUN_OPERATION) { std::cout << "Failed dry run to disable monitoring on instance " << instanceId << ": " << dryRunOutcome.GetError().GetMessage() << std::endl; return false; } unrequest.SetDryRun(false); Aws::EC2::Model::UnmonitorInstancesOutcome 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; } return unmonitorInstancesOutcome.IsSuccess(); }
  • 有关API详细信息,请参阅 “AWS SDK for C++ API参考 UnmonitorInstances” 中的。