How to Connect an AWS EBS Volume to Another EC2 Instance

How-to Connect an AWS EBS Volume to Another Instance |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.

 

Get 24/7 Data Access and 100% Uptime with AWS + N2WS

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

  1. Open AWS Console.
  2. Click the Volume section within AWS console under the EC2 dashboard.
    volume
  3. Select the EBS Volume that you want to attach to an EC2 instance. The EBS volume should be in available status.
    attachvolume
  4. 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.
    attachvolumedialog
  5. After attaching the volume, you will be able to see identical block devices for an EC2 instance.
    blockdevice
  6. 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.
  7. 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
  8. 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.
  9. List the block device by using the command: lsblk
    lsblk
  10. Check the file system on the attached volume: file -s /dev/xvdf
  11. 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
    How to Connect an AWS EBS Volume to Another EC2 Instance
  12. Create a mount directory where you want to mount the device: mkdir /mountdir
  13. 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     2fstabfile
  14. Run below-mentioned mount command to mount all of the filesystems in /etc/fstab file.
    mount -a
  15. Check the disk’s free space by using the command below
    df -h
    diskfreeCongratulations! 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.
  16. You must unmount the volume before you detach it.
  17. If the EBS volume is inside the root device of an instance then you must stop the original EC2 instance before detaching the volume.
  18. Make sure that the EBS volume and new instances  the same availability zone.
  19. 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)
  20. 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.
How to Connect an AWS EBS Volume to Another EC2 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:

How to Connect an AWS EBS Volume to Another EC2 Instance

Selecting the instance to attach with newly created volume.
How to Connect an AWS EBS Volume to Another EC2 Instance

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, managerclick on the stat menu open ‘Server Manager’. From Manager, you can navigate to ‘File and Storage Services’ => ‘Volumes’ à Disks:

How to Connect an AWS EBS Volume to Another EC2 Instance

Bringing online the new volume simply right click on the volume:
How to Connect an AWS EBS Volume to Another EC2 Instance

After getting online, right click on volume and select ‘New Volume’:
How to Connect an AWS EBS Volume to Another EC2 Instance

Click ‘Next’ to begin the process of defining a new drive letter:
How to Connect an AWS EBS Volume to Another EC2 Instance

‘Disk1′ is the newly created volume of 30GB. Click Next’ to proceed:
How to Connect an AWS EBS Volume to Another EC2 Instance

Specify the size and click ‘Next’:
How to Connect an AWS EBS Volume to Another EC2 Instance

Specify the drive letter:
How to Connect an AWS EBS Volume to Another EC2 Instance

When you reach the File system setting ‘NTFS’, click ‘Next’:
How to Connect an AWS EBS Volume to Another EC2 Instance

‘Create’ will create the new volume drive letter which is ‘D:\’ and brings it online with the current instance:

How to Connect an AWS EBS Volume to Another EC2 Instance

How to Connect an AWS EBS Volume to Another EC2 Instance

The new volume is attached with Windows instance as ‘New Volume (D: ).
How to Connect an AWS EBS Volume to Another EC2 Instance

Extend the volume in the Windows server:

  1. We need to log into our Windows server using Remote Desktop
  2. Once we are in our Windows server, we will select Server Management and then Disk Management.

How to Connect an AWS EBS Volume to Another EC2 Instance

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.
How to Connect an AWS EBS Volume to Another EC2 Instance

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
then command:
$ 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:
C:\> Get-Module

AWS CLI has been installed and configured for both Windows and Linux servers.
1.       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.

2.       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’).

Share this post →

You might also like: