我对这种情况感到困惑:我可以检索与 EC2 实例关联的卷的所有快照,但是一旦EC2实例被删除,其关联卷就会被删除。我一直在Boto3文档周围揉搓并挣扎。下面是我列出与Volume关联的快照的工作示例。
import boto3
ec2 = boto3.resource('ec2')
volume_id = "something"
list_of_snapshots = []
vol = ec2.Volume(id=volume_id)
snapshots = vol.snapshots.all()
try:
for snapshot in snapshots:
if snapshot:
list_of_snapshots.append(snapshot.id)
return list_of_snapshots[0]
except:
return None
答案 0 :(得分:2)
答案 1 :(得分:0)
@John Rotenstein建议,
现在可能的解决方案是在创建快照时在标记内添加VolumeID,因此代码结果如下:
import boto3
ec2 = boto3.client('ec2')
volume_id= "something"
list_of_snapshots = []
snapshots = ec2.describe_snapshots(Filters=[{'Name':'tag:volume_id', 'Values': [volume_id]}])['Snapshots']
try:
for snapshot in snapshots:
if snapshot:
list_of_snapshots.append(snapshot['SnapshotId'])
return list_of_snapshots
except:
return None