I'm trying to delete all EBS snapshots except the X newest ones on a Windows AWS instance via the EC2 command-line API tools. I've found开发者_JAVA百科 the following for Linux, is it possible to do the same in Windows?
ec2-describe-snapshots | sort -r -k 5 | sed 1,6d | awk '{print "Deleting snapshot: " $2}; system("ec2-delete-snapshot " $2)'
Update
Just realized that you apparently want to delete all EBS snapshots indeed - in case, the script can be adjusted to this as well via boto's get_all_snapshots()
method, which, well, gets all EBS Snapshots associated with the current credentials.
You'll need to replace the volume oriented code accordingly, e.g. replace
snapshots = volume.snapshots()
with
snapshots = conn.get_all_snapshots()
Initial Answer
While certainly possible via a combination of the Amazon EC2 API Tools and a respective PowerShell script as well, I personally prefer the excellent boto to interact with AWS in a platform agnostic way (i.e. the scripts work on Linux, MacOS, Windows) - Boto is a Python package that provides interfaces to Amazon Web Services.
I've just been about to assemble something in this regard, but Vaibhav Puranik has it readily available, see Manage EBS snapshots with a python script - his script creates a new ebs snapshot and deletes all the previous snapshots except a few newest snapshots.
So while it offers more functionality than requested, the script is simple enough to be adjusted to your needs, e.g. remove the following two lines to get rid of snapshot creation, if so desired:
if volume.create_snapshot(description):
print 'Snapshot created with description: ' + description
Disclaimer: I have neither thoroughly reviewed nor tested the script myself!
Accordingly, given the script deletes data for good, you should review and test it yourself before using it in production.
Good luck!
精彩评论