0
0
AwsHow-ToBeginner · 3 min read

How to Attach EBS Volume to EC2 Instance in AWS

To attach an EBS volume to an EC2 instance, use the AWS CLI command aws ec2 attach-volume with the volume ID, instance ID, and device name. This connects the storage volume to your instance so you can use it like a hard drive.
📐

Syntax

The AWS CLI command to attach an EBS volume to an EC2 instance is:

  • --volume-id: The ID of the EBS volume you want to attach.
  • --instance-id: The ID of the EC2 instance to attach the volume to.
  • --device: The device name exposed to the instance (e.g., /dev/sdf).
bash
aws ec2 attach-volume --volume-id <volume-id> --instance-id <instance-id> --device <device-name>
💻

Example

This example shows how to attach an EBS volume with ID vol-0abcd1234efgh5678 to an EC2 instance with ID i-0123456789abcdef0 as device /dev/sdf.

bash
aws ec2 attach-volume --volume-id vol-0abcd1234efgh5678 --instance-id i-0123456789abcdef0 --device /dev/sdf
Output
{ "AttachTime": "2024-06-01T12:00:00.000Z", "Device": "/dev/sdf", "InstanceId": "i-0123456789abcdef0", "State": "attaching", "VolumeId": "vol-0abcd1234efgh5678" }
⚠️

Common Pitfalls

Common mistakes when attaching EBS volumes include:

  • Using an incorrect --device name not supported by the instance OS.
  • Trying to attach a volume that is already attached to another instance.
  • Not having the correct permissions to perform the attach operation.
  • Forgetting to format and mount the volume inside the instance after attaching.
bash
## Wrong device name example (may cause failure)
aws ec2 attach-volume --volume-id vol-0abcd1234efgh5678 --instance-id i-0123456789abcdef0 --device /dev/xvzz

## Correct device name example
aws ec2 attach-volume --volume-id vol-0abcd1234efgh5678 --instance-id i-0123456789abcdef0 --device /dev/sdf
📊

Quick Reference

Remember these tips when attaching EBS volumes:

  • Use aws ec2 attach-volume with correct IDs and device name.
  • Device names usually start with /dev/sd or /dev/xvd.
  • Check volume state before attaching; it must be available.
  • After attaching, log into the instance to format and mount the volume.

Key Takeaways

Use the AWS CLI command aws ec2 attach-volume with volume ID, instance ID, and device name to attach an EBS volume.
Ensure the volume is in available state and not attached elsewhere before attaching.
Choose a valid device name supported by your instance's operating system.
After attaching, format and mount the volume inside the EC2 instance to use it.
Check your AWS permissions to allow volume attachment operations.