Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boto3: Get EC2 images owned by me

I would like to get all ami images owned by me. I tried something like below:

ec2 = boto3.resource('ec2')
owner_id = 'owner_id'
filters = [{'Name': 'owner-id', 'Values': [owner_id]}]
images = ec2.images.filter(Filters=filters).all()

But I need to put owner_id explicid in the code. Is it any solution to do that automatically from aws credentials?

like image 519
drootnar Avatar asked Oct 24 '16 07:10

drootnar


2 Answers

You should be able to use self for the owner. This is what I use.

boto3conn = boto3.resource("ec2", region_name="us-west-2")
images = boto3conn.images.filter(Owners=['self']) 
like image 124
iamguest Avatar answered Sep 26 '22 07:09

iamguest


This will help, It will show you ALL AMI thats owned by your aws account

import boto3

client = boto3.client('ec2', region_name='us-east-1')

response = client.describe_images(Owners=['self'])
for ami in response['Images']:
  print (ami['ImageId'])

like image 38
CloudNinja Avatar answered Sep 23 '22 07:09

CloudNinja