AWS SDK for .NET
Developer Guide

Using Elastic IP Addresses in Amazon EC2

This .NET example shows you how to:

  • Retrieve descriptions of your Elastic IP addresses

  • Allocate and associate an Elastic IP address with an Amazon EC2 instance

  • Release an Elastic IP address

The Scenario

An Elastic IP address is a static IP address designed for dynamic cloud computing. An Elastic IP address is associated with your AWS account, and is a public IP address reachable from the Internet.

If your Amazon EC2 instance doesn't have a public IP address, you can associate an Elastic IP address with your instance to enable communication with the Internet.

In this example, you use the AWS SDK for .NET to manage Elastic IP addresses by using these methods of the Amazon EC2 client class:

For more information about Elastic IP addresses in Amazon EC2, see Elastic IP Addresses in the Amazon EC2 User Guide for Windows Instances.

Describe Elastic IP Addresses

Create an AmazonEC2Client object. Next, create a DescribeAddressesRequest object to pass as a parameter, filtering the addresses returned by those in your VPC. To retrieve descriptions of all your Elastic IP addresses, omit the filter from the parameters. Then call the DescribeAddresses method of the AmazonEC2Client object.

public void DescribeElasticIps() { using (var client = new AmazonEC2Client(RegionEndpoint.USWest2)) { var addresses = client.DescribeAddresses(new DescribeAddressesRequest { Filters = new List<Filter> { new Filter { Name = "domain", Values = new List<string> { "vpc" } } } }).Addresses; foreach(var address in addresses) { Console.WriteLine(address.PublicIp); Console.WriteLine("\tAllocation Id: " + address.AllocationId); Console.WriteLine("\tPrivate IP Address: " + address.PrivateIpAddress); Console.WriteLine("\tAssociation Id: " + address.AssociationId); Console.WriteLine("\tInstance Id: " + address.InstanceId); Console.WriteLine("\tNetwork Interface Owner Id: " + address.NetworkInterfaceOwnerId); } } }

Allocate and Associate an Elastic IP Address

Create an AmazonEC2Client object. Next, create an AllocateAddressRequest object for the parameter used to allocate an Elastic IP address, which in this case specifies that the domain is a VPC. Call the AllocateAddress method of the AmazonEC2Client object.

Upon success, the returned AllocateAddressResponse object has an AllocationId property that identifies the allocated Elastic IP address.

Create an AssociateAddressRequest object for the parameters used to associate an Elastic IP address to an Amazon EC2 instance. Include the AllocationId from the newly allocated address and the InstanceId of the Amazon EC2 instance. Then call the AssociateAddress method of the AmazonEC2Client object.

public void AllocateAndAssociate(string instanceId) { using (var client = new AmazonEC2Client(RegionEndpoint.USWest2)) { var allocationId = client.AllocateAddress(new AllocateAddressRequest { Domain = DomainType.Vpc }).AllocationId; Console.WriteLine("Allocation Id: " + allocationId); var associationId = client.AssociateAddress(new AssociateAddressRequest { AllocationId = allocationId, InstanceId = instanceId }).AssociationId; Console.WriteLine("Association Id: " + associationId); } }

Release an Elastic IP Address

Create an AmazonEC2Client object. Next, create a ReleaseAddressRequest object for the parameters used to release an Elastic IP address, which in this case specifies the AllocationId for the Elastic IP address. Releasing an Elastic IP address also disassociates it from any Amazon EC2 instance. Call the ReleaseAddress method of the Amazon EC2 service object.

public void Release(string allocationId) { using (var client = new AmazonEC2Client(RegionEndpoint.USWest2)) { client.ReleaseAddress(new ReleaseAddressRequest { AllocationId = allocationId }); } }