Skip to content
Tutorials Center
My learning

Topics

Lesson 11 of 70 ยท Compute with EC2

EBS Volumes, Snapshots and Instance Store

Learn EC2 storage: EBS volume types (gp3, io2, st1), attach and detach, incremental snapshots, encryption, instance store and EFS, with runnable emulators.

An EC2 instance needs a disk, and AWS offers several kinds with very different behavior. The most important one is Amazon EBS (Elastic Block Store): network-attached disks that keep their data when the instance stops. There is also instance store, fast disks inside the host that disappear with the instance, and EFS, a shared network file system. Choosing the wrong one is how people lose data, so it is worth understanding the differences.

This lesson builds on EC2 Instances. The first stand-in shows how EBS snapshots store only changed blocks, and the second tests what survives each lifecycle event.

AWS cannot run on this site, so the CLI commands are read-only. The runnable examples are Python stand-ins that model the ideas (blocks and references, and lifecycle rules). They do not talk to AWS.

EBS volumes

An EBS volume is a block device that you attach to an instance, format with a file system and mount, like a hard disk over the network. Key facts:

  • A volume lives in one Availability Zone and can attach only to instances in the same zone.
  • It exists independently of the instance. You can detach it and attach it to another instance in the same zone.
  • Most volumes attach to one instance at a time. Provisioned IOPS (io1 and io2) volumes also support Multi-Attach in limited cases for clustered software.
  • It is replicated within its Availability Zone for durability, but one zone is still one failure domain, which is why backups (snapshots) matter.
  • You can grow a volume and change its type while it is in use (Elastic Volumes), but you cannot shrink it.

Volume types

Type Kind Use it for
gp3 General purpose SSD The default choice: boot volumes, small and medium databases, dev and test. Baseline of 3,000 IOPS and 125 MiB/s, and you can raise IOPS and throughput independently of size.
gp2 General purpose SSD (older) Legacy. Performance is tied to size and uses burst credits. Prefer gp3 for new volumes.
io2 / io2 Block Express Provisioned IOPS SSD Latency-sensitive databases that need high, consistent IOPS. You pay for the IOPS you provision.
st1 Throughput optimized HDD Large sequential reads and writes: logs, data warehouses, streaming.
sc1 Cold HDD Infrequently accessed data at the lowest cost.

SSD volumes suit random access (IOPS) and HDD volumes suit big sequential streams (throughput). HDD volumes (st1 and sc1) cannot be boot volumes. Exact limits and prices change over time, so check the current numbers before sizing anything.

Creating and attaching a volume (read-only commands, with placeholder IDs):

Terminal
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-0abc123 \
  --instance-id i-0abc123 --device /dev/sdf

Then, on the instance (also read-only here), the volume is a raw device that must be formatted once and mounted:

Terminal
lsblk
sudo mkfs -t xfs /dev/nvme1n1       # only the first time: it erases the volume
sudo mkdir /data && sudo mount /dev/nvme1n1 /data

