Python Boto3 SnapshotNotFound 异常。无法删除快照

发布于 2025-01-16 21:19:10 字数 1929 浏览 3 评论 0原文

我是 AWS 新手,我编写了一个 Boto3 脚本,该脚本通过 OwnerId 获取快照并一一删除较旧的快照。我遇到一个奇怪的问题,boto 客户端找到所有快照,当它到达 client.delete_snapshot(SnapshotId=snapshot.snapshot_id) 时,它会抛出 SnapshotNotFoundException - 无法删除 ID 为 abcd 的快照。

奇怪的是,当我进入 AWS 控制台并搜索 ID 时,快照就在那里,我可以从 AWS 控制台中删除它,并且我在 Boto3 配置文件中使用相同的帐户凭据。

[default]
aws_access_key_id=foo
aws_secret_access_key=bar 

这是我尝试过的。 Boto3 脚本

from datetime import datetime, timedelta, timezone
import boto3
ec2 = boto3.resource('ec2')
cl = boto3.client('ec2')
count=0
snapshots = ec2.snapshots.filter(OwnerIds=['xxxxxxxxxx'])
def if_associated_to_ami(client, snapshot_id):
    img = client.describe_images(Filters=[{'Name': 'block-device-mapping.snapshot-id', 'Values': [snapshot_id]}])
    try:
        ami_id = img['Images'][0]['ImageId']
        #print("Snapshot(" + snapshot_id + ") is associated to image(" + ami_id + "). Return True")
        return True
    except IndexError:
        #print("Snapshot(" + snapshot_id + ") is not associated to any image. Return False")
        return False
for snapshot in snapshots:
    if if_associated_to_ami(cl, snapshot.snapshot_id):
        print('Unabble to delete Snapshot with Id = {}. Snapshot is in used! '. format(snapshot.snapshot_id))
    else:
        start_time = snapshot.start_time
        delete_time = datetime.now(tz=timezone.utc) - timedelta(days=90)
        if delete_time > start_time:
            #snapshot.delete()
            cl.delete_snapshot(SnapshotId=snapshot.snapshot_id)
            print('Snapshot with Id = {} is deleted '. format(snapshot.snapshot_id))
    count+=1
    if count == 1000:
        break

如果您遇到缩进问题,请检查此处的文件。 https://github.com/DeveloperMujtaba/usual-resources/blob/ master/boto3.py 有人可以指出这个问题吗?我们将不胜感激。谢谢。

I'm new to AWS and I have written a Boto3 script which gets the snapshots by OwnerId and delete older snapshots one by one. I'm having a strange issue that boto client finds all Snapshots and when it reaches client.delete_snapshot(SnapshotId=snapshot.snapshot_id) It throws SnapshotNotFoundException - Unable to delete snapshot with id abcd.

It's weird that when i go to AWS Console and search for the ID, Snapshot is there and I can delete it from the AWS Console and I'm using the same account credentials with Boto3 config file.

[default]
aws_access_key_id=foo
aws_secret_access_key=bar 

Here is what i have tried.
Boto3 Script

from datetime import datetime, timedelta, timezone
import boto3
ec2 = boto3.resource('ec2')
cl = boto3.client('ec2')
count=0
snapshots = ec2.snapshots.filter(OwnerIds=['xxxxxxxxxx'])
def if_associated_to_ami(client, snapshot_id):
    img = client.describe_images(Filters=[{'Name': 'block-device-mapping.snapshot-id', 'Values': [snapshot_id]}])
    try:
        ami_id = img['Images'][0]['ImageId']
        #print("Snapshot(" + snapshot_id + ") is associated to image(" + ami_id + "). Return True")
        return True
    except IndexError:
        #print("Snapshot(" + snapshot_id + ") is not associated to any image. Return False")
        return False
for snapshot in snapshots:
    if if_associated_to_ami(cl, snapshot.snapshot_id):
        print('Unabble to delete Snapshot with Id = {}. Snapshot is in used! '. format(snapshot.snapshot_id))
    else:
        start_time = snapshot.start_time
        delete_time = datetime.now(tz=timezone.utc) - timedelta(days=90)
        if delete_time > start_time:
            #snapshot.delete()
            cl.delete_snapshot(SnapshotId=snapshot.snapshot_id)
            print('Snapshot with Id = {} is deleted '. format(snapshot.snapshot_id))
    count+=1
    if count == 1000:
        break

if you face indentation issues, please check the file here.
https://github.com/DeveloperMujtaba/usual-resources/blob/master/boto3.py
can someone please indicate the issue please? It would be much appreciated. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

饮湿 2025-01-23 21:19:10

老实说,仅通过查看您的代码,我无法说出为什么它会大量出现,而且,b/c 您已经拥有快照资源,为什么不这样做:

snapshot.delete()

Honestly, just by looking at your code, I cannot tell why it bulks at that but also, b/c you already have the snapshot resource, why not just do:

snapshot.delete()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文