When we deregister an Amazon EBS-backed AMI, it doesn't affect the snapshots that were created during the AMI creation process. We'll continue to incur storage costs for these snapshots. Therefore, if we are finished with the snapshots, we should delete it.
In fact, they cant do the mistake of cleaning up snapshots, its a revenue for them!
So we will have to take care of cleaning up snapshots.
Here is how we can do it from AWS Java SDK.
Download AWS Java SDK from here
Add to java classpath/buildpath.
import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import com.amazonaws.auth.BasicAWSCredentials; import com.amazonaws.regions.Region; import com.amazonaws.regions.Regions; import com.amazonaws.services.ec2.AmazonEC2Client; import com.amazonaws.services.ec2.model.DeleteSnapshotRequest; import com.amazonaws.services.ec2.model.DescribeImagesRequest; import com.amazonaws.services.ec2.model.DescribeImagesResult; import com.amazonaws.services.ec2.model.DescribeSnapshotsRequest; import com.amazonaws.services.ec2.model.DescribeSnapshotsResult; import com.amazonaws.services.ec2.model.Image; import com.amazonaws.services.ec2.model.Snapshot; public class FindSanpshots { public static void main(String[] args) throws IOException { BasicAWSCredentials basicAWSCredentials = new BasicAWSCredentials("xx", "yyy"); AmazonEC2Client amazonEC2Client = new AmazonEC2Client(basicAWSCredentials); Region region = Region.getRegion(Regions.fromName("us-west-2")); amazonEC2Client.setEndpoint(region.getServiceEndpoint("ec2")); DescribeImagesRequest withOwners = new DescribeImagesRequest().withOwners("self"); DescribeImagesResult images = amazonEC2Client.describeImages(withOwners); ArrayList < String > imageIdList = new ArrayList < String > (); List < Image > amiList = images.getImages(); for (Image image: amiList) { imageIdList.add(image.getImageId()); } DescribeSnapshotsRequest withOwnerIds = new DescribeSnapshotsRequest(). withOwnerIds("self"); DescribeSnapshotsResult describeSnapshots = amazonEC2Client. describeSnapshots(withOwnerIds); List < Snapshot > snapshots = describeSnapshots.getSnapshots(); // ensure snapshot size and ami size in your region. System.out.println(snapshots.size()); System.out.println(amiList.size()); int count = 0; int size = 0; // find orphans and delete. for (Snapshot snapshot: snapshots) { String description = snapshot.getDescription(); // get AMI id of snapshot using regex from its description. Pattern pattern = Pattern.compile("for(.*?)from"); Matcher matcher = pattern.matcher(description); while (matcher.find()) { String amiId = matcher.group(1).trim(); // ami id is currently 12 character long. if (!imageIdList.contains(amiId) && amiId.length() <= 12) { String snapshotId = snapshot.getSnapshotId(); DeleteSnapshotRequest r = new DeleteSnapshotRequest(snapshotId); amazonEC2Client.deleteSnapshot(r); System.out.println(amiId); size += snapshot.getVolumeSize(); count++; } } } System.out.println("Orphan Snapshots Deleted : " + count); System.out.println("Orphan Snapshots Size : " + size); } }
Nice blog... Thanks for sharing script. Share the difference in its pricing and AWS snapshot pricing.
ReplyDeleteery informative post, really help me.
ReplyDeleteNaresh IT is The Best Software Training Institute, It Provides AWS Online Training, From this AWS Online Course you will be able to learn all the Concepts of AWS with real-time Industry Exports, Having the combined experience of more than 10+ yrs in the industry.