How to recover accidentally deleted AMIs or EBS Snapshots using EC2 Recycle Bin
Sh*t happens. We are humans and we learn from our mistakes so today let’s learn to restore accidentally deleted AWS AMIs.
I’m sure many of us might have unintentionally deleted an AMI at some point in time. I have done it a couple of times. Anyway, let’s save ourselves from that Oops moment by using the EC2 Recycle Bin feature.
Alright, let’s sign in to the AWS Management Console and navigate to Recycle Bin service.
Recycle Bin
Recycle Bin supports recovering EBS snapshots and AMIs, but we need to create the retention rule separately. Let’s create the retention rule for recovering AMIs first.
AMI Retention Rule
You can either use tags to filter the resources or apply the rule to all the resources. For simplicity, we will create the rule to cover all the AMIs and retain them for 1 day.
Great so our retention rule for AMI is ready.
It’s time to create a retention rule for our EBS snapshot. So let’s continue.
EBS Retention Rule
Just like our AMIs, we will retain our EBS snapshots for 1 day and the rule will cover all the snapshots.
Alright, so our EBS snapshot retention rule is ready too.
Test Phase
Time to test the rule so let’s head over to the EC2 service and visit the AMI page to deregister an AMI owned by you. I created one for this article so I’ll deregister the same.
Select an AMI owned by you, click on Actions dropdown and deregister it.
Note: Deregistering an AMI does not delete the associated snapshot automatically let's switch to the Snapshots page and delete the associated snapshot as well.
Select the associated EBS snapshot, click on Actions dropdown and delete it.
Let’s navigate back to Recycle Bin service and go to the Resources page using the navigation in the left panel to see if our AMI and snapshot were retained.
Perfect, so both my EBS snapshot and AMI were retained and I hope yours were retained too.
So, after deregistering the AMI we learn that it was a golden AMI for spinning up dev instances but we don’t have to worry since we have the retention rule in place and both the snapshot and AMI are safe.
Recovery Phase
Let’s recover the AMI and snapshot.
Note: As the AMI depends on the snapshot we first need to restore the snapshot else we will receive an error while restoring the AMI.
So, let’s restore our snapshot by selecting it and clicking on the Recover button.
Now let’s restore the AMI. Switch to AMIs using the dropdown, select the AMI and click on the Recover button.
Alright, once the resources are recovered let’s navigate back to the EC2 service to confirm if both the AMI and EBS snapshot has been successfully restored.
Yay! Both of them have been restored.
Covering the basics
-
Earlier deleted or deregistered AMIs could not be recovered or restored but with the introduction of the EC2 Recycle Bin feature you could recover or restore both a deleted or deregistered AMI and a deleted EBS snapshot if you have retention rules in place.
-
You can automate EC2 instance recovery in case of system or instance health check failure. You can create a CloudWatch alarm to monitor both system and instance health for an EC2 instance and assign an action to recover or reboot respectively when the alarm is triggered. You can manage the creation of these alarms using Terraform.
-
Conditions under which you will be able to retrieve data after an EC2 instance has been terminated:
An EBS snapshot exists for that EC2 instance. The freshness of data depends on the time difference between instance termination and snapshot creation
EBS volumes were not terminated upon instance termination. In this case, you could launch a new instance and attach these volume(s) to the new instance to retrieve the data
If none of the conditions are satisfied then, unfortunately, you cannot retrieve data from an EC2 instance that has been terminated.
-
EBS snapshot can be restored to an EC2 instance in the following way:
In EC2, switch to the snapshots page using the left navigation panel
Select the snapshot that you want to restore
Click on Actions dropdown followed by Create Volume
Provide or update the required inputs and proceed with volume creation
Once the volume is in an Available state, select it to attach it to the EC2 instance at an appropriate path by using Attach Volume option available within the Actions dropdown
Finally, depending on the instance OS type you might have to login within the OS and mount the volume
-
Using the EC2 Recycle Bin feature you can recover or restore from accidental deletion of EBS snapshots or EC2 AMIs