如何使用 boto 删除 AMI?
(交叉发布到 boto-users)
给定图片 ID,如何使用 boto 删除它?
(cross posted to boto-users)
Given an image ID, how can I delete it using boto?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用较新的 boto(使用 2.38.0 进行测试),您可以运行:
或者
第一个将删除 AMI,第二个还将删除附加的 EBS 快照
With newer boto (Tested with 2.38.0), you can run:
or
The first will delete the AMI, the second will also delete the attached EBS snapshot
您使用 deregister() API。
有几种获取图像 ID 的方法(即您可以列出所有图像并搜索其属性等)
以下是一个代码片段,它将删除您现有的 AMI 之一(假设它位于欧盟地区)
(编辑):以及事实上查看了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)
(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.
对于 Boto2,请参阅 katriels 答案。在这里,我假设您使用的是 Boto3。
如果您有 AMI(
boto3.resources.factory.ec2.Image
类的对象),您可以调用其取消注册
函数。例如,要删除具有给定 ID 的 AMI,您可以使用:如果您拥有必要的权限,您应该会看到
Request would have successed, but DryRun flag is set
异常。要摆脱该示例,请省略DryRun
并使用:这篇博文详细介绍了如何使用 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 itsderegister
function. For example, to delete an AMI with a given ID, you can use: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 outDryRun
and use:This blog post elaborates on how to delete AMIs and snapshots with Boto3.
脚本删除 AMI 及其关联的快照。确保您具有运行此脚本的正确权限。
Script delates the AMI and associated Snapshots with it. Make sure you have right privileges to run this script.
使用 EC2.Image 资源,您可以简单地调用 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:
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