How to Launch a New EC2 Instance Using an Existing EBS Volume as Root Device


3 views

When you launch an EC2 instance with an EBS-backed AMI, AWS automatically creates an EBS volume for the root device. By default, this volume gets deleted when you terminate the instance. However, you can configure it to persist by setting DeleteOnTermination to false either during launch or through modifications.

aws ec2 modify-instance-attribute \
    --instance-id i-1234567890abcdef0 \
    --block-device-mappings "[{\"DeviceName\": \"/dev/sda1\",\"Ebs\":{\"DeleteOnTermination\":false}}]"

Yes, you can directly attach the existing EBS volume as the root device for a new instance without going through the AMI creation process. Here's how:

  1. Stop the original instance (if it's still running but unhealthy)
  2. Detach the EBS volume from the failed instance
  3. Launch a new instance with any AMI (this will create a temporary root volume)
  4. Stop the new instance immediately after launch
  5. Detach the temporary root volume
  6. Attach your existing EBS volume to the new instance as /dev/sda1
  7. Start the new instance

Here's how to implement this process using AWS CLI:

# Step 1: Detach the volume from old instance
aws ec2 detach-volume --volume-id vol-1234567890abcdef0

# Step 2: Launch new instance (note the AMI doesn't matter)
INSTANCE_ID=$(aws ec2 run-instances \
    --image-id ami-0abcdef1234567890 \
    --instance-type t2.micro \
    --query 'Instances[0].InstanceId' \
    --output text)

# Step 3: Stop the new instance
aws ec2 stop-instances --instance-ids $INSTANCE_ID

# Step 4: Wait for instance to stop
aws ec2 wait instance-stopped --instance-ids $INSTANCE_ID

# Step 5: Detach the temporary volume
TEMP_VOL=$(aws ec2 describe-instances \
    --instance-ids $INSTANCE_ID \
    --query 'Reservations[0].Instances[0].BlockDeviceMappings[0].Ebs.VolumeId' \
    --output text)
aws ec2 detach-volume --volume-id $TEMP_VOL

# Step 6: Attach your existing volume
aws ec2 attach-volume \
    --volume-id vol-1234567890abcdef0 \
    --instance-id $INSTANCE_ID \
    --device /dev/sda1

# Step 7: Start the instance
aws ec2 start-instances --instance-ids $INSTANCE_ID
  • The new instance must use the same architecture (x86 vs ARM) as the original instance
  • Kernel versions and virtualization types must be compatible
  • Network configuration might need adjustment if the instance has static IP settings
  • Always create snapshots before performing such operations

While the direct method works, creating an AMI offers better portability:

# Create snapshot
SNAPSHOT_ID=$(aws ec2 create-snapshot \
    --volume-id vol-1234567890abcdef0 \
    --description "Recovery snapshot" \
    --query 'SnapshotId' \
    --output text)

# Wait for snapshot completion
aws ec2 wait snapshot-completed --snapshot-ids $SNAPSHOT_ID

# Register AMI
aws ec2 register-image \
    --name "recovery-ami" \
    --architecture x86_64 \
    --root-device-name "/dev/sda1" \
    --block-device-mappings "[{\"DeviceName\": \"/dev/sda1\",\"Ebs\":{\"SnapshotId\":\"$SNAPSHOT_ID\",\"VolumeType\":\"gp2\"}}]"

This method is more robust as it creates a portable AMI that can be launched on any compatible instance type.


When working with AWS EC2 instances, you'll frequently encounter situations where you need to recover from hardware failures or migrate instances. A common case involves preserving the root EBS volume (with DeleteOnTermination=false) after instance termination and reusing it as a boot device.

The most straightforward approach is attaching the existing EBS volume as the root device during instance launch:

aws ec2 run-instances \
    --image-id ami-0abcdef1234567890 \
    --instance-type t3.micro \
    --block-device-mappings "[{\"DeviceName\": \"/dev/xvda\",\"Ebs\":{\"VolumeId\":\"vol-1234567890abcdef0\"}}]" \
    --key-name MyKeyPair

Important considerations:

  • The volume must be in the same Availability Zone as your target instance
  • Ensure the volume's architecture matches the instance type (x86 vs. ARM)
  • The volume must not be attached to another running instance

For recovering from a hypervisor failure:

# 1. Stop the instance (if not already terminated)
aws ec2 stop-instances --instance-ids i-1234567890abcdef0

# 2. Detach the volume
aws ec2 detach-volume --volume-id vol-1234567890abcdef0

# 3. Launch new instance with the existing volume
aws ec2 run-instances \
    --instance-type t3.micro \
    --block-device-mappings "[{\"DeviceName\": \"/dev/xvda\",\"Ebs\":{\"VolumeId\":\"vol-1234567890abcdef0\"}}]" \
    --security-group-ids sg-903004f8 \
    --subnet-id subnet-6e7f829e

While creating an AMI from a snapshot adds steps, it provides portability across regions:

# Create snapshot
aws ec2 create-snapshot --volume-id vol-1234567890abcdef0

# Register AMI
aws ec2 register-image \
    --name "recovered-ami" \
    --description "AMI recovered from existing EBS volume" \
    --root-device-name "/dev/xvda" \
    --block-device-mappings "[{\"DeviceName\": \"/dev/xvda\",\"Ebs\":{\"SnapshotId\":\"snap-1234567890abcdef0\"}}]"

When booting from existing volumes:

  • GP3 volumes maintain consistent performance after reattachment
  • IO1/IO2 volumes retain their provisioned IOPS settings
  • For critical workloads, consider pre-warming EBS volumes

For infrastructure-as-code deployments:

Resources:
  RecoveredInstance:
    Type: AWS::EC2::Instance
    Properties:
      InstanceType: t3.micro
      BlockDeviceMappings:
        - DeviceName: /dev/xvda
          Ebs:
            VolumeId: vol-1234567890abcdef0
      SecurityGroupIds:
        - sg-903004f8
      SubnetId: subnet-6e7f829e

Common issues and solutions:

  • VolumeInUse: Ensure the volume is detached from previous instances
  • IncorrectDevice: Verify the device name matches the original AMI (check with aws ec2 describe-images)
  • KernelPanic: The volume must contain a bootable OS image compatible with the instance type