6

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.

anothernode
  • 5,100
  • 13
  • 43
  • 62
New_to_work
  • 253
  • 2
  • 5
  • 16
  • Could you add more detail? What exactly does "not all the images" mean? How do you know there are images missing? What exactly do you expect the output to look like and why do you expect it to look like that? And how exactly does the actual output differ from the one you are expecting? – anothernode Oct 05 '18 at 09:55

2 Answers2

19
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!).

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
  • 5
    I would add that describe_images() returns a dict. Its well documented here: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.describe_images – Cale Sweeney May 20 '20 at 18:53
0
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

Shubham Jain
  • 199
  • 1
  • 4
  • 15
  • Not exactly. It will print list of AMI of all instances you're running. It can be own private AMIs and publicly available AMI. But this method will omit everything that is not an active instance right now. – wisp Nov 17 '20 at 11:39