2011-03-15 41 views

回答

6

您使用deregister()API。

有获取图像ID的一些方法(比如,你可以列出所有图像,并搜索其属性等)

下面是这将删除现有的AMI(假设它是在一个代码片段EU地区)

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() 

(编辑):事实上,查看2.0的在线文档,还有另一种方法。

已经确定了图像ID,你可以使用boto.ec2.connection的deregister_image(image_id)方法,这相当于我猜测的相同的东西。

+0

顺便说一句,这与博托2.0b4,最新的。我没有检查,看看这是否适用于早期版本(例如最新的Ubuntu回购,例如有1.9.something) – liamf 2011-04-28 23:03:05

7

对于较新的伯特(与2.38.0测试),你可以运行:

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

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

第一个将删除该AMI,第二个也将删除附加EBS快照

+6

如何做同样的Boto3?问题在于,您无法使用Boto3中的deregister_image()函数删除快照。在Boto3中可以采用其他方式吗? http://boto3.readthedocs.org/en/latest/reference/services/ec2.html#EC2.Client.deregister_image – tom 2015-08-06 07:49:13

+0

@tom你找到了解决方案吗?陷入同样的​​问题 – 2017-08-22 09:54:24

+0

为Boto3添加了一个答案:https://stackoverflow.com/a/48445252/783510 – 2018-01-25 14:32:15

0

对于Boto2,请参阅katriels answer。在这里,我假设你正在使用Boto3。

如果您有AMI(类别为boto3.resources.factory.ec2.Image的对象),则可以调用它的deregister函数。例如,与给定的ID删除一个AMI,您可以使用:

import boto3 

ec2 = boto3.resource('ec2') 

ami_id = 'ami-1b932174' 
ami = list(ec2.images.filter(ImageIds=[ami_id]).all())[0] 

ami.deregister(DryRun=True) 

如果有必要的权限,你应该看到一个Request would have succeeded, but DryRun flag is set例外。为了摆脱的例子,离开了DryRun和使用:

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

This blog post阐述了如何删除的AMI和快照与Boto3。