How to Query Images AMI's from AWS Console based on their status : Available using Python boto3?

霸气de小男生 提交于 2020-01-14 05:44:08

问题


I need to get the Details of Images AMI's from AWS Console based on their State: Available.

When I tried it is getting stuck and not printing any line.

Python code 1:

conn = boto3.resource('ec2')
image = conn.describe_images()
print(image) # prints nothing
for img in image:
  image_count.append(img)
print("img count ->" + str(len(image_count)))
#prints nothing

Is there any exact keywords for this Image AMI's Please correct me


回答1:


An important thing to realize about AMIs is that every AMI is provided.

If you only wish to list the AMIs belonging to your own account, use Owners=self:

import boto3

ec2_client = boto3.client('ec2')

images = ec2_client.describe_images(Owners=['self'])

available = [i['ImageId'] for i in images['Images'] if i['State'] == 'available']



回答2:


If you want to do your own filtering change describe_images(Filters...) to describe_images(). Note: describe_images() returns a lot of data. Be prepared to wait a few minutes. On my system 89,362 images for us-east-1.

import boto3

client = boto3.client('ec2')

image_count = []

response = client.describe_images(Filters=[{'Name': 'state', 'Values': ['available']}])

if 'Images' in response:
    for img in response['Images']:
        image_count.append(img)

print("img count ->" + str(len(image_count)))


来源:https://stackoverflow.com/questions/52329817/how-to-query-images-amis-from-aws-console-based-on-their-status-available-usi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!