2016-05-04 14 views
-1

Da Amazon von ec2-ami-tools auf awscli umschaltete (siehe this), ist Robert Sindalls solution nicht länger akzeptabel.Unbenutzte Amazon EC2 Snapshots entfernen

Also habe ich beschlossen, sein Skript für neue Python/JSON-Standards umzuschreiben.

Fühlen Sie sich frei, irgendwelche Fragen zu stellen oder Ihre Lösung zur Verfügung zu stellen.

Antwort

0

Bitte installieren und konfigurieren Sie awscli ordnungsgemäß. Für MacOS würde ich brew Version empfehlen.

Nach der Installation nicht vergessen aws configure

funktioniert einwandfrei für mich laufen, aber trotzdem BENUTZUNG AUF EIGENE GEFAHR.

import json, subprocess 

def remove_unused_snaps(region): 
    line = 'aws ec2 describe-images --region {} --owners self'.format(region) 
    p = subprocess.Popen(line.split(), 
         stdout=subprocess.PIPE, stderr=subprocess.PIPE) 
    out, err = p.communicate() 
    obj = json.loads(out.decode("utf-8")) 

    snaps_used = set(i['BlockDeviceMappings'][0]['Ebs']['SnapshotId'] for i in obj['Images']) 

    line = 'aws ec2 describe-snapshots --region {} --owner-ids self'.format(region) 
    p = subprocess.Popen(line.split(), 
         stdout=subprocess.PIPE, stderr=subprocess.PIPE) 
    out, err = p.communicate() 
    obj = json.loads(out.decode("utf-8")) 

    snaps_old = set(i['SnapshotId'] for i in obj['Snapshots']) 

    line = 'aws ec2 delete-snapshot --region {} --snapshot-id'.format(region) 
    for snap in list(snaps_old - snaps_used): 
     p = subprocess.Popen(line.split() + [snap], 
          stdout=subprocess.PIPE, stderr=subprocess.PIPE) 
     out, err = p.communicate() 
     print(snap, out, err) 

Dann einfach laufen: remove_unused_snaps('us-west-2')