【问题标题】:How to check whether a snapshot with ID is in use of AWS AMI's in Boto?如何检查具有 ID 的快照是否正在使用 Boto 中的 AWS AMI?
【发布时间】:2021-11-24 00:15:17
【问题描述】:

我在删除快照时收到以下错误。我想删除我的 AWS AMI 和其他实例当前未使用的快照。我也尝试过但收到此错误。

Traceback (most recent call last):
<path to error file>
EC2ResponseError: EC2ResponseError: 400 Bad Request
<?xml version="1.0" encoding="UTF-8"?><Response><Errors><Error><Code>InvalidSnapshot.InUse</Code><Message>The snapshot snap-xxxxxxxx is currently in use by ami-xxxxxxxx</Message></Error></Errors><RequestID>bbe55333-4acf-4ca7-9aa3-49307b189ca3</RequestID></Response>

【问题讨论】:

  • 你能在这里贴一些代码吗?

标签: python-2.7 amazon-web-services boto


【解决方案1】:

很遗憾,没有直接从 EBS 快照获取 AMI ID 的 API。

相反,您可以选择其他方式。

  1. 使用ec2:DescribeImages 获取AMI 映像列表。
  2. 对于返回的每个 AMI 映像,检查与 AMI 映像关联的 EBS 快照列表。
  3. 查看是否包含有问题的 EBS 快照。

编辑:另一种可能性:

您可以使用ec2:DescribeImages 对 EBS 快照 ID 进行过滤。

https://ec2.amazonaws.com/?Action=DescribeImages
 &Filter.1.Name=block-device-mapping.snapshot-id
 &Filter.1.Value=snap-xxxx

参考:http://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeImages.html

【讨论】:

    【解决方案2】:

    删除所有未使用的快照:

    for s in $(comm -23 <(echo $(ec2-describe-snapshots --region ap-southeast-1 | grep SNAPSHOT | awk '{print $2}' | sort | uniq) | tr ' ' '\n') <(echo $(ec2-describe-images --region ap-southeast-1 | grep BLOCKDEVICEMAPPING | awk '{print $3}' | sort | uniq) | tr ' ' '\n') | tr '\n' ' ')
    do
        echo Deleting snapshot $s
        ec2-delete-snapshot --region ap-southeast-1 $s  
    done
    

    【讨论】:

      【解决方案3】:

      1) 检索所有快照,使用正则表达式在快照描述中搜索 AMI-ID。

          reAmi = re.compile('ami-[^ ]+')
          snapshotImageId = reAmi.findall(snapshot.description)
      

      2) 检索所有 AMI。检查在第一步中检索到的 AMI-ID 是否仍然存在,如果不存在,则不再需要与该特定 AMI 关联的快照。

      完整代码发布here

      希望对你有帮助!!

      【讨论】:

        【解决方案4】:

        以防万一有人仍然想知道如何做到这一点:

        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
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-10-22
          • 2020-11-16
          • 1970-01-01
          • 2020-07-10
          • 1970-01-01
          • 1970-01-01
          • 2013-07-11
          • 1970-01-01
          相关资源
          最近更新 更多