[英]How to retrieve all snapshots related to a AWS EC2 instance using BOTO3 Python?
我糾結於這種情況:我可以檢索與EC2實例關聯的卷的所有快照,但是一旦刪除EC2實例,關聯的卷也會被刪除。 我一直在摸索Boto3文檔並苦苦掙扎。 以下是我的工作示例,列出了與卷關聯的快照。
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
正如@約翰·羅滕斯坦(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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.