On Nitro instances the device shows up as /dev/nvme... whatever name you chose during the attach. Add an entry to /etc/fstab (preferably with the volume's UUID) so that it is mounted again after a reboot.

Resizing and changing a volume

Elastic Volumes let you change size, type, IOPS and throughput on a live volume. It takes two steps, because AWS enlarges the block device and you must then enlarge the partition and file system inside it (read-only commands):

Terminal
# 1. Grow the volume from 100 GiB to 200 GiB and raise its IOPS
aws ec2 modify-volume --volume-id vol-0abc123 --size 200 --iops 6000

# 2. Watch the modification (it goes "modifying" -> "optimizing" -> "completed")
aws ec2 describe-volumes-modifications --volume-ids vol-0abc123

# 3. On the instance: extend the partition, then the file system
lsblk
sudo growpart /dev/nvme1n1 1          # only if the disk has a partition
sudo xfs_growfs -d /data              # XFS
# sudo resize2fs /dev/nvme1n1p1       # ext4 instead
df -h /data

Rules to remember:

  • You can increase the size but never decrease it. To shrink, create a smaller volume, copy the data across (for example with rsync), and swap the volumes.
  • After a modification you must wait about six hours (and until the volume reaches the optimizing state) before modifying the same volume again.
  • Take a snapshot before big changes. It is a cheap way to get your data back.
  • The volume stays online during the change, but busy volumes can show reduced performance until the optimizing phase ends.

The gp3 type has limits that the API enforces: IOPS from 3,000 to 16,000, throughput from 125 to 1,000 MiB/s, at most 500 IOPS per GiB of size, and at most 0.25 MiB/s of throughput per provisioned IOPS. The stand-in checks a request against those documented limits, so you can see why a small volume cannot have huge IOPS.

Python Validate gp3 settings before modify-volume

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Multi-Attach

Normally a volume attaches to one instance. Multi-Attach lets a single io1 or io2 volume attach to up to 16 Linux instances that run on the Nitro system, all in the same Availability Zone. Every attached instance can read and write the same blocks at once.

That is a sharp tool. EBS does not coordinate the writers. If two instances mount an ordinary file system such as XFS or ext4 on the same volume, each one caches its own view of the metadata, and the file system is corrupted within seconds. Multi-Attach is for software that manages concurrent access itself: a cluster-aware file system (such as GFS2 or OCFS2), or applications that use the device directly with a coordination mechanism such as NVMe reservations. For ordinary shared files across instances, use EFS. A few more limits: you cannot use Multi-Attach for a boot volume, the volume cannot span zones, and removing the multi-attach setting requires detaching all but one instance.

Terminal
aws ec2 create-volume --availability-zone eu-west-1a \
  --size 200 --volume-type io2 --iops 10000 --multi-attach-enabled
aws ec2 attach-volume --volume-id vol-0abc123 --instance-id i-0aaa1111 --device /dev/sdf
aws ec2 attach-volume --volume-id vol-0abc123 --instance-id i-0bbb2222 --device /dev/sdf

EBS best practices

  • Choose gp3 unless you have a measured reason not to, and set IOPS and throughput to what the workload needs.
  • Use separate volumes for the OS and for data. Then you can replace, resize or snapshot them independently.
  • Check the Delete on termination flag of each volume, so you neither lose data nor leave orphaned volumes that keep billing.
  • Use EBS-optimized instance types (most current types are), so that disk traffic does not compete with network traffic. A volume can be faster than its instance's EBS bandwidth limit.
  • Schedule snapshots with Data Lifecycle Manager or AWS Backup, and test restoring one.
  • Encrypt volumes by default (an account-level setting) and tag them with an owner.
  • Find and delete unattached volumes regularly. They keep costing money.

Snapshots

A snapshot is a point-in-time backup of a volume. Snapshots are stored by AWS in a durable regional store (managed for you, not visible as objects in your own S3 buckets). Their key property is that they are incremental: the first snapshot stores all used blocks, and later snapshots store only the blocks that changed since the previous one.

flowchart LR
    V["Volume<br/>8 blocks"] --> S1["Snapshot 1<br/>stores 8 blocks"]
    V --> S2["Snapshot 2<br/>stores 2 changed blocks,<br/>points to 6 from Snapshot 1"]
    V --> S3["Snapshot 3<br/>stores 1 changed block,<br/>points to the rest"]

Each snapshot looks like a full backup, but only changed blocks are stored again.

Two consequences follow:

  • Every snapshot restores like a full copy. You can delete an old snapshot without breaking newer ones: AWS keeps any block that a remaining snapshot still needs.
  • Cost depends on changes, not on how many snapshots you keep, so frequent snapshots of a mostly stable volume are cheap.

You can create a volume from a snapshot in any zone of the region, copy a snapshot to another region (for disaster recovery) and share it with other accounts. Snapshots of a root volume are what AMIs are built from. In production, automate this with Amazon Data Lifecycle Manager or AWS Backup (see Systems Manager and Day-Two Operations) rather than creating them by hand.

Terminal
aws ec2 create-snapshot --volume-id vol-0abc123 --description "before upgrade"
aws ec2 create-volume --snapshot-id snap-0abc123 --availability-zone eu-west-1b

A restored volume loads blocks lazily from the snapshot store, so the first read of each block can be slower than normal (called lazy loading). Fast snapshot restore avoids that, for an extra fee.

An incremental snapshot emulator

This stand-in models a volume as a list of blocks and snapshots as lists of references. It counts how many block copies are really stored, deletes a snapshot in the middle of the chain, and restores another. The data layout is a simplification of what AWS does internally, but the behavior it shows (incremental storage and safe deletes) is real.

Python Incremental snapshots and safe deletion

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Deleting snap-2 frees only one block, the older version of block 2 that nothing else uses, while block 5's new version stays because snap-3 still points to it. The restore of snap-3 still matches the live volume, which is the guarantee that matters.

Encryption

EBS encryption uses AWS KMS keys (see Encryption and KMS) and protects data at rest, data moving between the instance and the volume, and snapshots and volumes created from them. It is supported by all current instance types and the performance impact is negligible. Two practical points:

  • Turn on EBS encryption by default in each region you use. New volumes and snapshot copies are then encrypted automatically.
  • A snapshot of an encrypted volume is encrypted. To encrypt an existing unencrypted volume, snapshot it, copy the snapshot with encryption enabled, and create a new volume from the copy.
Terminal
aws ec2 enable-ebs-encryption-by-default --region eu-west-1

Instance store

Some instance types (those with a d in their name, and the storage-optimized i families) come with disks physically inside the host server. This instance store is very fast and costs nothing extra. Its drawback is that it is ephemeral:

  • Data survives an OS reboot.
  • Data is lost when the instance stops, hibernates, terminates, or the underlying disk or host fails.

Use it for scratch space, caches, buffers and data that is replicated elsewhere (for example one node of a distributed database that has other copies). Never use it as the only place for data you cannot recreate.

EFS in brief

Amazon EFS is a managed NFS file system. Unlike EBS, it can be mounted by many instances (and containers and Lambda functions) at once, across several Availability Zones, and it grows and shrinks automatically. It is for Linux workloads that need shared files: content directories, home folders, shared configuration. It has higher latency than EBS and costs more per GB, so it is not a replacement for a fast database disk. Amazon FSx offers managed Windows file shares and other specialized file systems. Object storage with S3 is a different model again.

What survives what?

The second stand-in writes one file to four kinds of storage and applies lifecycle events. It encodes the default rules from this lesson: the root volume is deleted on termination, a volume attached afterwards is kept, and instance store is lost on stop and termination.

Python What survives a reboot, stop or terminate?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The data volume survives everything because it is a separate EBS volume that you attached after launch. That is a common pattern: keep the valuable data on its own volume so the instance can be replaced. The root volume of a launched instance can be changed to survive termination through the DeleteOnTermination setting.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which volume type is the usual default for new general-purpose workloads?

    gp3 is the general-purpose SSD type with a solid baseline and independently adjustable IOPS and throughput. HDD types are for sequential workloads and cannot be boot volumes.
  2. 2. Why can you delete an older snapshot without breaking newer ones?

    Snapshots are incremental but each one references all blocks it needs, so AWS removes only blocks that no remaining snapshot uses.
  3. 3. What happens to instance store data when you stop the instance?

    Instance store is physically tied to the host. It survives a reboot but not a stop, hibernate, terminate or hardware failure.
  4. 4. An EBS volume is in eu-west-1a. Where can you attach it directly?

    Volumes are bound to a single Availability Zone. To use the data elsewhere, snapshot it and create a new volume in the target zone.
  5. 5. Which storage can many instances in several Availability Zones mount at the same time?

    EFS is a shared NFS file system spanning zones. EBS volumes attach to one instance (with limited Multi-Attach exceptions) and instance store is local to a host.

Summary

  • EBS volumes are network disks in one Availability Zone that outlive the instance. gp3 is the default, io2 is for high IOPS databases, and st1 and sc1 are HDD types for sequential and cold data.
  • Snapshots are incremental backups stored regionally. Each restores like a full copy and can be deleted safely in any order.
  • Encrypt volumes with KMS and turn on encryption by default per region.
  • Instance store is fast but ephemeral: it survives a reboot but not a stop, termination or hardware failure.
  • EFS is a shared, multi-AZ file system for Linux workloads that need common files.
  • Keep valuable data on its own volume, and back it up with snapshots.

Next: On-Demand, Reserved, Savings Plans and Spot