Search code examples
c#amazon-web-servicesec2-api-tools

How can I get a list of my EC2 instances that have assigned Elastic IPs?


How can I get a list of my EC2 instances that have assigned static public Elastic IPs using C#?


Solution

  • Basically you will get instances and public IP-s, then you will compare if instance contains any public IP address.

    Here is the code which will do that for you:

                    AmazonEC2 ec2 = AWSClientFactory.CreateAmazonEC2Client();
    
                    //Create instance request
                    DescribeInstancesRequest request = new DescribeInstancesRequest();
                    DescribeInstancesResponse response = ec2.DescribeInstances(request);
    
                    //Create ip address request enumeration
                    DescribeAddressesRequest daRequest = new DescribeAddressesRequest();
                    DescribeAddressesResponse daResponse = ec2.DescribeAddresses(daRequest);
                    var publicIps = from ips in daResponse.DescribeAddressesResult.Address select ips.PublicIp;
    
                    //List of the instances which has public IP
                    List<RunningInstance> publicIpInstances = new List<RunningInstance>();
    
                    //Iterate over instances and check if they have public IP
                    foreach (Reservation ri in response.DescribeInstancesResult.Reservation)                    
                        publicIpInstances.AddRange(ri.RunningInstance.Where(inst => publicIps.Contains(inst.IpAddress)));