Is Data Recovery Possible?
Have you ever thought about what to do in case your EC2 instance is lost? Many people believe that's not possible. I have been using AWS services and EC2 Instances for my websites for more than a year now, and I can tell you it is indeed possible to recover your data.
Reasons for Losing EC2 Instances
There are two primary issues I have faced with EC2 Instances where I needed to restore or recover data:
1. Sudden Instance Failure
Suddenly, my running instance got failed for no reason. When I checked its health status, both checks were failing. In that case, you can't get the same instance running again. Neither AWS support can make that instance run again, as happened with me.
2. Auto Scaling Group Misconfiguration
When you create an Auto Scaling Group, there are a few options that you need to be alert about: Max Range, Min Range, and Desired Instance.
- If you set min range to 0 (zero) or your Desired to 0, there are high chances you might lose the instance if scaling policies trigger a scale-in event.
The Concept: EBS Volumes Persist
What to do to recover the data when an Instance is terminated, corrupted, or lost?
In AWS, every instance creates a volume (EBS Volume). When an instance is lost or corrupted, their volume usually still exists there. It is like your computer system is not in a running stage, but the Hard Disk is still there and intact.
Steps to Recover Lost EC2 Instance Data
To get the latest data from your last running instance, you can create a new instance and attach the old volume (from the lost instance) to this new instance.
Procedure for Attaching Volume & Getting Data:
- Create a New Instance: Launch a new EC2 instance in the same Availability Zone as your old volume.
- Stop the Instance: Change its State to Stop immediately after launch.
- Detach the New Volume: Detach the root volume that got associated with it at the time of creation (you won't need this blank drive).
- Identify the Old Volume: Find the volume of your last instance. If you have many, check the "Creation Date" to guess which one was your last volume.
- Attach the Old Volume: Attach this old volume to your new instance.
- Make it Root: When attaching the volume, ensure you mount it as the root device (e.g.,
/dev/xvdaor/dev/sda1), otherwise, the Instance will not start.
Once attached, start the instance. It should boot up with all your old data intact!