如何使用boto

时间:2016-04-26 23:33:43

标签: python boto ami

我试图删除与AMI相对应的所有快照。我尝试过以下命令。使用以下命令,我只能删除一个卷。在Boto delete_snapshot设置为True时,我们可以删除在/ dev / sda1上挂载的EBS卷。有没有办法删除AMI的所有卷?

>>> conn.deregister_image('ami-xxxx', delete_snapshot=True, dry_run=False)
True

运行命令后,一个快照被删除,但还有一个快照仍在显示。如何删除ami-xxxx的所有快照?

1 个答案:

答案 0 :(得分:1)

我还没有找到解决这个问题的方法。所以我写了一个python脚本来完成这项工作。

    try:
        list_snaps = conn.get_all_snapshots(filters={'owner_id' :'xxxxx'}) #it used to save some time if we filter by owner
        for i in list_snaps:
            find_ami_id = re.search(r'.* for (.*) from .*', i.description, re.M|re.I)
            if find_ami_id:
                if find_ami_id.group(1) == b: #b is the ami id that we were deleted
                    print "Delete the following snap id: %s" %i.id
                    conn.delete_snapshot(i.id, dry_run=False)
                    time.sleep(10) #wait for a while to delete snapshots one by one
    except boto.exception.BotoServerError, e:
        print e.error_message