AWS Delete 110,000 snapshots - Have script, needs improvements
Budget: $30 – $250 USD
I have an AWS CLI script that lists snapshots older than X amount of days (currently 7), puts them into a flat file and then deletes them. The script works but it seems to get stuck probably due to have to manage a file with over 100,000 snapshot ids. I was looking for some improvement to this script so that it works and runs needing to be logged into cli (could trigger by cron). Also there isn't any need to save any snapshots, looking for them all to be deleted.
Here's the script:
#!/bin/bash -x
CURRENT_DATE=$(date +%Y-%m-%d);
OWNER_ID=*****
OLD_DATE=`date +%Y-%m-%d --date '7 days ago'`
aws ec2 describe-snapshots --owner-ids $OWNER_ID --query "Snapshots[?StartTime<=\`$OLD_DATE\`].SnapshotId" --output text > oldsnaps.txt
LIST=`cat oldsnaps.txt | wc -l`
if [ $LIST -eq "0" ]; then
echo "No snapshots older than 7 days have been found on your server."
exit
else
echo "Deleted Snapshots:" > deleted.txt
echo "" >> deleted.txt
for i in `cat oldsnaps.txt`
do
aws ec2 delete-snapshot --snapshot-id $i
echo $i : Deleted >> deleted.txt
done
fi
Here's the script:
#!/bin/bash -x
CURRENT_DATE=$(date +%Y-%m-%d);
OWNER_ID=*****
OLD_DATE=`date +%Y-%m-%d --date '7 days ago'`
aws ec2 describe-snapshots --owner-ids $OWNER_ID --query "Snapshots[?StartTime<=\`$OLD_DATE\`].SnapshotId" --output text > oldsnaps.txt
LIST=`cat oldsnaps.txt | wc -l`
if [ $LIST -eq "0" ]; then
echo "No snapshots older than 7 days have been found on your server."
exit
else
echo "Deleted Snapshots:" > deleted.txt
echo "" >> deleted.txt
for i in `cat oldsnaps.txt`
do
aws ec2 delete-snapshot --snapshot-id $i
echo $i : Deleted >> deleted.txt
done
fi