Amazon EBS
Amazon Elastic Block Store (EBS) provides network-attached disks (volumes) for EC2 instances. To the operating system a volume looks like a normal hard drive (/dev/nvme1n1) that you format and mount. Unlike S3 objects, EBS is block storage: programs can change a few bytes in the middle of a file, which databases and operating systems need.
Key facts:
- A volume lives in one Availability Zone and is replicated inside it. It can attach only to instances in the same AZ (most volume types attach to one instance at a time).
- Volumes persist independently of the instance: stop or even terminate the instance and a data volume survives (unless "delete on termination" is set, the default for root volumes).
- Size can be increased and the type changed on the fly with Elastic Volumes.
Volume types
| Type | Kind | Performance | Use for |
|---|---|---|---|
| gp3 | General-purpose SSD | 3,000 IOPS and 125 MB/s baseline, configurable up to 16,000 IOPS | Most workloads, boot volumes |
| gp2 | General-purpose SSD (older) | 3 IOPS per GB, burst to 3,000 | Legacy; gp3 is usually cheaper |
| io2 Block Express | Provisioned-IOPS SSD | Up to 256,000 IOPS, sub-millisecond | Large critical databases |
| st1 | Throughput-optimised HDD | High MB/s for sequential reads | Big data, log processing |
| sc1 | Cold HDD | Lowest cost | Infrequently accessed sequential data |
Instance store is different: physical disks on the host, very fast but ephemeral: data is lost when the instance stops or the host fails. Use it for caches and scratch space only.
Creating, attaching and snapshotting
aws ec2 create-volume --availability-zone eu-west-1a --size 100 --volume-type gp3 \
--iops 4000 --throughput 250 --encrypted
aws ec2 attach-volume --volume-id vol-0a1b2c3d4e5f67890 --instance-id i-0f1e2d3c4b5a69788 --device /dev/sdf
aws ec2 create-snapshot --volume-id vol-0a1b2c3d4e5f67890 --description "db before upgrade"Sample output of the snapshot command:
{
"SnapshotId": "snap-066877671789bd71b",
"State": "pending",
"VolumeSize": 100,
"Encrypted": true,
"Description": "db before upgrade"
}Model: gp2 vs gp3 performance
gp2 performance grows with size (3 IOPS per GB, minimum 100, maximum 16,000). gp3 gives 3,000 IOPS at any size and lets you buy more separately. That changes how you size volumes:
def gp2_iops(size_gb):
return min(max(3 * size_gb, 100), 16_000)
def gp3_iops(size_gb, extra=0):
return min(3000 + extra, 16_000)
print(f"{'size GB':>8} {'gp2 IOPS':>9} {'gp3 IOPS':>9}")
for size in (20, 100, 500, 1000, 2000, 6000):
print(f"{size:>8} {gp2_iops(size):>9,} {gp3_iops(size):>9,}")
need = 9000
print(f"\nTo get {need:,} IOPS on gp2 you need {need // 3:,} GB;"
f" on gp3 any size + {need - 3000:,} extra provisioned IOPS.") size GB gp2 IOPS gp3 IOPS
20 100 3,000
100 300 3,000
500 1,500 3,000
1000 3,000 3,000
2000 6,000 3,000
6000 16,000 3,000
To get 9,000 IOPS on gp2 you need 3,000 GB; on gp3 any size + 6,000 extra provisioned IOPS.
Model: incremental snapshots
Snapshots are stored in S3 (managed by AWS) and are incremental: the first one copies all used blocks, later ones only the blocks that changed. Deleting an old snapshot never breaks newer ones; blocks still needed are kept.
volume = {0: "boot", 1: "db-a", 2: "db-b", 3: "logs-1"} # block -> content
snapshots = [] # each snapshot: dict of the blocks it stores itself
seen = {} # block -> content in the latest snapshot
def snapshot(name):
changed = {b: c for b, c in volume.items() if seen.get(b) != c}
snapshots.append((name, changed))
seen.update(changed)
print(f"{name}: stored {len(changed)} new block(s) {sorted(changed)}")
snapshot("snap-1")
volume[2] = "db-b2"; volume[4] = "logs-2"
snapshot("snap-2")
snapshot("snap-3") # nothing changed
volume[1] = "db-a2"
snapshot("snap-4")
total = sum(len(blocks) for _, blocks in snapshots)
print(f"blocks stored in total: {total} (a full copy each time would be {4 + 5 + 5 + 5})")snap-1: stored 4 new block(s) [0, 1, 2, 3]
snap-2: stored 2 new block(s) [2, 4]
snap-3: stored 0 new block(s) []
snap-4: stored 1 new block(s) [1]
blocks stored in total: 7 (a full copy each time would be 19)
Because a volume lives in one AZ, snapshots are also how you move data: restore a snapshot as a new volume in another AZ, or copy it to another Region for disaster recovery. Amazon Data Lifecycle Manager or AWS Backup can take and expire snapshots on a schedule.
Common mistakes
- Trying to attach a volume to an instance in a different AZ (restore a snapshot there instead).
- Keeping important data on instance store volumes.
- Forgetting unattached volumes: they are billed every month until deleted.
- Over-provisioning gp2 volumes just to get IOPS; gp3 decouples size and performance.
Test yourself
Can an EBS volume in eu-west-1a be attached to an instance in eu-west-1b?
- Yes, always
- No, create a snapshot and restore it in eu-west-1b
- Only gp3 volumes
- Only when stopped
What happens to data on an instance store volume when the instance is stopped?
- It is kept
- It is lost
- It is moved to S3
- It is snapshotted
What does 'incremental' mean for EBS snapshots?
- Each snapshot is a full copy
- Only blocks changed since the previous snapshot are stored
- Snapshots grow every day
- Snapshots can only be taken in increments of 1 GB