简体   繁体   中英

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM