AWS SDK for C++
Developer Guide

Using Elastic IP Addresses in Amazon EC2


These code snippets assume that you understand the material in Getting Started Using the AWS SDK for C++ and have configured default AWS credentials using the information in 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).

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.


#include <aws/core/Aws.h> #include <aws/ec2/EC2Client.h> #include <aws/ec2/model/AllocateAddressRequest.h> #include <aws/ec2/model/AllocateAddressResponse.h> #include <aws/ec2/model/AssociateAddressRequest.h> #include <aws/ec2/model/AssociateAddressResponse.h> #include <iostream>


Aws::EC2::EC2Client ec2; Aws::EC2::Model::AllocateAddressRequest request; request.SetDomain(Aws::EC2::Model::DomainType::vpc); auto outcome = ec2.AllocateAddress(request); if (!outcome.IsSuccess()) { std::cout << "Failed to allocate elastic ip address:" << outcome.GetError().GetMessage() << std::endl; return; } Aws::String allocation_id = outcome.GetResult().GetAllocationId(); Aws::EC2::Model::AssociateAddressRequest associate_request; associate_request.SetInstanceId(instance_id); associate_request.SetAllocationId(allocation_id); auto associate_outcome = ec2.AssociateAddress(associate_request); if (!associate_outcome.IsSuccess()) { std::cout << "Failed to associate elastic ip address" << allocation_id << " with instance " << instance_id << ":" << associate_outcome.GetError().GetMessage() << std::endl; return; } std::cout << "Successfully associated elastic ip address " << allocation_id << " with instance " << instance_id << 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.


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


Aws::EC2::EC2Client ec2; Aws::EC2::Model::DescribeAddressesRequest request; auto outcome = ec2.DescribeAddresses(request); if (outcome.IsSuccess()) { std::cout << std::left << std::setw(20) << "InstanceId" << std::setw(15) << "Public IP" << std::setw(10) << "Domain" << std::setw(20) << "Allocation ID" << std::setw(25) << "NIC ID" << std::endl; const auto &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(20) << address.GetAllocationId() << std::setw(25) << address.GetNetworkInterfaceId() << std::endl; } } else { std::cout << "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.


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


Aws::Client::ClientConfiguration config; config.region = Aws::Region::US_WEST_2; Aws::EC2::EC2Client ec2(config); Aws::EC2::Model::ReleaseAddressRequest request; request.SetAllocationId(allocation_id); auto outcome = ec2.ReleaseAddress(request); if (!outcome.IsSuccess()) { std::cout << "Failed to release elastic ip address " << allocation_id << ":" << outcome.GetError().GetMessage() << std::endl; } else { std::cout << "Successfully released elastic ip address " << allocation_id << 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 EC2-Classic or 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