Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete an AMI using boto?

(cross posted to boto-users)

Given an image ID, how can I delete it using boto?

like image 685
ripper234 Avatar asked Mar 15 '11 15:03

ripper234


People also ask

How do I delete my AMI image?

In the navigation pane, choose Snapshots. Select a snapshot to delete (look for the AMI ID from the prior step in the Description column). Choose Actions, Delete snapshot. When prompted for confirmation, choose Delete.


2 Answers

You use the deregister() API.

There are a few ways of getting the image id (i.e. you can list all images and search their properties, etc)

Here is a code fragment which will delete one of your existing AMIs (assuming it's in the EU region)

connection = boto.ec2.connect_to_region('eu-west-1', \
                                    aws_access_key_id='yourkey', \
                                    aws_secret_access_key='yoursecret', \
                                    proxy=yourProxy, \
                                    proxy_port=yourProxyPort)


# This is a way of fetching the image object for an AMI, when you know the AMI id
# Since we specify a single image (using the AMI id) we get a list containing a single image
# You could add error checking and so forth ... but you get the idea
images = connection.get_all_images(image_ids=['ami-cf86xxxx'])
images[0].deregister()

(edit): and in fact having looked at the online documentation for 2.0, there is another way.

Having determined the image ID, you can use the deregister_image(image_id) method of boto.ec2.connection ... which amounts to the same thing I guess.

like image 78
liamf Avatar answered Sep 27 '22 22:09

liamf


With newer boto (Tested with 2.38.0), you can run:

ec2_conn = boto.ec2.connect_to_region('xx-xxxx-x')
ec2_conn.deregister_image('ami-xxxxxxx')

or

ec2_conn.deregister_image('ami-xxxxxxx', delete_snapshot=True)

The first will delete the AMI, the second will also delete the attached EBS snapshot

like image 30
katriel Avatar answered Sep 27 '22 22:09

katriel