如何使用 boto 删除 AMI?

发布于 2024-10-22 05:16:21 字数 155 浏览 2 评论 0原文

(交叉发布到 boto-users

给定图片 ID,如何使用 boto 删除它?

(cross posted to boto-users)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

沉鱼一梦 2024-10-29 05:16:21

使用较新的 boto(使用 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 快照

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

月光色 2024-10-29 05:16:21

您使用 deregister() API。

有几种获取图像 ID 的方法(即您可以列出所有图像并搜索其属性等)

以下是一个代码片段,它将删除您现有的 AMI 之一(假设它位于欧盟地区)

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) 方法...我猜这与我猜的相同。

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.

等风也等你 2024-10-29 05:16:21

对于 Boto2,请参阅 katriels 答案。在这里,我假设您使用的是 Boto3。

如果您有 AMI(boto3.resources.factory.ec2.Image 类的对象),您可以调用其取消注册 函数。例如,要删除具有给定 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 successed, but DryRun flag is set 异常。要摆脱该示例,请省略 DryRun 并使用:

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

这篇博文详细介绍了如何使用 Boto3 删除 AMI 和快照。

For Boto2, see katriels answer. Here, I am assuming you are using Boto3.

If you have the AMI (an object of class boto3.resources.factory.ec2.Image), you can call its deregister function. For example, to delete an AMI with a given ID, 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 an Request would have succeeded, but DryRun flag is set exception. To get rid of the example, leave out DryRun and use:

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

This blog post elaborates on how to delete AMIs and snapshots with Boto3.

2024-10-29 05:16:21

脚本删除 AMI 及其关联的快照。确保您具有运行此脚本的正确权限。

输入 - 请传递区域和 AMI id(n) 作为输入

import boto3
import sys

def main(region,images):
    region = sys.argv[1]
    images = sys.argv[2].split(',') 
    ec2 = boto3.client('ec2', region_name=region)
    snapshots = ec2.describe_snapshots(MaxResults=1000,OwnerIds=['self'])['Snapshots']
        # loop through list of image IDs
    for image in images:
        print("====================\nderegistering {image}\n====================".format(image=image))
        amiResponse = ec2.deregister_image(DryRun=True,ImageId=image)
        for snapshot in snapshots:
            if snapshot['Description'].find(image) > 0:
                snap = ec2.delete_snapshot(SnapshotId=snapshot['SnapshotId'],DryRun=True)
                print("Deleting snapshot {snapshot} \n".format(snapshot=snapshot['SnapshotId']))
    
main(region,images)

Script delates the AMI and associated Snapshots with it. Make sure you have right privileges to run this script.

Inputs - Please pass region and AMI ids(n) as inputs

import boto3
import sys

def main(region,images):
    region = sys.argv[1]
    images = sys.argv[2].split(',') 
    ec2 = boto3.client('ec2', region_name=region)
    snapshots = ec2.describe_snapshots(MaxResults=1000,OwnerIds=['self'])['Snapshots']
        # loop through list of image IDs
    for image in images:
        print("====================\nderegistering {image}\n====================".format(image=image))
        amiResponse = ec2.deregister_image(DryRun=True,ImageId=image)
        for snapshot in snapshots:
            if snapshot['Description'].find(image) > 0:
                snap = ec2.delete_snapshot(SnapshotId=snapshot['SnapshotId'],DryRun=True)
                print("Deleting snapshot {snapshot} \n".format(snapshot=snapshot['SnapshotId']))
    
main(region,images)
歌入人心 2024-10-29 05:16:21

使用 EC2.Image 资源,您可以简单地调用 deregister():

示例:

for i in ec2res.images.filter(Owners=['self']):
    print("Name: {}\t Id: {}\tState: {}\n".format(i.name, i.id, i.state))
    i.deregister()

请参阅此以了解如何使用不同的过滤器:
为 ec2 记录的有效值是什么。 images.filter 命令?

另请参阅:https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Image.deregister

using the EC2.Image resource you can simply call deregister():

Example:

for i in ec2res.images.filter(Owners=['self']):
    print("Name: {}\t Id: {}\tState: {}\n".format(i.name, i.id, i.state))
    i.deregister()

See this for using different filters:
What are valid values documented for ec2.images.filter command?

See also: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Image.deregister

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文