首页 > 解决方案 > 如何根据状态从 AWS 控制台查询图像 AMI:使用 Python boto3 可用?

问题描述

我需要根据状态从 AWS 控制台获取图像 AMI 的详细信息:可用。

当我尝试时它被卡住并且没有打印任何行。

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

此图像 AMI 是否有任何确切的关键字,请纠正我

标签: pythonimageboto3amazon-ami

解决方案


关于 AMI,需要意识到的重要一点是提供了每个AMI。

如果您只想列出属于您自己账户的 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']

推荐阅读