简体   繁体   English

使用 boto3 删除备份管理的快照

[英]Deleting Snapshots managed by Backup using boto3

I wanted to delete the old snapshots from my aws account i used the following code but i am getting an error can you please suggest any code changes我想从我的 aws 帐户中删除旧快照,我使用了以下代码,但出现错误,您能否建议任何代码更改

this is the code这是代码

import json
import boto3
import datetime
client = boto3.client('ec2',region_name='us-east-1')
snapshots = client.describe_snapshots(OwnerIds=['self'])
def lambda_handler(event, context):
    print("printing snapshots")
    print(snapshots)
    Totalcount = 0
    deletecount = 0
    for snapshot in snapshots['Snapshots']:
        id = snapshot['SnapshotId']
        a = snapshot['StartTime']
        Totalcount = Totalcount + 1
        b = a.date()
        c = datetime.datetime.now().date()
        d = c-b
        try:
          if d.days>=31:
              id = snapshot['SnapshotId']
              deletecount = deletecount + 1
              client.delete_snapshot(SnapshotId=id)
              print ('Snapshot with Id = {id}  will be deleted '.format(id = id))
        except Exception as e:
          if 'InvalidSnapshot.InUse' in e.message:
              
              print("skipping this snapshot")
              continue
    print ('Total Snapshots in Account are {Totalcount}.'.format(Totalcount = Totalcount))
    print ('Deleted Snapshots of age grater than 31 are {deletecount}.'.format(deletecount = deletecount))

this the error这是错误

An error occurred (InvalidParameterValue) when calling the DeleteSnapshot operation: This snapshot is managed by the AWS Backup service and cannot be deleted via EC2 APIs. If you wish to delete this snapshot, please do so via the Backup console.

Loos like your backups are done by AWS Backup.好像您的备份是由 AWS Backup 完成的。 For that you can use AWS Backup client and use the delete-recovery-point API to do that:为此,您可以使用 AWS Backup 客户端并使用 delete-recovery-point API 来执行此操作:

https://docs.aws.amazon.com/cli/latest/reference/backup/delete-recovery-point.html https://docs.aws.amazon.com/cli/latest/reference/backup/delete-recovery-point.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM