AWS for DevOps — EBS Volumes & Snapshots

3 minute read

5.4 — EBS Volumes & Snapshots

EBS (Elastic Block Store) provides block-level storage for EC2 instances. Think of it as a virtual hard drive that you can attach, detach, snapshot, and resize.

EBS Volume Types

Type Code IOPS Throughput Use Case
General Purpose SSD gp3 3,000-16,000 125-1,000 MB/s Most workloads — boot volumes, apps
General Purpose SSD gp2 100-16,000 (scales with size) 250 MB/s Legacy — use gp3 instead
Provisioned IOPS SSD io2 Up to 64,000 1,000 MB/s Databases needing consistent IOPS
Throughput Optimised HDD st1 500 500 MB/s Big data, log processing
Cold HDD sc1 250 250 MB/s Infrequent access, lowest cost

Default choice: gp3 — it gives you 3,000 IOPS and 125 MB/s baseline regardless of size, and you can provision more independently.

Creating and Attaching a Volume

# Create a 50 GB gp3 volume
$ aws ec2 create-volume \
    --volume-type gp3 \
    --size 50 \
    --availability-zone ap-south-1a \
    --tag-specifications 'ResourceType=volume,Tags=[{Key=Name,Value=app-data}]'
{
    "VolumeId": "vol-abc123",
    "Size": 50,
    "VolumeType": "gp3",
    "State": "creating"
}

# Attach it to an instance
$ aws ec2 attach-volume \
    --volume-id vol-abc123 \
    --instance-id i-0abc123 \
    --device /dev/xvdf

# On the instance — format and mount
$ sudo mkfs -t xfs /dev/xvdf
$ sudo mkdir /data
$ sudo mount /dev/xvdf /data

# Make it persistent across reboots
$ echo '/dev/xvdf /data xfs defaults,nofail 0 2' | sudo tee -a /etc/fstab

Resizing a Volume

You can increase the size of a volume without downtime:

# Increase from 50 GB to 100 GB
$ aws ec2 modify-volume \
    --volume-id vol-abc123 \
    --size 100

# Wait for the modification to complete
$ aws ec2 describe-volumes-modifications \
    --volume-ids vol-abc123 \
    --query 'VolumesModifications[0].ModificationState'
"optimizing"   # then "completed"

# On the instance — extend the file system
$ sudo xfs_growfs /data    # for XFS
# or
$ sudo resize2fs /dev/xvdf # for ext4

Snapshots

A snapshot is a point-in-time backup of an EBS volume, stored in S3 (managed by AWS):

# Create a snapshot
$ aws ec2 create-snapshot \
    --volume-id vol-abc123 \
    --description "app-data backup $(date +%Y-%m-%d)" \
    --tag-specifications 'ResourceType=snapshot,Tags=[{Key=Name,Value=app-data-backup}]'
{
    "SnapshotId": "snap-abc123",
    "VolumeId": "vol-abc123",
    "State": "pending"
}

# List snapshots
$ aws ec2 describe-snapshots --owner-ids self \
    --query 'Snapshots[*].[SnapshotId,VolumeId,StartTime,State]' \
    --output table

# Create a volume from a snapshot (e.g., in a different AZ)
$ aws ec2 create-volume \
    --snapshot-id snap-abc123 \
    --availability-zone ap-south-1b \
    --volume-type gp3

Snapshots are incremental — only changed blocks since the last snapshot are stored:

  Snapshot 1: Full copy (50 GB)
  Snapshot 2: Only changed blocks (2 GB) ← fast and cheap
  Snapshot 3: Only changed blocks (1 GB)

Automating Snapshots with Data Lifecycle Manager

$ aws dlm create-lifecycle-policy \
    --description "Daily EBS snapshots, retain 7" \
    --state ENABLED \
    --execution-role-arn arn:aws:iam::123456789012:role/AWSDataLifecycleManagerDefaultRole \
    --policy-details '{
        "PolicyType": "EBS_SNAPSHOT_MANAGEMENT",
        "ResourceTypes": ["VOLUME"],
        "TargetTags": [{"Key": "Backup", "Value": "daily"}],
        "Schedules": [{
            "Name": "DailySnapshot",
            "CreateRule": {"Interval": 24, "IntervalUnit": "HOURS", "Times": ["03:00"]},
            "RetainRule": {"Count": 7},
            "CopyTags": true
        }]
    }'

Tag your volumes with Backup=daily and DLM handles the rest.

Common Mistakes

Mistake Why It Hurts Fix
Using gp2 for new volumes gp3 is cheaper and more flexible Always use gp3
Not tagging volumes Can’t tell what a volume is for Tag: Name, Environment, Backup
Deleting snapshots without checking Might lose your only backup Use DLM for automated management
Volume and instance in different AZs Can’t attach Create volume in the same AZ as the instance

Quick Reference

Task Command
Create volume aws ec2 create-volume --volume-type gp3 --size SIZE --availability-zone AZ
Attach volume aws ec2 attach-volume --volume-id VOL --instance-id INST --device DEV
Resize volume aws ec2 modify-volume --volume-id VOL --size NEW_SIZE
Create snapshot aws ec2 create-snapshot --volume-id VOL --description DESC
Create from snapshot aws ec2 create-volume --snapshot-id SNAP --availability-zone AZ
Delete snapshot aws ec2 delete-snapshot --snapshot-id SNAP

Next → EFS (Elastic File System)

Updated: