List All EC2 Instances
Been playing around a lot with the AWS API. Going to post a few handy snippets every once in a while.
Typically, you start EC2 instances by ID. In this case, we’re doing it by tags (e.g. the name of the instance).
namespace EC2_Instance { class Program { public static void StartInstance(AmazonEC2Client c, string InstanceID) { StartInstancesRequest r = new StartInstancesRequest(); r.InstanceId.Add(InstanceID); c.StartInstances(r); } public static void StopInstance(AmazonEC2Client c, string InstanceID) { StopInstancesRequest r = new StopInstancesRequest(); r.InstanceId.Add(InstanceID); c.StopInstances(r); } public static void Main(string[] args) { AmazonEC2Client ec2Client = new AmazonEC2Client(); var instancesRequest = new DescribeInstancesRequest(); instancesRequest.Filter.Add(new Filter() { Name = "tag:Name", Value = new List { "*" } }); DescribeInstancesResponse statusResponse = ec2Client.DescribeInstances(instancesRequest); foreach (var item in statusResponse.DescribeInstancesResult.Reservation) { Console.WriteLine(item.RunningInstance.ElementAt(0).Tag.Where(x => x.Key == "Name").First().Value); Console.WriteLine(item.RunningInstance.ElementAt(0).InstanceId); Console.WriteLine("\n"); } Console.ReadLine(); Console.WriteLine("\n\nDone."); } } }
Categories