Reputation:
I have 25k + snapshots in my account and wanted them to be deleted. I will do that. But henceforth, I can't do it manually. So have to set a retention period (eg: 10 days) and want them deleted automatically after the retention period with the script.
Do some one have script to do that?
Thanks in advance.
I have tried and have a script to delete the snapshots that are 30 days old. But need a script to delete automatically after a retention period.
The snapshots should be deleted after a retention period.
Upvotes: 2
Views: 11002
Reputation: 377
There is also available solution from Amazon directly:
https://aws.amazon.com/solutions/implementations/ops-automator/
This solution includes actions to automatically create, copy and delete EBS snapshots.
Upvotes: 0
Reputation: 5021
I would recommend using something like the AWS Backup or Lifecycle Manager to do this in an automated manner when you're creating new snapshots but if you need to deal with old snapshots in an existing count which wasn't using that sort of thing, here's how you can do it.
I would strongly recommend configuring Recycling Bin before doing this, just in case you make a mistake.
Note that the examples below use a JMESPath query to filter out anything tagged by AWS Backup.
date
CLI utility to calculate one month before the current date in this example:$ aws ec2 describe-snapshots --owner self --query 'Snapshots[?StartTime <= `'$(date -v -1m -I)'` && !contains(Tags[].Key, `aws:backup:source-resource`)]'
$ aws ec2 describe-snapshots --owner self --query 'Snapshots[?StartTime <= `'$(date -v -1m -I)'` && !contains(Tags[].Key, `aws:backup:source-resource`)].[SnapshotId]' --output=text | xargs -P32 -L1 -p aws ec2 delete-snapshot --snapshot-id
Note the -p
in there to prompt you for confirmation. Remove that when you're absolutely certain that you really want to delete things.
Upvotes: 0
Reputation: 43
To delete these snapshots you can simply use a bash script which will fetch all the snapshot ids older than 10 days and then can delete them using AWS CLI command.
to find all the snapshots created before a particular timestamp (say some date):
aws ec2 describe-snapshots --owner self --output json | jq '.Snapshots[] | select(.StartTime > "2019-12-18")' > snap_list.txt
use for loop to delete all the snapshots from the list generated one by one.
for snap_id in `cat snap_list.txt`
do
aws ec2 delete-snapshot --snapshot-id $snap_id
done
or you can simply use the snapshot retention period in AWS for snapshot automatic deletion in future
Upvotes: 0
Reputation: 7407
You should have a look at AWS Backup:
AWS Backup is a fully managed backup service that makes it easy to centralize and automate the backup of data across AWS services in the cloud and on premises. Using AWS Backup, you can centrally configure backup policies and monitor backup activity for your AWS resources. AWS Backup automates and consolidates backup tasks that were previously performed service-by-service, removing the need to create custom scripts and manual processes. With just a few clicks on the AWS Backup console, you can create backup policies that automate backup schedules and retention management.
See also Automating the Amazon EBS Snapshot Lifecycle:
You can use Amazon Data Lifecycle Manager (Amazon DLM) to automate the creation, retention, and deletion of snapshots taken to back up your Amazon EBS volumes.
Upvotes: 3