从关联的 AMI 获取快照 ID 列表
Getting A List of Snapshot IDs From Their Associated AMI
我正在使用 boto3 尝试获取与其 AMI 关联的快照的快照 ID。
到目前为止我有这个:
#!/usr/bin/env python
import boto3
ec2_client = boto3.client('ec2', region_name='REGION')
owner_id = 'ACCOUNT_ID'
all_images = {}
all_used_images = []
# Pull all AMIs owned by the account
amis_response = ec2_client.describe_images(
Filters=[
{
'Name': 'root-device-type',
'Values': [ 'ebs']
},
],
Owners=[ owner_id ]
)
# Get list of snapshots associated with AMIs
for image_info in amis_response['Images']:
image_id = image_info['ImageId']
print (image_id)
snapshot_ids = []
for device_map in image_info['BlockDeviceMappings']:
snapshot_ids.append(device_map['Ebs']['SnapshotId'])
all_images[image_id] = snapshot_ids
amis_response 提取帐户中的所有 ami 信息,但是当我尝试获取快照列表时,我不断收到:
snapshot_ids.append(device_map['Ebs']['SnapshotId'])
KeyError: 'Ebs'
我错过了什么?
当我打印 amis_response 的输出时,我看到:
u'BlockDeviceMappings': [{u'DeviceName': '/dev/sda1', u'Ebs': {u'SnapshotId': 'snap-0f5b70d9ddaa7b2e1'
Ebs
可能不会出现在每个输出中。您可以检查一下:
for device_map in image_info['BlockDeviceMappings']:
if 'Ebs' in device_map:
snapshot_ids.append(device_map['Ebs']['SnapshotId'])
else:
print('No Ebs info for', image_id)
all_images[image_id] = snapshot_ids
我正在使用 boto3 尝试获取与其 AMI 关联的快照的快照 ID。
到目前为止我有这个:
#!/usr/bin/env python
import boto3
ec2_client = boto3.client('ec2', region_name='REGION')
owner_id = 'ACCOUNT_ID'
all_images = {}
all_used_images = []
# Pull all AMIs owned by the account
amis_response = ec2_client.describe_images(
Filters=[
{
'Name': 'root-device-type',
'Values': [ 'ebs']
},
],
Owners=[ owner_id ]
)
# Get list of snapshots associated with AMIs
for image_info in amis_response['Images']:
image_id = image_info['ImageId']
print (image_id)
snapshot_ids = []
for device_map in image_info['BlockDeviceMappings']:
snapshot_ids.append(device_map['Ebs']['SnapshotId'])
all_images[image_id] = snapshot_ids
amis_response 提取帐户中的所有 ami 信息,但是当我尝试获取快照列表时,我不断收到:
snapshot_ids.append(device_map['Ebs']['SnapshotId'])
KeyError: 'Ebs'
我错过了什么?
当我打印 amis_response 的输出时,我看到:
u'BlockDeviceMappings': [{u'DeviceName': '/dev/sda1', u'Ebs': {u'SnapshotId': 'snap-0f5b70d9ddaa7b2e1'
Ebs
可能不会出现在每个输出中。您可以检查一下:
for device_map in image_info['BlockDeviceMappings']:
if 'Ebs' in device_map:
snapshot_ids.append(device_map['Ebs']['SnapshotId'])
else:
print('No Ebs info for', image_id)
all_images[image_id] = snapshot_ids