Sub topic 13
Automating snapshots and retention
This chapter assumes you can create a snapshot by hand, as covered in the snapshots chapter. It also assumes you know how EBS tags work and can read an IAM role ARN and a JSON file. Here you will replace manual snapshots with policies that create them on a schedule and delete them when they expire.
Why automate snapshots
Manual snapshots fail in predictable ways. Someone forgets to run them, new volumes are never added to the routine, and old snapshots pile up because nobody owns deleting them. A lifecycle policy fixes all three. It selects volumes by tag, creates snapshots on a schedule, and removes them under a retention rule.
AWS offers two services for this:
- Amazon Data Lifecycle Manager (DLM) is built into EC2 and automates only EBS snapshots and EBS-backed AMIs.
- AWS Backup is a central service that protects many resource types, including EBS, RDS, EFS and DynamoDB, with one plan.
| Aspect | Data Lifecycle Manager | AWS Backup |
|---|---|---|
| Scope | EBS snapshots and EBS-backed AMIs | Many AWS services |
| Selecting resources | Tags on volumes or instances | Tags, resource IDs, or resource types |
| Retention | Count-based or age-based | Age-based, set in days |
| Storage | Snapshots in your account | Recovery points in a backup vault |
| Extra controls | Cross-Region and cross-account copy | Vault Lock, cross-account backup, audit reports |
| Extra cost | No charge for DLM itself | No charge for the service beyond the storage it uses |
Choose DLM when you only need EBS snapshots and want the simplest setup. Choose AWS Backup when you need one retention and compliance model across services, or immutable vaults.
How a DLM policy works
A DLM policy has four parts.
- Target. Tags that select resources, and a resource type of
VOLUMEorINSTANCE. WithINSTANCE, DLM takes crash-consistent snapshots of all volumes attached to the tagged instance together. - Schedule. The interval, such as every 12 hours or every 24 hours, and the start time in UTC. A policy can hold up to four schedules, for example one daily and one weekly.
- Retention. Either keep the newest N snapshots (count-based) or keep snapshots for a number of days (age-based).
- Execution role. An IAM role that DLM assumes to create and delete snapshots on your behalf.
Create a policy step by step
1. Create the execution role
DLM needs a role. If you have not used DLM before, create the default one:
aws dlm create-default-role --resource-type snapshotThis creates AWSDataLifecycleManagerDefaultRole. The console creates the same role when you build your first policy. If your organization restricts IAM, create a role with a trust policy for dlm.amazonaws.com and permissions to create, copy, describe, tag and delete snapshots.
2. Tag the volumes
The policy finds volumes by tag, so the tag is the switch that puts a volume under backup:
aws ec2 create-tags \
--resources vol-0123456789abcdef0 \
--tags Key=Backup,Value=daily3. Write the policy details
Save this as policy.json. It snapshots every volume tagged Backup=daily at 03:00 UTC and keeps the newest seven snapshots.
{
"PolicyType": "EBS_SNAPSHOT_MANAGEMENT",
"ResourceTypes": ["VOLUME"],
"TargetTags": [{ "Key": "Backup", "Value": "daily" }],
"Schedules": [
{
"Name": "Daily",
"CopyTags": true,
"TagsToAdd": [{ "Key": "CreatedBy", "Value": "dlm" }],
"CreateRule": {
"Interval": 24,
"IntervalUnit": "HOURS",
"Times": ["03:00"]
},
"RetainRule": { "Count": 7 }
}
]
}CopyTags copies the volume's tags to each snapshot, which helps with cost allocation. TagsToAdd marks snapshots so you can recognize them later.
4. Create and verify the policy
Replace the account ID with your own:
aws dlm create-lifecycle-policy \
--description "Daily snapshots, keep 7" \
--state ENABLED \
--execution-role-arn arn:aws:iam::123456789012:role/AWSDataLifecycleManagerDefaultRole \
--policy-details file://policy.json
aws dlm get-lifecycle-policiesAfter the first scheduled run, list the snapshots DLM created. Every snapshot from a policy carries the system tag aws:dlm:lifecycle-policy-id:
aws ec2 describe-snapshots \
--owner-ids self \
--filters Name=tag-key,Values=aws:dlm:lifecycle-policy-id \
--query 'Snapshots[].[SnapshotId,StartTime,State,VolumeId]' \
--output tableRetention behavior and edge cases
Retention is where most surprises happen.
- Count versus age. Count-based retention accepts values from 1 to 1000 and always keeps the newest N. Age-based retention deletes by days since creation. If a schedule pauses, count-based retention keeps old snapshots indefinitely. Age-based retention would still delete them. Pick the one that matches your recovery requirement. "Restore to any point in the last 14 days" is an age rule, and "keep the last 7 backups" is a count rule.
- Incremental storage. Deleting an old snapshot never breaks newer ones. EBS keeps any block that a later snapshot still references. So a count of 7 does not cost seven times the volume size. You pay for the changed blocks across all retained snapshots.
- Scope of deletion. DLM deletes only snapshots that it created under that policy and schedule. Snapshots you take by hand, or that belong to another policy, are never expired by it. Manual snapshots therefore need their own cleanup process.
- Deleting a policy. Deleting or disabling a policy stops creation and expiry. It does not delete existing snapshots. Those snapshots keep costing money until you remove them yourself.
- Overlapping policies. If two policies match one volume, both create snapshots. Use distinct tag values, or one policy with several schedules, to avoid duplicates.
- Consistency. Snapshots of a running volume are crash-consistent. This is like recovering after a power loss. Databases usually recover from that, but if you need application-consistent snapshots, DLM supports pre and post scripts that run through AWS Systems Manager.
- Snapshots of tagged-but-missing resources. If a volume loses its tag, DLM silently stops protecting it. The next run simply does not include it. Tag governance, such as an AWS Config rule that checks for the
Backuptag, closes this gap. - Encrypted volumes. DLM snapshots of encrypted volumes are encrypted with the volume's KMS key. The execution role must be allowed to use that key, or the run fails.
Copies and longer retention
A snapshot in the same Region and account does not protect you from a Regional failure or a compromised account. DLM schedules can add cross-Region copy rules, which copy each snapshot to another Region with its own retention. Cross-account sharing is also available. For long-term retention of snapshots you rarely restore, a schedule can move them to the EBS Snapshots Archive tier. This lowers storage cost, but archived snapshots must be restored before use and are billed for a minimum period of 90 days.
Using AWS Backup instead
In AWS Backup, you create a backup plan containing rules. Each rule has a schedule, a destination backup vault, and a retention period in days. A resource assignment then picks EBS volumes by tag or ARN. Backup stores each result as a recovery point and enforces retention itself. Do not delete those snapshots through the EC2 console, because Backup manages their lifecycle. Vault Lock can make recovery points immutable, which is the main reason teams choose Backup for compliance.
Exercise
A DLM policy keeps the newest 7 snapshots with a daily schedule. The execution role is accidentally deleted for 10 days, then restored. A teammate also took 3 manual snapshots of the same volume during that time. What happens to the snapshots when the policy resumes, and how many does DLM expire?
Show answer
The policy resumes creating daily snapshots, and count-based retention keeps the newest 7 that DLM created under that policy. Snapshots older than the newest 7 are expired. Because no snapshots were created for 10 days, the older ones were not deleted during the outage, so the oldest DLM snapshots remain until new ones push them out of the count. The 3 manual snapshots are never touched, because DLM only deletes snapshots it created. They must be deleted by hand.
Exercise
You delete a lifecycle policy to clean up. What happens to the snapshots it created, and what command filter finds them?
Show answer
The snapshots remain and keep incurring storage charges. Find them with aws ec2 describe-snapshots --owner-ids self --filters Name=tag-key,Values=aws:dlm:lifecycle-policy-id and delete the ones you no longer need.