How can I delete an image (AMI) in AWS EC2 using the SDK?

两盒软妹~` 提交于 2019-12-11 11:13:08

问题


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#?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/6512539/how-can-i-delete-an-image-ami-in-aws-ec2-using-the-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!