選取您的 Cookie 偏好設定

我們使用提供自身網站和服務所需的基本 Cookie 和類似工具。我們使用效能 Cookie 收集匿名統計資料,以便了解客戶如何使用我們的網站並進行改進。基本 Cookie 無法停用,但可以按一下「自訂」或「拒絕」以拒絕效能 Cookie。

如果您同意,AWS 與經核准的第三方也會使用 Cookie 提供實用的網站功能、記住您的偏好設定,並顯示相關內容,包括相關廣告。若要接受或拒絕所有非必要 Cookie,請按一下「接受」或「拒絕」。若要進行更詳細的選擇,請按一下「自訂」。

Using Elastic IP Addresses in Amazon EC2 - AWS SDK for C++
此頁面尚未翻譯為您的語言。 請求翻譯

Using Elastic IP Addresses in Amazon EC2

Prerequisites

Before you begin, we recommend you read Getting started using the AWS 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 AWS (for the service and the action). For more information, see Providing AWS credentials.

Allocate an Elastic IP Address

To use an Elastic IP address, you first allocate one to your account, and then associate it with your instance or a network interface.

To allocate an Elastic IP address, call the EC2Client’s AllocateAddress function with an AllocateAddressRequest object containing the network type (classic EC2 or VPC).

Warning

We are retiring EC2-Classic on August 15, 2022. We recommend that you migrate from EC2-Classic to a VPC. For more information, see Migrate from EC2-Classic to a VPC in the the Amazon EC2 User Guide for Linux Instances or the Amazon EC2 User Guide for Windows Instances. Also see the blog post EC2-Classic Networking is Retiring – Here's How to Prepare.

The AllocateAddressResponse class in the response object contains an allocation ID that you can use to associate the address with an instance, by passing the allocation ID and instance ID in a AssociateAddressRequest to the EC2Client’s AssociateAddress function.

Includes

#include <aws/ec2/EC2Client.h> #include <aws/ec2/model/AllocateAddressRequest.h> #include <aws/ec2/model/AssociateAddressRequest.h> #include <iostream>

Code

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(); Aws::EC2::Model::AssociateAddressRequest associate_request; associate_request.SetInstanceId(instanceId); associate_request.SetAllocationId(allocationID); const Aws::EC2::Model::AssociateAddressOutcome associate_outcome = ec2Client.AssociateAddress(associate_request); if (!associate_outcome.IsSuccess()) { std::cerr << "Failed to associate Elastic IP address " << allocationID << " with instance " << instanceId << ":" << associate_outcome.GetError().GetMessage() << std::endl; return false; } std::cout << "Successfully associated Elastic IP address " << allocationID << " with instance " << instanceId << std::endl;

See the complete example.

Describe Elastic IP Addresses

To list the Elastic IP addresses assigned to your account, call the EC2Client’s DescribeAddresses function. It returns an outcome object that contains a DescribeAddressesResponse which you can use to get a list of Address objects that represent the Elastic IP addresses on your account.

Includes

#include <aws/ec2/EC2Client.h> #include <aws/ec2/model/DescribeAddressesRequest.h> #include <aws/ec2/model/DescribeAddressesResponse.h> #include <iomanip> #include <iostream>

Code

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; }

See the complete example.

Release an Elastic IP Address

To release an Elastic IP address, call the EC2Client’s ReleaseAddress function, passing it a ReleaseAddressRequest containing the allocation ID of the Elastic IP address you want to release.

Includes

#include <aws/ec2/EC2Client.h> #include <aws/ec2/model/ReleaseAddressRequest.h> #include <iostream>

Code

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; }

After you release an Elastic IP address, it is released to the AWS IP address pool and might be unavailable to you afterward. Be sure to update your DNS records and any servers or devices that communicate with the address. If you attempt to release an Elastic IP address that you already released, you’ll get an AuthFailure error if the address is already allocated to another AWS account.

If you are using a default VPC, then releasing an Elastic IP address automatically disassociates it from any instance that it’s associated with. To disassociate an Elastic IP address without releasing it, use the EC2Client’s DisassociateAddress function.

If you are using a non-default VPC, you must use DisassociateAddress to disassociate the Elastic IP address before you try to release it. Otherwise, Amazon EC2 returns an error (InvalidIPAddress.InUse).

See the complete example.

More Information

隱私權網站條款Cookie 偏好設定
© 2025, Amazon Web Services, Inc.或其附屬公司。保留所有權利。