删除未使用的Amazon EC2快照

时间:2016-05-04 18:47:14

标签: python amazon-ec2 aws-cli

由于亚马逊从ec2-ami-tools切换到awscli(检查this),Robert Sindall的solution已不再可接受。

所以我决定为新的python / json标准重写他的脚本。

随时提出任何问题或提供解决方案。

1 个答案:

答案 0 :(得分:0)

请正确安装和配置awscli。对于MacOS,我建议使用brew版本。

安装后请勿忘记运行$payment = Auth::user()->payments()->orderBy('updated_at', 'desc')->first();

对我来说完美无瑕,但无论如何都要自行承担使用风险。

aws configure

然后只需运行它: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)