AWS SDK for Java
Developer Guide

Using Elastic IP Addresses in Amazon EC2

Important

This is a preview release and is not recommended for production environments.

Allocating 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 method with an AllocateAddressRequest object containing the network type (classic EC2 or VPC).

The returned AllocateAddressResponse 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 method.

Imports

import software.amazon.awssdk.services.ec2.Ec2Client; import software.amazon.awssdk.services.ec2.model.AllocateAddressRequest; import software.amazon.awssdk.services.ec2.model.AllocateAddressResponse; import software.amazon.awssdk.services.ec2.model.AssociateAddressRequest; import software.amazon.awssdk.services.ec2.model.AssociateAddressResponse; import software.amazon.awssdk.services.ec2.model.DomainType;

Code

Ec2Client ec2 = Ec2Client.create(); AllocateAddressRequest allocate_request = AllocateAddressRequest.builder() .domain(DomainType.VPC) .build(); AllocateAddressResponse allocate_response = ec2.allocateAddress(allocate_request); String allocation_id = allocate_response.allocationId(); AssociateAddressRequest associate_request = AssociateAddressRequest.builder() .instanceId(instance_id) .allocationId(allocation_id) .build(); AssociateAddressResponse associate_response = ec2.associateAddress(associate_request); System.out.printf( "Successfully associated Elastic IP address %s " + "with instance %s", associate_response.associationId(), instance_id);

See the complete example on GitHub.

Describing Elastic IP Addresses

To list the Elastic IP addresses assigned to your account, call the Ec2Client's describeAddresses method. It returns a DescribeAddressesResponse which you can use to get a list of Address objects that represent the Elastic IP addresses on your account.

Imports

import software.amazon.awssdk.services.ec2.Ec2Client; import software.amazon.awssdk.services.ec2.model.Address; import software.amazon.awssdk.services.ec2.model.DescribeAddressesResponse;

Code

Client ec2 = Ec2Client.create(); DescribeAddressesResponse response = ec2.describeAddresses(); for(Address address : response.addresses()) { System.out.printf( "Found address with public IP %s, " + "domain %s, " + "allocation id %s " + "and NIC id %s", address.publicIp(), address.domain(), address.allocationId(), address.networkInterfaceId()); }

See the complete example on GitHub.

Releasing an Elastic IP Address

To release an Elastic IP address, call the Ec2Client's releaseAddress method, passing it a ReleaseAddressRequest containing the allocation ID of the Elastic IP address you want to release.

Imports

import software.amazon.awssdk.services.ec2.Ec2Client; import software.amazon.awssdk.services.ec2.model.ReleaseAddressRequest; import software.amazon.awssdk.services.ec2.model.ReleaseAddressResponse;

Code

Ec2Client ec2 = Ec2Client.create(); ReleaseAddressRequest request = ReleaseAddressRequest.builder() .allocationId(alloc_id).build(); ReleaseAddressResponse response = ec2.releaseAddress(request);

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 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 method.

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 on GitHub.

More Information