Sometimes you need to rescue the data stored on AWS EBS volume that is attached to an original Amazon EC2 instance in your production environment. In case the instance is terminated or inaccessible, you can still access the data stored on that Instance EBS.
The following steps are useful when handling a failure scenario:
- You need to detach the volume from the original EC2 instance after stopping it.
- Attach the same volume to the newly launched EC2 instance.
- After remounting the volume, the data can be easily accessible.
The Step-By-Step Re-Mounting Process
Amazon Elastic Block Store (Amazon EBS) allows us to create storage volumes and attach them to Amazon EC2 instances. You can attach and detach the EBS volume to any EC2 instance and mount it after creating a file system on top of these volumes. Below is the detailed step-by-step process for re-mounting EBS volume to Amazon EC2 instance. Attaching EBS Volume to EC2 Instance
- Open AWS Console.
- Click the Volume section within AWS console under the EC2 dashboard.
- Select the EBS Volume that you want to attach to an EC2 instance. The EBS volume should be in available status.
- Enter the Instance ID or the Instance name. Make sure that the Amazon EBS volume and the Amazon EC2 instance are in the same availability zone.
- After attaching the volume, you will be able to see identical block devices for an EC2 instance.
- Congratulations ! You have successfully attached the Amazon EBS volume to an Instance. Now you need to ssh to the instance and mount the device to a mount directory.
- How To Mount EBS VolumeLog in to an instance through ssh and list the disk attached to the instance by using the command: fdisk -l
- Depending on the block device or driver of the kernel, the device might be attached with a different name than what you specified. For example, if we specify a device name of /dev/sdf, our device might be renamed /dev/xvdf by the kernel. In most cases, the trailing letter remains the same.
- List the block device by using the command: lsblk
- Check the file system on the attached volume: file -s /dev/xvdf
- EBS volumes restored from snapshots often have a file system on them already. If it’s not a new volume, we need to create a file system for them before we can mount them. We can do this by using: mkfs.ext4 /dev/xvdf
- Create a mount directory where you want to mount the device: mkdir /mountdir
- Open the /etc/fstab file and add a new line at the end of file, as demonstrated in the screenshot below. Adding this entry is important. This will make sure even after rebooting the instance directory, mounting will begin.device_name mount_point file_system_type fs_mntops fs_freq fs_passn /dev/xvdf /mountdir ext4 defaults, nofail 0 2
- Run below-mentioned mount command to mount all of the filesystems in /etc/fstab file. mount -a
- Check the disk’s free space by using the command below df -h Congratulations! You can see the /dev/xvdf device size and also see that it’s mounted on mountdir.Amazon EBS Re-Mounting Best PracticesUsing EBS is easy. However, there are few things that needs to be taken care of while re-mounting the EBS to an EC2 Instance.
- You must unmount the volume before you detach it.
- If the EBS volume is inside the root device of an instance then you must stop the original EC2 instance before detaching the volume.
- Make sure that the EBS volume and new instances the same availability zone.
- Make sure to check how many volumes you can attach to your instance. It depends on your operating system. (refer to http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/volume_limits.html)
- Volumes that have been restored from snapshots, or have already been attached to an instance, likely have a file system on them. if you create a new file system on the top of an existing file system, the operation overwrites your data. So, only in the case of a new volume, should you create a file system.
Connecting a Volume with Windows server:
Let’s assume that we have a Windows 2012 server already launched with 30GB of EBS volume, and we would like to add another EBS volume of 30GB. First we need to detach the volume from current instance.
To do so using AWS console, we need to go to Volumes tab, select the volumes, and detach the volume from the previous instance. This can be done with the AWS CLI command as well.
$aws ec2 detach volume --volume-id vol-5erd004ar
After detaching the volume, we attach the volume to current Windows instances: Selecting the instance to attach with newly created volume. The volume is now attached to the windows instance, but the newly attached volume is not working yet.
To bring that volume online and define a new Drive letter, we have to log in to our Windows instance.
- From the windows server manager, click on the start menu open ‘Server Manager’.
- From Manager, you can navigate to ‘File and Storage Services’ => ‘Volumes’
- Bringing online the new volume simply right click on the volume.
- After getting online, right click on volume and select ‘New Volume’:
- Click ‘Next’ to begin the process of defining a new drive letter: ‘Disk1’ is the newly created volume of 30GB.
- Click Next’ to proceed.
- Specify the size and click ‘Next’: Specify the drive letter.
- When you reach the File system setting ‘NTFS’, click ‘Next’
- ‘Create’ will create the new volume drive letter which is ‘D:\’ and brings it online with the current instance.
The new volume is attached with Windows instance as ‘New Volume (D: ). Extend the volume in the Windows server:
- We need to log into our Windows server using Remote Desktop
- Once we are in our Windows server, we will select Server Management and then Disk Management.
Right click the drive and click on ‘Extend’. That will navigate you to the next page, where we will add our new volume and allocate the space in MB (MegaByte). Then, you need to click on next to expand our existing volume. This will merge both (attached and existing) EBS volumes into one complete volume.
Using the AWS-CLI
AWS CLI gives us the opportunity to replicate the task from the command line. From a Linux EC2 instance, we can install aws cli by committing:
$ sudo apt-get install -y python-pip $ sudo pip install awscli Adding access key id and secret access key in $HOME/.aws/config [default]
aws_access_key_id = <access key id>
aws_secret_access_key = <secret access key>
region = us-east-1
$ export AWS_CONFIG_FILE=$HOME/.aws/config
For windows instance:
C:\> Import-Module "C:\Program Files (x86)\AWS Tools\PowerShell\AWSPowerShell\AWSPowerShell.psd1"
C:\> Import-Module AWSPowerShell
To verify the module is loaded type:
AWS CLI has been installed and configured for both Windows and Linux servers.
To create a new EBS volume, we need to use the below-mentioned command:
$ aws ec2 create-volume --size 30 --region us-east-1 --availability-zone us-east-1a
This will create a volume of 30GB. When checking it in the portal volume section the created volume of 30GB will have a volume ID. For example, the volume ID is ‘vol-5erd004ar’. In next step, we will add this newly created EBS volume with our current EC2-instance.
To attach a volume with an instance, we commit the command given below:
$ aws ec2 attach-volume --volume-id vol-5erd004ar --instance-id i-0e9c2ah52 --device /dev/xvdf
(Here instance id is the currently using ec2-instance id and volume id is newly created volume which we created in previous step also a device name ‘/dev/xvdf’, since the root volume for the current instance has the block devices of ‘/dev/xvda’).