Created
January 21, 2022 14:33
-
-
Save alastairhm/85d5724770418065252e69882f61ec2e to your computer and use it in GitHub Desktop.
AWS CLI delete all but last five EBS snapshots with a certain name
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# | |
# Delete all but the last 5 snapshots with passed tag. | |
SNAPSHOTSID=$(aws ec2 describe-snapshots \ | |
--owner-ids self \ | |
--filters Name=tag:Name,Values="${1}" \ | |
--query "Snapshots[*].{ID:SnapshotId}" --output text|tail +5) | |
for id in $SNAPSHOTSID; do | |
aws ec2 delete-snapshot --snapshot-id "$id" | |
echo "Successfully deleted snapshot $id" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment