AWS AMIs refer to pre-configured bundled software that has the necessary information to launch an EC2 instance. We can say that AMI refers to a root device for the instance; when an EC2 instance is launched from that AMI it will have all the OS, software as well as the applications which were bundled when that AMI was created.
If you create an AMI from an existing instance, once the AMI is created, AWS creates a snapshot of the root storage as well as all the EBS volumes that are attached to the instance. When you deregister the AMI, it is simply deleted. However, all the snapshots that were attached to the AMI remain and need to be deleted manually.
The diagram below illustrates this concept. If you do not manually delete snapshots after deregistering an AMI, they will continue to incur costs.
This article delves into how to deregister AMIs as well as delete snapshots. In addition, we provide you with an automated script that deletes all snapshots that are associated with a respective AMI while deregistering the AMI.
Part 1:
Deregister AMIs and delete snapshots using the Amazon Management Console & CLI To deregister an AMI, follow the steps below:- Go to the AWS EC2 console and AMI section. Select the desired AMI. Next, select the ‘Deregister’ option in the ‘Actions’ tab.
- Select the ‘Continue’ option to deregister. The AMI is now deregistered.
You can perform the same steps using the AWS CLI, as shown below: (Note: It is assumed that AWS CLI has already been installed on the local machine.)aws ec2 deregister-image –image-id <ami-id>
As mentioned above, the snapshots associated with the deregistered AMI are not automatically deleted, requiring you to delete them manually. This process is outlined below:
- You can find the snapshots that are associated with the AMI using the AMI ID in the ‘Search’ option, as shown below:
- To delete the snapshot, select the snapshot and click on the ‘Delete’ option from the ‘Actions’ tab.
You can perform the same steps using the AWS CLI, as shown below: aws ec2 delete-snapshot –snapshot-id <snapshot-id>
Part 2:
Delete the AMI and snapshots simultaneously using Powershell and a Linux script At times, it can be tedious to delete an AMI and then locate all of its associated snapshots in order to delete them, as well. Taking that into consideration, a script has been created that can delete both an AMI and its associated snapshots, simultaneously. All you need to do is enter the AMI ID into the script. Two scripts have been created: one by PowerShell Script for Windows machines and the other by and for Linux. It is assumed that the AWS CLI was installed on both machines/instances and that the access credentials were set for the AWS account. If you run these scripts from an EC2 instance, it is recommended to use the AWS IAM Role for better security. PowerShell Script: $amiName = ‘<The users needs to mention their AMI ID>’ $myImage = Get-EC2Image $amiName $count = $myImage[0].BlockDeviceMapping.Count $mySnaps = @() for ($i=0; $i -lt $count; $i++) { $snapId = $myImage[0].BlockDeviceMapping[$i].Ebs | foreach {$_.SnapshotId} $mySnaps += $snapId } Write-Host “Unregistering” $amiName Unregister-EC2Image $amiName foreach ($item in $mySnaps) { Write-Host ‘Removing’ $item Remove-EC2Snapshot $item }- Flexible backup policies and schedules
- Consistent database backup, such as SQL Server, Oracle, MySQL, MongoDB and more
- Instance recovery, even with data located across AWS regions, within seconds
- “Pull” and “Push” based alerts and notifications
- Application consistent backup