如何使用 boto3 获取所有 AWS AMI 的列表?

How to get the list of all AWS AMIs using boto3?

我想列出使用控制台和 Boto 3 可以看到的所有 AWS AMI(Amazon 机器映像)。

我已尝试使用 describe_instances() 获取 ImageID,但并未列出所有图像。

import boto3

ec2_client = boto3.client('ec2', region_name='ap-southeast-2') # Change as appropriate

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

这会列出您的帐户创建的所有 AMI。如果您省略 'self' 位,它将列出所有可公开访问的 AMI(而且列表很大!)。

import boto3
ec2 = boto3.client('ec2', region_name=region)
response = ec2.describe_instances()
for reservation in response["Reservations"]:
    for instance in reservation["Instances"]:
        print(instance["ImageId"])

这将为您提供您拥有的 aws 帐户中所有已用 AMI id 的列表