New_to_work
New_to_work

Reputation: 263

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

I want to list all the AWS AMI's (Amazon Machine Image) that I can see using the console and Boto 3.

I have tried using describe_instances() to get ImageIDs but not all the images are getting listed.

Upvotes: 6

Views: 21996

Answers (2)

Shubham Jain
Shubham Jain

Reputation: 199

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

This will give you list of all the used AMI ids in aws account you own

Upvotes: 0

John Rotenstein
John Rotenstein

Reputation: 269101

import boto3

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

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

This lists all AMIs that were created by your account. If you leave out the 'self' bit, it will list all publicly-accessible AMIs (and the list is BIG!).

Upvotes: 20

Related Questions