Created
March 9, 2017 18:38
-
-
Save TerribleDev/9c1937357d1f86bd1f405a39cb82041e to your computer and use it in GitHub Desktop.
delete aws instances on a tag
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| static void Main(string[] args) | |
| { | |
| var t = new Amazon.EC2.AmazonEC2Client("key", "scrt", Amazon.RegionEndpoint.USEast1); | |
| string nextToken = null; | |
| var lst = new List<Instance>(); | |
| do | |
| { | |
| var instances = t.DescribeInstances(new Amazon.EC2.Model.DescribeInstancesRequest() { NextToken = nextToken }); | |
| nextToken = instances.NextToken; | |
| lst.AddRange(instances.Reservations.SelectMany(a=>a.Instances).Where(a=>a.Tags.Any(b=>b.Key == "key" && b.Value == "value"))); | |
| } while (!string.IsNullOrEmpty(nextToken)); | |
| t.TerminateInstances(new TerminateInstancesRequest() { InstanceIds = lst.Select(a => a.InstanceId).ToList() }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment