Sub topic 13
Backing up with EBS snapshots
This chapter assumes you know what an EBS volume is, can use the AWS CLI, and understand that a volume lives in one Availability Zone (AZ). It covers how snapshots work, how to create and restore them, and the errors people commonly hit.
What a snapshot is
An EBS snapshot is a point-in-time copy of a volume. AWS stores it durably in Amazon S3, but you manage it through EC2, not through your own buckets. A snapshot belongs to a Region, not to an AZ. That makes it the usual way to move data between AZs or Regions.
Snapshots are incremental. The first snapshot of a volume copies all written blocks. Later snapshots store only blocks that changed since the previous one. You can still delete older snapshots safely. AWS keeps any blocks that newer snapshots still need.
Creating a snapshot
aws ec2 create-snapshot \
--volume-id vol-0123456789abcdef0 \
--description "tutorial-web data volume backup"The command returns right away with a state of pending. The volume stays attached and usable while the snapshot is written. The snapshot is only usable for restore once its state is completed. Check it with this command:
aws ec2 describe-snapshots --snapshot-ids snap-0123456789abcdef0 \
--query 'Snapshots[].[SnapshotId,State,Progress]' --output tableRestoring and copying
You never restore "into" a snapshot. You create a new volume from it, then attach it to an instance.
aws ec2 create-volume \
--snapshot-id snap-0123456789abcdef0 \
--availability-zone us-east-1b \
--volume-type gp3Pick any AZ in the snapshot's Region. To use a snapshot in another Region, copy it first with aws ec2 copy-snapshot, giving --source-region, --source-snapshot-id, and the destination --region.
flowchart LRGotchas
- Crash-consistent only. A snapshot captures data already written to the volume. It does not capture data still in application or OS memory. For a database, flush and pause writes first, or use a tool that coordinates this, before you snapshot.
- Slow first reads. A volume restored from a snapshot loads blocks from S3 on first access. Early reads can be slower. Fast snapshot restore avoids this, at extra cost.
- Encryption carries over. Snapshots of encrypted volumes are encrypted, and volumes made from them are encrypted too. Copying a snapshot to another Region needs access to the KMS key you choose.
- Instance store is not covered. Snapshots apply only to EBS. Data on instance store volumes cannot be snapshotted.
- Snapshots cost money. You pay per GB-month of stored snapshot data. Deleting an instance or volume does not delete its snapshots.
Exercise
You deleted an EBS volume in us-east-1a, but you have a completed snapshot of it. How do you get the data back on an instance in us-east-1c, and what does the old snapshot cost while you wait?
Show answer
Run create-volume with the snapshot ID and --availability-zone us-east-1c, then attach the new volume to the instance and mount it. Snapshots work across AZs in a Region. The snapshot keeps costing per GB-month of stored data until you delete it.