Sub topic 2
Creating and Managing Amazon EBS Volumes
Assume you understand EC2 instances, Availability Zones, Linux commands, and basic file systems. Amazon Elastic Block Store (EBS) provides persistent block storage for an EC2 instance. A volume is tied to one Availability Zone, so attach it only to an instance in the same Zone.
Create and attach a volume
Create an encrypted 100 GiB gp3 volume:
aws ec2 create-volume \
--availability-zone us-east-1a \
--volume-type gp3 \
--size 100 \
--iops 3000 \
--throughput 125 \
--encrypted \
--tag-specifications 'ResourceType=volume,Tags=[{Key=Name,Value=data-vol}]'Attach it to an instance:
aws ec2 attach-volume \
--volume-id vol-0123456789abcdef0 \
--instance-id i-0123456789abcdef0 \
--device /dev/sdfOn Nitro-based instances, Linux often exposes the device as an NVMe name such as /dev/nvme1n1, rather than /dev/sdf.
Format and mount the volume
A new empty volume has no file system. Format it, mount it, and persist the mount by UUID:
lsblk
sudo mkfs -t xfs /dev/nvme1n1
sudo mkdir /data
sudo mount /dev/nvme1n1 /data
sudo blkid /dev/nvme1n1
echo 'UUID=<uuid-from-blkid> /data xfs defaults,nofail 0 2' | sudo tee -a /etc/fstab
sudo mount -aBefore detaching, stop applications, unmount the file system, and confirm that no process is using it. Delete the volume only after detaching it and confirming that its data is no longer needed.
Root volumes and Elastic Volumes
An instance's root volume contains its operating system. An AMI can launch new instances with a defined root-volume mapping. The DeleteOnTermination setting controls whether that root volume is deleted when the instance terminates. Preserve it when you need the disk for recovery.
Elastic Volumes lets you change size, type, or performance while a volume remains attached. After increasing an XFS volume, extend the file system:
aws ec2 modify-volume \
--volume-id vol-0123456789abcdef0 \
--size 200 \
--iops 6000
sudo xfs_growfs /dataSnapshots
An EBS snapshot is a point-in-time backup stored by AWS. The first snapshot stores the volume's data. Later snapshots store changed blocks, so retain only the recovery points your policy requires.
aws ec2 create-snapshot \
--volume-id vol-0123456789abcdef0 \
--description 'data-vol nightly'Exercise
Why must you unmount a file system before detaching its EBS volume?
Show answer
Unmounting flushes pending writes and prevents applications from using a device that is no longer attached.