Skip to content

Instantly share code, notes, and snippets.

@TerribleDev
Created March 9, 2017 18:38
Show Gist options
  • Select an option

  • Save TerribleDev/9c1937357d1f86bd1f405a39cb82041e to your computer and use it in GitHub Desktop.

Select an option

Save TerribleDev/9c1937357d1f86bd1f405a39cb82041e to your computer and use it in GitHub Desktop.
delete aws instances on a tag
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