问题
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