如何使用 C# 获取已分配静态公共弹性 IP 的 EC2 实例的列表?
问问题
1759 次
1 回答
2
基本上,您将获得实例和公共 IP,然后您将比较实例是否包含任何公共 IP 地址。
这是将为您执行此操作的代码:
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)));
于 2013-02-06T23:55:36.090 回答