Sub topic 13
Automating snapshots and retention
This chapter assumes you can already create snapshots by hand, tag resources, and create an IAM role. Manual snapshots get forgotten, and old ones pile up and cost money. Two services remove that chore: Amazon Data Lifecycle Manager (DLM) and AWS Backup.
How retention works
Snapshots are incremental. Deleting one snapshot removes only the blocks that no other snapshot in the chain still references. A retention policy therefore saves less than the sum of the snapshot sizes suggests, but it never breaks the remaining snapshots.
Data Lifecycle Manager
DLM is built into EC2. It creates and deletes EBS snapshots on a schedule. It has no extra charge, and you pay only for the snapshots. A policy has four parts:
- Target: volumes or instances that carry a given tag.
- Schedule: how often to run, and at what time (UTC).
- Retention: keep a fixed count of snapshots, or keep them for a number of days.
- Role: an IAM role that DLM assumes to create and delete snapshots.
Create a policy
Save the policy details as policy.json. This policy snapshots every volume tagged Backup=daily once a day and keeps the latest seven snapshots.
{
"PolicyType": "EBS_SNAPSHOT_MANAGEMENT",
"ResourceTypes": ["VOLUME"],
"TargetTags": [{ "Key": "Backup", "Value": "daily" }],
"Schedules": [
{
"Name": "daily-snapshots",
"CreateRule": { "Interval": 24, "IntervalUnit": "HOURS", "Times": ["03:00"] },
"RetainRule": { "Count": 7 },
"CopyTags": true
}
]
}Then tag a volume and create the policy. Replace the account ID and volume ID with your own.
aws ec2 create-tags \
--resources vol-0123456789abcdef0 \
--tags Key=Backup,Value=daily
aws dlm create-lifecycle-policy \
--description "Daily EBS snapshots, keep 7" \
--state ENABLED \
--execution-role-arn arn:aws:iam::123456789012:role/AWSDataLifecycleManagerDefaultRole \
--policy-details file://policy.jsonA policy can hold up to four schedules. For example, you can add a weekly schedule with a longer retention alongside the daily one.
AWS Backup
AWS Backup is a central service that covers EBS, RDS, EFS, DynamoDB and more. Use it when you need one backup plan across several services, copies to another Region or account, or vault locking to stop deletion. You define a backup plan with rules and assign resources by tag or ID. Backup storage is billed, and snapshots it creates are managed by the plan.
| Need | Better fit |
|---|---|
| EBS-only snapshots, simple schedule | DLM |
| Many AWS services under one policy | AWS Backup |
| Cross-account copies and compliance locking | AWS Backup |
| No additional service charge | DLM |
Common errors
- Nothing happens: the tag key or value on the volume does not exactly match
TargetTags. Tags are case-sensitive. - Policy never runs: the state is
DISABLED, or the execution role lacks permission to create snapshots. - Snapshots not pruned: DLM only deletes snapshots that it created. Manual snapshots stay until you remove them.
- Duplicate snapshots and costs: a DLM policy and an AWS Backup plan both target the same volume. Pick one owner per volume.
Exercise
A volume is tagged Backup=Daily, but your DLM policy targets Backup=daily and no snapshots appear. Why, and how do you fix it?
Show answer
Tag values are case-sensitive, so the volume does not match the policy's target tag. Re-tag the volume with Backup=daily (or change the policy's TargetTags), and DLM will include it at the next scheduled run.