简体   繁体   English

如何根据其状态从AWS控制台查询图像AMI:可通过Python boto3使用?

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

I need to get the Details of Images AMI's from AWS Console based on their State: Available. 我需要根据其状态从AWS Console获取图像AMI的详细信息:可用。

When I tried it is getting stuck and not printing any line. 当我尝试时,它卡住了,无法打印任何行。

Python code 1: Python代码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 此图像AMI是否有确切的关键字,请指正我

If you want to do your own filtering change describe_images(Filters...) to describe_images() . 如果要进行自己的过滤,请将describe_images(Filters...)更改为describe_images() Note: describe_images() returns a lot of data. 注意: describe_images()返回大量数据。 Be prepared to wait a few minutes. 准备等待几分钟。 On my system 89,362 images for us-east-1. 在我的系统上,us-east-1的图像为89,362张。

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)))

An important thing to realize about AMIs is that every AMI is provided. 关于AMI的重要一点是要提供每个 AMI。

If you only wish to list the AMIs belonging to your own account, use Owners=self : 如果仅希望列出属于您自己帐户的AMI,请使用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']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM