How to remove AMI using boto?

(crossroads placed in boto-users )

Given the id of the image, how to remove it with boto?

+4
source share
3 answers

You are using the deregister () API.

There are several ways to get the image ID (i.e. you can list all the images and search for their properties, etc.)

Here is a piece of code that will remove one of your existing AMIs (assuming it is 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 actually looking at the online documentation for 2.0, there is another way.

After defining the image id, you can use the deregister_image (image_id) method for boto.ec2.connection ... which is the same as what I think.

+6
source

With the new 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 remove the AMI, the second will also remove the attached EBS snapshot

+7
source

For Boto2, see mating catriels . Here I assume that you are using Boto3.

If you have an AMI (object of class boto3.resources.factory.ec2.Image ), you can call it deregister . For example, to remove an AMI with a given identifier, you can use:

 import boto3 ec2 = boto3.resource('ec2') ami_id = 'ami-1b932174' ami = list(ec2.images.filter(ImageIds=[ami_id]).all())[0] ami.deregister(DryRun=True) 

If you have the necessary permissions, you should see the exception Request would have succeeded, but DryRun flag is set . To get rid of the example, leave DryRun and use:

 ami.deregister() # WARNING: This will really delete the AMI 

This blog post details how to remove AMI and snapshots using Boto3.

0
source

Source: https://habr.com/ru/post/1343794/


All Articles