Detach an EBS Volume from an EC2 Instance and Attach It to Another

Types of Placement Groups in EC2

Can You Detach an EBS Volume from an EC2 Instance and Attach It to Another?

Yes! Amazon Elastic Block Store (EBS) volumes can be detached from one EC2 instance and attached to another. This is useful for:

βœ… Recovering data from an instance that is not booting
βœ… Migrating storage to another instance
βœ… Replacing an instance while keeping the same storage
βœ… Performing maintenance or troubleshooting


1. Important Considerations Before Detaching an EBS Volume

  • You cannot detach the root volume of a running EC2 instance unless you stop the instance first.
  • Data loss may occur if you detach an EBS volume without unmounting it first.
  • If the volume is encrypted, the target instance must have the required decryption permissions.
  • Multi-Attach is only supported for io1/io2 EBS volumes, but for most cases, an EBS volume can be attached to only one instance at a time.

2. Steps to Detach and Attach an EBS Volume (AWS Console & CLI)

πŸ“Œ Step 1: Unmount the Volume (Linux Only)

Before detaching, if the volume is mounted on a Linux instance, unmount it to prevent data corruption:

sudo umount /dev/xvdf

Use lsblk to check mounted volumes:

lsblk

πŸ“Œ Step 2: Detach the EBS Volume

πŸ”Ή Method 1: Using AWS Console

1️⃣ Open EC2 Dashboard
2️⃣ Click on Volumes under Elastic Block Store
3️⃣ Find the volume you want to detach
4️⃣ Click Actions β†’ Detach Volume
5️⃣ Confirm the detachment

πŸ“Œ Tip: If the volume is in use, stop the instance first.


πŸ”Ή Method 2: Using AWS CLI

1️⃣ Find the Volume ID of the attached volume:

aws ec2 describe-volumes --filters Name=attachment.instance-id,Values=i-0123456789abcdef0

2️⃣ Detach the volume:

aws ec2 detach-volume --volume-id vol-0a1b2c3d4e5f6g7h8

βœ… The volume is now detached!


πŸ“Œ Step 3: Attach the EBS Volume to Another Instance

πŸ”Ή Method 1: Using AWS Console

1️⃣ Go to EC2 Dashboard β†’ Volumes
2️⃣ Select the detached EBS volume
3️⃣ Click Actions β†’ Attach Volume
4️⃣ Select the new EC2 instance
5️⃣ Choose a device name (e.g., /dev/xvdf)
6️⃣ Click Attach

βœ… The volume is now attached to the new instance!


πŸ”Ή Method 2: Using AWS CLI

1️⃣ Attach the volume to another instance:

aws ec2 attach-volume --volume-id vol-0a1b2c3d4e5f6g7h8 --instance-id i-0987654321abcdef0 --device /dev/xvdf

πŸ“Œ Step 4: Mount the EBS Volume (Linux Only)

After attaching, mount the volume on the new instance:

1️⃣ Check if the volume is recognized:

lsblk

2️⃣ Create a mount directory and mount the volume:

sudo mkdir /mnt/new_volume
sudo mount /dev/xvdf /mnt/new_volume

3️⃣ Verify:

df -h

βœ… Your EBS volume is now successfully migrated! πŸš€


3. Example Scenario: Migrating an EBS Volume from One Instance to Another

πŸ“Œ Scenario:
A company has an EC2 instance (Instance A) running an application. They need to migrate the storage to a new instance (Instance B) while preserving the data.

Steps Taken:

1️⃣ Unmounted the volume on Instance A
2️⃣ Detached the volume from Instance A
3️⃣ Attached the volume to Instance B
4️⃣ Mounted the volume on Instance B

βœ… The application now runs on the new instance with the same data!


4. Common Issues and Fixes

❌ “Volume is in-use” error while detaching
πŸ”Ή Solution: Stop the instance before detaching the root volume.

❌ “Device not found” after attaching
πŸ”Ή Solution: Reboot the instance or check lsblk.

❌ “Permission denied” when accessing volume
πŸ”Ή Solution: Adjust file system permissions using chmod or chown.


5. Summary

ActionAWS ConsoleAWS CLI Command
Detach VolumeActions β†’ Detach Volumeaws ec2 detach-volume --volume-id vol-xxxx
Attach VolumeActions β†’ Attach Volumeaws ec2 attach-volume --volume-id vol-xxxx --instance-id i-xxxx --device /dev/xvdf
Mount Volume (Linux)Use lsblk & mountsudo mount /dev/xvdf /mnt/volume

πŸ’‘ Key Takeaways:

βœ… You can detach an EBS volume and attach it to another instance.
βœ… Unmount the volume first to prevent data corruption.
βœ… Use AWS CLI or console to manage the process.
βœ… Mount the volume after attachment in Linux.

πŸš€ Need help with AWS infrastructure? Contact us for expert support!

Would you like an automation script to handle EBS migration? πŸ€–

About Anant 413 Articles
Senior technical writer