Python
Python

Reputation: 63

How to list all public Virtual Machines (EC2) instances of all Regions

I want to get a list of all public Instances of EC2 of any Region. I have tried with C#.net, but I am getting a list of my EC2 Instances which I have created in my Region with below code.

DescribeInstancesResponse describeInstancesResponse = client.DescribeInstances();
List<Reservation> reservation = describeInstancesResponse.Reservations;
var allInstance = reservation.SelectMany(x => x.Instances).ToList();

But my problem is to find out all the instances which I have created or anyone else created(public Virtual machines in Running Status). Is this possible? Please let me know how it will work? Thanks in advance!

Upvotes: 0

Views: 466

Answers (1)

John Hanley
John Hanley

Reputation: 81356

You will need to connect to each region and then list the instances in that region one at a time.

At the top of your code you will want to get the list of regions:

AmazonEC2Client client = new AmazonEC2Client();
DescribeRegionsResponse response = client.DescribeRegions();
var regions = new List<Region>();
regions = response.Regions;
foreach (Region region in regions)
{
    Console.WriteLine(region.RegionName);
}

In the foreach section process each region:

AmazonEC2Client ec2Client = new AmazonEC2Client(region.RegionName);
// add your code here

Upvotes: 1

Related Questions