在boto3

问题描述

使用boto3,当我在describe_images()中过滤图像时,以“ platform”作为名称,只有在使用“ windows”作为值时,并且在尝试使用linux或ubuntu时,我才发现过滤结果似乎正常工作,如何获取Linux映像?

def describe_images(images,platform = ['windows'] image_type = ['machine'],architecture = ['x86_64'],owners = ['amazon'],root_device_type = ['instance-store']):#Not working when platform = ['linux']
global ec2

def describe(ec2):
    try:
        describe_img = ec2.describe_images(Filters = [{'Name' : 'platform','Values' : platform},{'Name' : 'image-type','Values' : image_type},{'Name' : 'architecture','Values' : architecture},{'Name' : 'root-device-type','Values' : root_device_type}],Owners = owners,DryRun = False)
    except:
        raise
    else:
        for img in describe_img['Images']:
            #print('---img:',img)
            images.append(img['ImageId'])
            #print('---img[]:',img['ImageId'])
        print('len images:',len(images))
        print('---images:',images)
    
try:
    #print('describe images entered')
    describe_img = ec2.describe_images(DryRun = True)

except ClientError as e:
    #print('--if dry run op in e:','DryRunoperation' in str(e))
    if 'DryRunoperation' not in str(e):
        tkinter.messageBox.showerror(title = 'Error',message = 'It is found that the account doesn\'t have the permission(s) required to see the images. Please try again after obtaining the required permission(s).')
    elif 'DryRunoperation' in str(e):
        describe(ec2)
    else:
        raise
#print('leaving describe images.')

解决方法

platform选项仅适用于Windows(来自docs):

对于Windows AMI,此值设置为windows;否则为空白。

因此获取Linux映像不要使用 platform过滤器。

但是,目前我不确定是否不使用过滤器是否还会包含Windows。如果是,那么您要么稍后将其过滤掉,要么使用其他选项仅针对特定于Linux映像的过滤。