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
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 AllocateAddressResponseAssociateAddress
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
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
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
-
Elastic IP Addresses in the Amazon EC2 User Guide
-
AllocateAddress in the Amazon EC2 API Reference
-
DescribeAddresses in the Amazon EC2 API Reference
-
ReleaseAddress in the Amazon EC2 API Reference