Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I list all EC2 instances using Amazon .NET library?

I'm trying to programatically access all of my EC2 instances using the .NET library.

How can I get a list of all instances, and fetch their individual IP address?

like image 254
sergserg Avatar asked Dec 15 '22 00:12

sergserg


2 Answers

Use AmazonEC2Client.DescribeInstances Method

result = client.DescribeInstances();

foreach (var instance in result.Reservations[0].Instances) {
    privateIps.add(instance.PrivateIpAddress);
}
like image 103
Uri Agassi Avatar answered Jan 19 '23 00:01

Uri Agassi


In AWS and EC2 Speak, when you want to get a list of something, or find out more about it, it's a "Describe" call.

For example:

  • DescribeImages
  • DescribeVolumes
  • DescribeSnapshots

... and the one you're specifically looking for:

  • DescribeInstances

The DescribeInstances call will return you a data structure that has the IP Address for each instance. Note that this is a PAGED API, which means if you many instances (>1000) you'll need to keep calling it, providing the relevant page token, to get the complete list.

like image 41
Chris M. Avatar answered Jan 18 '23 23:01

Chris M.