如何使用开发工具包删除 AWS EC2 中的映像 (AMI)?

发布于 2024-11-17 12:00:29 字数 162 浏览 3 评论 0原文

我正在使用 CreateImage() 从现有实例创建一个新的 AMI,我希望有一个 DeleteImage() 可以以相反的方式工作。不幸的是,EC2Client 上不存在此方法。

使用 C# 通过 SDK 删除 AMI 的正确方法是什么?

I am using CreateImage() to create a new AMI from an existing Instance and I was hoping there was a DeleteImage() which would work in the converse fashion. Unfortunately this method does not exist on the EC2Client.

What is the proper way to delete an AMI through the SDK using C#?

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

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

发布评论

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

评论(2

摘星┃星的人 2024-11-24 12:00:29

这是一个快速片段:

AmazonEC2 ec2 = AWSClientFactory.CreateAmazonEC2Client();

DeregisterImageRequest deregisterImageRequest = new DeregisterImageRequest();
deregisterImageRequest.ImageId = AMIName;

DeregisterImageResponse deregisterImageResponse = new DeregisterImageResponse();
deregisterImageResponse = ec2.DeregisterImage(deregisterImageRequest);

记住处理异常并删除快照。

但是,删除关联的快照存在问题。

如果您尝试使用DescribeImageAttributeRequest查找块设备映射,则会发生异常 - “未经授权尝试访问受限资源”:

DescribeImageAttributeRequest describeImageAttributeRequest = new DescribeImageAttributeRequest().WithImageId("ami-name").WithAttribute("blockDeviceMapping");

DescribeImageAttributeResponse describeImageAttributeResponse = new DescribeImageAttributeResponse();

describeImageAttributeResponse = ec2.DescribeImageAttribute(describeImageAttributeRequest);

请参阅帖子:
https://forums.aws.amazon.com/message.jspa?messageID=231972

Here is a quick snippet:

AmazonEC2 ec2 = AWSClientFactory.CreateAmazonEC2Client();

DeregisterImageRequest deregisterImageRequest = new DeregisterImageRequest();
deregisterImageRequest.ImageId = AMIName;

DeregisterImageResponse deregisterImageResponse = new DeregisterImageResponse();
deregisterImageResponse = ec2.DeregisterImage(deregisterImageRequest);

Remember to handle exceptions and remove the snapshots..

However there is an issue with deleting the associated snapshots.

If you try to find out the blockdevice mapping using DescribeImageAttributeRequest an exception occurs - "Unauthorized attempt to access restricted resource" :

DescribeImageAttributeRequest describeImageAttributeRequest = new DescribeImageAttributeRequest().WithImageId("ami-name").WithAttribute("blockDeviceMapping");

DescribeImageAttributeResponse describeImageAttributeResponse = new DescribeImageAttributeResponse();

describeImageAttributeResponse = ec2.DescribeImageAttribute(describeImageAttributeRequest);

See post:
https://forums.aws.amazon.com/message.jspa?messageID=231972

因为看清所以看轻 2024-11-24 12:00:29

有一个 DeregisterImage()那应该做你想做的事。请注意,您可以自行决定是否删除该映像随后可能基于的任何快照。

There is a DeregisterImage() that should do what you want. Note that it's up to you to delete any snapshots the image may be based upon afterward.

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