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/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>

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; } allocationId = outcome.GetResult().GetAllocationId(); 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/core/Aws.h> #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; auto 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 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(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/core/Aws.h> #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); auto 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