AWS

EC2VolumeInUse - EC2 Volume In Use

Getting an **EC2VolumeInUse** error means the EBS volume you're trying to delete or modify is currently attached to an EC2 instance—volumes must be detached before deletion or certain modifications. This client-side error (4xx) happens when AWS validates EBS volume attachment status. Most common when volumes are attached to running instances, but also appears when volumes are attached to stopped instances, volume deletion is attempted while attached, volume modification occurs while attached, or volumes are in use by another operation.

#Common Causes

  • Identity: IAM policy allows EC2 volume operations but volume is attached. Service Control Policy (SCP) enforces volume detachment rules.
  • Network: VPC endpoint EC2 volume restrictions. Volume attached to instance.
  • Limits: Volume attached to running instance. Volume attached to stopped instance. Volume deletion attempted while attached. Volume modification while attached. Volume in use by another operation.

Solutions

  1. 1Step 1: Diagnose - Check volume attachment status: aws ec2 describe-volumes --volume-ids vol-XXXXX --query 'Volumes[0].[VolumeId,State,Attachments[0].InstanceId,Attachments[0].Device]' --output table. Verify if volume is attached.
  2. 2Step 2: Diagnose - Check instance state: Get instance ID from volume attachment: aws ec2 describe-volumes --volume-ids vol-XXXXX --query 'Volumes[0].Attachments[0].InstanceId' --output text. Check instance state: aws ec2 describe-instances --instance-ids i-XXXXX --query 'Reservations[0].Instances[0].State.Name' --output text.
  3. 3Step 3: Diagnose - Wait for operations to complete: Check if volume is in use: aws ec2 describe-volumes --volume-ids vol-XXXXX --query 'Volumes[0].State' --output text. Wait if state is 'in-use' or 'modifying'.
  4. 4Step 4: Fix - Detach volume from instance: Detach volume: aws ec2 detach-volume --volume-id vol-XXXXX --instance-id i-XXXXX. Wait for detachment: aws ec2 wait volume-available --volume-ids vol-XXXXX.
  5. 5Step 5: Fix - Stop instance if needed or force detach: If instance is running, stop it first: aws ec2 stop-instances --instance-ids i-XXXXX. Or force detach: aws ec2 detach-volume --volume-id vol-XXXXX --instance-id i-XXXXX --force. Then delete volume: aws ec2 delete-volume --volume-id vol-XXXXX.

</>Code Examples

Check EBS Volume Attachment Status
1#!/bin/bash
2VOLUME_ID="vol-1234567890abcdef0"
3
4echo "=== Checking Volume Attachment Status ==="
5VOLUME_INFO=$(aws ec2 describe-volumes \
6  --volume-ids ${VOLUME_ID} \
7  --query 'Volumes[0].[VolumeId,State,Attachments[0].InstanceId,Attachments[0].Device]' \
8  --output table 2>&1)
9
10if [ $? -eq 0 ]; then
11  echo "${VOLUME_INFO}"
12  
13  # Get attachment details
14  INSTANCE_ID=$(aws ec2 describe-volumes \
15    --volume-ids ${VOLUME_ID} \
16    --query 'Volumes[0].Attachments[0].InstanceId' \
17    --output text)
18  
19  if [ ! -z "${INSTANCE_ID}" ] && [ "${INSTANCE_ID}" != "None" ]; then
20    echo "\n✗ Volume is attached to instance: ${INSTANCE_ID} (EC2VolumeInUse)"
21    echo "Detach volume before deletion or modification"
22  else
23    echo "\n✓ Volume is not attached"
24  fi
25else
26  echo "✗ Volume not found or error: ${VOLUME_INFO}"
27fi
Detach EBS Volume from Instance
1#!/bin/bash
2VOLUME_ID="vol-1234567890abcdef0"
3
4echo "=== Detaching EBS Volume ==="
5
6# Get instance ID
7INSTANCE_ID=$(aws ec2 describe-volumes \
8  --volume-ids ${VOLUME_ID} \
9  --query 'Volumes[0].Attachments[0].InstanceId' \
10  --output text)
11
12if [ -z "${INSTANCE_ID}" ] || [ "${INSTANCE_ID}" = "None" ]; then
13  echo "✗ Volume is not attached"
14  exit 0
15fi
16
17echo "Volume ID: ${VOLUME_ID}"
18echo "Instance ID: ${INSTANCE_ID}"
19
20# Check instance state
21INSTANCE_STATE=$(aws ec2 describe-instances \
22  --instance-ids ${INSTANCE_ID} \
23  --query 'Reservations[0].Instances[0].State.Name' \
24  --output text)
25
26echo "Instance state: ${INSTANCE_STATE}"
27
28# Detach volume
29echo "\n=== Detaching Volume ==="
30aws ec2 detach-volume \
31  --volume-id ${VOLUME_ID} \
32  --instance-id ${INSTANCE_ID} \
33  --output json
34
35if [ $? -eq 0 ]; then
36  echo "\n✓ Detachment initiated"
37  
38  # Wait for detachment
39  echo "Waiting for volume to become available..."
40  aws ec2 wait volume-available --volume-ids ${VOLUME_ID}
41  
42  if [ $? -eq 0 ]; then
43    echo "✓ Volume detached successfully"
44    echo "Now safe to delete: aws ec2 delete-volume --volume-id ${VOLUME_ID}"
45  else
46    echo "✗ Timeout waiting for detachment"
47  fi
48else
49  echo "\n✗ Failed to detach volume"
50fi
Force Detach and Delete EBS Volume
1#!/bin/bash
2VOLUME_ID="vol-1234567890abcdef0"
3
4echo "=== Force Detaching EBS Volume ==="
5echo "Warning: Force detach may cause data loss if volume is in use"
6
7# Get instance ID
8INSTANCE_ID=$(aws ec2 describe-volumes \
9  --volume-ids ${VOLUME_ID} \
10  --query 'Volumes[0].Attachments[0].InstanceId' \
11  --output text)
12
13if [ -z "${INSTANCE_ID}" ] || [ "${INSTANCE_ID}" = "None" ]; then
14  echo "Volume is not attached - safe to delete"
15  aws ec2 delete-volume --volume-id ${VOLUME_ID}
16  exit 0
17fi
18
19echo "Volume ID: ${VOLUME_ID}"
20echo "Instance ID: ${INSTANCE_ID}"
21
22# Force detach
23echo "\n=== Force Detaching ==="
24aws ec2 detach-volume \
25  --volume-id ${VOLUME_ID} \
26  --instance-id ${INSTANCE_ID} \
27  --force \
28  --output json
29
30if [ $? -eq 0 ]; then
31  echo "\n✓ Force detachment initiated"
32  
33  # Wait for detachment
34  echo "Waiting for volume to become available..."
35  aws ec2 wait volume-available --volume-ids ${VOLUME_ID}
36  
37  if [ $? -eq 0 ]; then
38    echo "✓ Volume detached"
39    
40    # Delete volume
41    echo "\n=== Deleting Volume ==="
42    aws ec2 delete-volume --volume-id ${VOLUME_ID}
43    
44    if [ $? -eq 0 ]; then
45      echo "✓ Volume deleted successfully"
46    else
47      echo "✗ Failed to delete volume"
48    fi
49  else
50    echo "✗ Timeout waiting for detachment"
51  fi
52else
53  echo "\n✗ Failed to force detach volume"
54fi

Related Errors

Provider Information

This error code is specific to AWS services. For more information, refer to the official AWS documentation.

EC2VolumeInUse - EC2 Volume In Use | AWS Error Reference | Error Code Reference