Workshop 3: Add Volume

Workshop Goals

In this dedicated volume management workshop, you’ll learn how to:
• Create and attach Amazon EBS volumes via AWS CLI
• Format and mount volumes inside Linux instances
• Configure automatic mounting using /etc/fstab
• Create, manage, and restore snapshots
• Resize volumes and grow filesystems dynamically

Prerequisites

• A running EC2 instance (Amazon Linux 2)
• AWS CLI v2 installed and configured
• IAM permissions: ec2:CreateVolume, AttachVolume, CreateSnapshot, ModifyVolume
• SSH access with a key pair

1. Create and Attach an EBS Volume

# Create a 20 GiB gp3 volume in the same AZ as your instance
ewsn ec2 create-volume \
  --size 20 \
  --volume-type gp3 \
  --availability-zone us-east-1a

# Attach the volume to your instance on device /dev/xvdh
ewsn ec2 attach-volume \
  --volume-id vol-0abc1234def56789a \
  --instance-id i-0123456789abcdef0 \
  --device /dev/xvdh

Parameters Explained

--size Volume size in GiB.
--volume-type gp3 for SSD performance.
--availability-zone Must match your EC2 instance’s AZ.
--volume-id The ID returned after creation.
--instance-id Your EC2 instance ID.
--device Linux device name (e.g., /dev/xvdh).

2. Format and Mount the Volume

# SSH into the instance
ssh -i ~/.ssh/MyKeyPair.pem ec2-user@<PublicIpAddress>

# Verify the attached device
lsblk

# Format with ext4 filesystem
sudo mkfs.ext4 /dev/xvdh

# Create a mount point and mount the volume
sudo mkdir /mnt/data
sudo mount /dev/xvdh /mnt/data

Formatting & Mounting Explained

lsblk Lists block devices and their mount points.
mkfs.ext4 Creates ext4 filesystem on the new volume.
mkdir /mnt/data Directory to mount the volume.
mount Attaches the filesystem to the directory.

3. Configure Automatic Mounting

# Backup current fstab
sudo cp /etc/fstab /etc/fstab.bak

# Add entry to /etc/fstab for persistent mount
echo "/dev/xvdh  /mnt/data  ext4  defaults,nofail  0  2" | sudo tee -a /etc/fstab

# Test fstab entries without reboot
sudo mount -a

fstab Entry Explained

/dev/xvdh Device name.
/mnt/data Mount point.
ext4 Filesystem type.
defaults,nofail Mount options; nofail prevents boot issues if missing.
0 2 Dump and fsck order: skip dump, fsck in pass 2.

4. Snapshot and Restore

# Create a snapshot of the volume
aws ec2 create-snapshot \
  --volume-id vol-0abc1234def56789a \
  --description "Workshop3 Snapshot"

# Restore a new volume from the snapshot
aws ec2 create-volume \
  --snapshot-id snap-0123abcd4567efgh8 \
  --availability-zone us-east-1a

Snapshot Parameters Explained

--snapshot-id ID of the source snapshot.
--description Label for the snapshot.
--availability-zone Zone for the restored volume.

5. Resizing Volumes and Filesystems

# Increase volume size to 30 GiB
aws ec2 modify-volume --volume-id vol-0abc1234def56789a --size 30

# On the instance, verify and resize filesystem
lsblk
sudo resize2fs /dev/xvdh

Resizing Explained

modify-volume Adjusts the EBS volume size.
resize2fs Expands the ext4 filesystem to use new size.

Next Steps

Explore advanced EBS features: Multi-Attach for high availability, encryption with AWS KMS keys, and integration with Data Lifecycle Manager for automated snapshot schedules and retention policies.

Previous: Workshop 2: Web Application on EC2 | Next: Workshop 4: AMI

<
>