Python 2.7 如何检查ID为的快照是否正在使用AWS AMI';波托在哪里?
删除快照时出现以下错误。我想删除AWS AMI和其他实例当前未使用的快照。。还有,我试过了,但出现了这个错误Python 2.7 如何检查ID为的快照是否正在使用AWS AMI';波托在哪里?,python-2.7,amazon-web-services,boto,Python 2.7,Amazon Web Services,Boto,删除快照时出现以下错误。我想删除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><Err
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>
不幸的是,没有API可以直接从EBS快照获取AMI ID 相反,你可以走另一条路
ec2:descripbeImages
获取AMI图像列表李>
ec2:descripbeImages
https://ec2.amazonaws.com/?Action=DescribeImages
&Filter.1.Name=block-device-mapping.snapshot-id
&Filter.1.Value=snap-xxxx
参考:删除所有未使用的快照:
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
对于$(comm-231)中的s检索所有快照,使用正则表达式在快照描述中搜索AMI-ID
reAmi = re.compile('ami-[^ ]+')
snapshotImageId = reAmi.findall(snapshot.description)
2) 检索所有AMI的。检查第一步中检索到的AMI-ID是否仍然存在,如果不存在,则不再需要与该特定AMI关联的快照
完整的代码已发布
希望能有帮助 你能在这里发布一些代码吗?