fbpx
Search
Close this search box.

How to Connect an AWS EBS Volume to Another EC2 Instance

How to Connect an AWS EBS Volume to Another EC2 Instance
Learn how to re-mount an existing AWS EBS volume to a new EC2 instance to rescue the data of a volume attached to an older EC2 instance.
Share This Post

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.
Example architecture diagram for N2WS Backup & Recovery on AWS.
The easier way to connect EBS to a new EC2

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 Volume: Log into 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
  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     2
      fstabfile
  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
    df -h
    diskfree
    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.
  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 amazon’s user guide)
  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.

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.

  1. From the windows server manager, click on the start menu open ‘Server Manager’.
  2. From Manager, you can navigate to ‘File and Storage Services’ => ‘Volumes’
  3. Bringing online the new volume simply right click on the volume.
  4. After getting online, right click on volume and select ‘New Volume’:
  5. Click ‘Next’ to begin the process of defining a new drive letter: ‘Disk1’ is the newly created volume of 30GB.
  6. Click Next’ to proceed.
  7. Specify the size and click ‘Next’: Specify the drive letter.
  8. When you reach the File system setting ‘NTFS’, click ‘Next’
  9. ‘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:

  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.

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.

Tips from the Expert
Picture of Sebastian Straub
Sebastian Straub
Sebastian is the Principle Solutions Architect at N2WS with more than 20 years of IT experience. With his charismatic personality, sharp sense of humor, and wealth of expertise, Sebastian effortlessly navigates the complexities of AWS and Azure to break things down in an easy-to-understand way.

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.

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

Next step

The easier way to connect EBS to a new EC2

Allowed us to save over $1 million in the management of AWS EBS snapshots...

a mockup of an ipad with the disaster-proof backup checklist on the screen
N2WS AWS Backup & Recovery logo

What your backup plan is missing...

Get this easy yet comprehensive checklist to fortify your backup plan across every critical dimension.

N2WS vs AWS Backup

Why chose N2WS over AWS Backup? Find out the critical differences here.

N2WS in comparison to AWS Backup, offers a single console to manage backups across accounts or clouds. Here is a stylized screenshot of the N2WS dashboard.