Sub topic 11
Restoring from snapshots and initializing volumes
This chapter assumes you can take a snapshot (covered in the previous chapter), use the AWS CLI, and mount a file system on Linux. It explains how a volume is created from a snapshot, why the first read of each block is slow, and how to remove that slowdown by initializing the volume or by using Fast Snapshot Restore.
What restoring means
A snapshot is not a volume. It is a set of blocks stored by EBS in Amazon S3, which you can't mount. To use the data, you create a new volume from the snapshot. The new volume is independent of the snapshot. Later changes to the volume don't alter the snapshot, and deleting the snapshot doesn't affect the volume.
The rules for restoring are:
- The volume must be created in the same Region as the snapshot. You can choose any Availability Zone in that Region. This is how you move data between Availability Zones.
- The volume size must be equal to or larger than the snapshot size. If you omit
--size, the snapshot size is used. - You can change the volume type, IOPS and throughput at restore time. For example, a snapshot of a gp2 volume can become a gp3 volume.
- A volume restored from an encrypted snapshot is always encrypted. A volume restored from an unencrypted snapshot can be encrypted at restore time.
- To use a snapshot in another Region, first copy it there with
aws ec2 copy-snapshot --source-region. Run the command in the destination Region.
SNAP=snap-0123456789abcdef0
# Wait until the snapshot is fully written before restoring from it.
aws ec2 wait snapshot-completed --snapshot-ids "$SNAP"
# Restore into another AZ, switching the type and size on the way.
aws ec2 create-volume \
--snapshot-id "$SNAP" \
--availability-zone us-east-1b \
--volume-type gp3 \
--size 200 \
--iops 4000 \
--throughput 250 \
--tag-specifications 'ResourceType=volume,Tags=[{Key=Name,Value=data-vol-restored}]'To restore an unencrypted snapshot into an encrypted volume, add --encrypted and optionally --kms-key-id alias/my-key. Without a key ID, EBS uses the default EBS KMS key for the account.
Lazy loading and first-access latency
A restored volume becomes available within seconds, and you can attach and use it right away. The data is not copied at that point. EBS loads each block from S3 the first time something reads it. This is called lazy loading.
The consequence is uneven performance. The first read of any block has higher latency than later reads, because EBS must fetch it from S3 first. A database that scans a freshly restored volume can look slow even though the volume type and IOPS are correct. Writes to blocks that haven't been loaded also trigger a fetch of the original block.
flowchart TDThe diagram block below shows the same flow with the required format.
New empty volumes are not affected. Lazy loading applies only to volumes created from snapshots, including volumes created from an AMI's snapshots, such as the root volume of an instance you launch.
Mounting a restored volume
A restored volume already contains the file system from the source volume. Do not run mkfs on it. That would erase the data you restored. Check for a file system and mount it:
lsblk -f
sudo mkdir -p /restore
sudo mount /dev/nvme2n1 /restoreIf the restored volume is larger than the snapshot, the file system keeps its old size until you extend it, as you did after Elastic Volumes changes. For XFS, run sudo xfs_growfs /restore after mounting.
Duplicate XFS UUIDs
If you attach a volume restored from a snapshot to the same instance as the original volume, the mount can fail. XFS refuses to mount two file systems with the same UUID, and the error mentions a duplicate UUID. You have two options:
- Mount with a temporary override:
sudo mount -o nouuid /dev/nvme2n1 /restore. - Give the restored file system a new UUID before mounting it:
sudo xfs_admin -U generate /dev/nvme2n1.
Use the second option if you want to put the restored volume in /etc/fstab, because both volumes would otherwise share a UUID there as well.
Crash consistency
A snapshot captures the blocks that were on the volume when it started. It doesn't include data held in memory or in the operating system's cache. A restored file system behaves like one after a power loss. Journaling file systems such as XFS recover on mount, but applications such as databases may need their own recovery step. For consistent snapshots, stop writes, or flush and freeze the file system, before you take the snapshot.
Initializing a volume
Initialization means forcing every block to load from S3 before production traffic arrives. The traditional method is to read the whole device once. The fio tool does this efficiently with large, parallel reads.
sudo yum install -y fio # use apt-get on Debian or Ubuntu
sudo fio \
--filename=/dev/nvme2n1 \
--rw=read \
--bs=1M \
--iodepth=32 \
--ioengine=libaio \
--direct=1 \
--name=volume-initializeThis reads every block and discards the data. It doesn't write to the volume, so it is safe on a volume holding data. While it runs, the volume is busy, so run it before you put the volume into service. The time depends on the volume size and the throughput the volume and instance allow. A 1 TiB volume read at 250 MiB/s takes roughly 70 minutes, since 1,048,576 MiB divided by 250 MiB/s is about 4,200 seconds.
Initialization is billed only through the normal cost of reads on the volume. For gp3 and io2 volumes, reads are included in the volume price. You don't pay per request.
Fast Snapshot Restore
Fast Snapshot Restore (FSR) removes the lazy-loading penalty without a manual read. You enable it for a specific snapshot in specific Availability Zones. Volumes created from that snapshot in those zones are fully initialized at creation.
aws ec2 enable-fast-snapshot-restores \
--availability-zones us-east-1a us-east-1b \
--source-snapshot-ids snap-0123456789abcdef0
aws ec2 describe-fast-snapshot-restores \
--filters Name=snapshot-id,Values=snap-0123456789abcdef0 \
--query 'FastSnapshotRestores[].[AvailabilityZone,State]' \
--output tableFSR moves through these states for each snapshot and zone:
| State | Meaning |
|---|---|
| `enabling` | AWS is preparing the snapshot. Volumes created now are not fully initialized. |
| `optimizing` | Preparation is under way. Volumes created now get partial benefit. |
| `enabled` | Volumes created from the snapshot are fully initialized. |
| `disabling` / `disabled` | FSR is being turned off, or is off. |
Several details matter when you plan to use it:
- FSR is enabled per snapshot and per Availability Zone. If you restore into a zone where it isn't enabled, you get a normal lazy-loading volume.
- Wait for the
enabledstate before relying on it. Preparation takes time that grows with snapshot size. - It has an hourly charge for each snapshot in each zone, for as long as it is enabled. Disable it when you no longer need it.
- It can speed up the volumes created per hour, but only up to a creation-credit limit that depends on the snapshot size. Creating many volumes quickly can exhaust the credits. Volumes created after that behave like normal restored volumes.
- There is a default quota on the number of snapshots with FSR enabled per Region. Check Service Quotas for the current value.
Choosing an approach
| Situation | Approach |
|---|---|
| Test or one-off restore, speed not critical | Restore and use it as is. |
| Restore once, then run a latency-sensitive workload | Restore, then initialize with `fio` before use. |
| Launch many volumes or instances from the same snapshot, with predictable speed | Enable FSR on the snapshot in the zones you use. |
| Large snapshot you rarely restore | Skip FSR. Initialize on demand. |
Common mistakes
- Running
mkfson a restored volume and losing the data. - Restoring into a different Region without copying the snapshot first. The command fails because the snapshot isn't found there.
- Forgetting to extend the file system after restoring to a larger size.
- Benchmarking a freshly restored volume and concluding that the volume type is too slow. Initialize first, then measure.
- Leaving FSR enabled on snapshots you no longer restore, so the hourly charge continues.
Exercise
You restore a 500 GiB snapshot to a new gp3 volume in us-east-1a and attach it to an instance. A database scan of the volume is slow the first time and fast the second time. What causes this, and give two ways to avoid it for future restores.
Show answer
The volume is lazily loaded. Each block is fetched from S3 on first access, so the first scan pays extra latency per block, and the second scan reads blocks already on the volume. To avoid it, either initialize the volume after restoring by reading the whole device with fio (--rw=read --bs=1M --direct=1), or enable Fast Snapshot Restore on the snapshot in us-east-1a and wait for the enabled state before creating the volume.
Exercise
You attach a volume restored from a snapshot to the same instance that holds the original volume. Mounting fails with a duplicate UUID error on an XFS file system. How do you mount it, and what should you not run?
Show answer
Mount with sudo mount -o nouuid <device> <mountpoint>, or change the UUID first with sudo xfs_admin -U generate <device>. Do not run mkfs, because the restored volume already has a file system and formatting would erase its data.