我需要提取所有“ SnapshotId”(先前过滤)以将其删除。但是我的循环不适用于具有2个或更多附加卷的AMIS ...
我试图用一条指令(而不是几个循环)来做到这一点。
import boto3
ec2_client = boto3.client('ec2')
#filters:
images = ec2_client.describe_images(Owners=['self'])
AMIDescription ="AMI backup generated by Lambda"
#loops:
data = [i['BlockDeviceMappings'][0]['Ebs']['SnapshotId'] for i in images['Images'] if i['Description'] == AMIDescription ]
#print:
print (data)
结果: ['snap-0f0dd0b4949bbd867','snap-0bec32fc28f885d9a','snap-022f5e19566c99256']
带有所有信息的json示例:
{
'Images': [
{
'Architecture': 'x86_64',
'CreationDate': '2019-07-09T14:49:47.000Z',
'ImageId': 'ami-08d431591b88d4335',
'ImageLocation': '068359028657/TEST 2 Snapshots',
'ImageType': 'machine',
'Public': False,
'OwnerId': '068359028657',
'ProductCodes': [
{
'ProductCodeId': '3f8t6t8fp5m9fdsxx18yzwriozxi',
'ProductCodeType': 'marketplace'
}
],
'State': 'available',
'BlockDeviceMappings': [
{
'DeviceName': '/dev/xvda',
'Ebs': {
'Encrypted': False,
'DeleteOnTermination': True,
'SnapshotId': 'snap-0bec32fc28fdf885d9a',
'VolumeSize': 100,
'VolumeType': 'gp2'
}
},
{
'DeviceName': '/dev/sdf',
'Ebs': {
'Encrypted': False,
'DeleteOnTermination': True,
'SnapshotId': 'snap-0782adffd4e3c7e2d2a',
'VolumeSize': 300,
'VolumeType': 'gp2'
}
}
],
'Description': 'AMI backup generated by Lambda',
'Hypervisor': 'xen',
'Name': 'TEST 2 Snapshots',
'RootDeviceName': '/dev/xvda',
'RootDeviceType': 'ebs',
'SriovNetSupport': 'simple',
'VirtualizationType': 'hvm'
},
{
'Architecture': 'x86_64',
'CreationDate': '2019-07-10T09:20:18.000Z',
'ImageId': 'ami-0ae6a5d15f87fsdada4',
'ImageLocation': '068359028657/TEST 2 Snapshots 2',
'ImageType': 'machine',
'Public': False,
'OwnerId': '06835902fds8657',
'ProductCodes': [
{
'ProductCodeId': '3f8t6ddt8fp5m9xx18yzwriozxi',
'ProductCodeType': 'marketplace'
}
],
'State': 'available',
'BlockDeviceMappings': [
{
'DeviceName': '/dev/xvda',
'Ebs': {
'Encrypted': False,
'DeleteOnTermination': False,
'SnapshotId': 'snap-0d74e92744efs49f504',
'VolumeSize': 100,
'VolumeType': 'gp2'
}
},
{
'DeviceName': '/dev/sdf',
'Ebs': {
'Encrypted': False,
'DeleteOnTermination': False,
'SnapshotId': 'snap-04fa670e447fs8b296e',
'VolumeSize': 300,
'VolumeType': 'gp2'
}
}
],
'Description': 'TEST 2 Snapshots 2',
'Hypervisor': 'xen',
'Name': 'TEST 2 Snapshots 2',
'RootDeviceName': '/dev/xvda',
'RootDeviceType': 'ebs',
'SriovNetSupport': 'simple',
'VirtualizationType': 'hvm'
}
],
'ResponseMetadata': {
'RequestId': 'b32d0a96-ssce50-4c97-9722-f7c0f72fsfce88',
'HTTPStatusCode': 200,
'HTTPHeaders': {
'content-type': 'text/xml;charset=UTF-8',
'content-length': '4277',
'vary': 'accept-encoding',
'date': 'Wed, 10 Jul 2019 10:18:15 GMT',
'server': 'AmazonEC2'
},
'RetryAttempts': 0
}
}
如何在Eb上包含一个循环以提取所有值,而不仅是第一个?