Created
October 6, 2023 08:43
-
-
Save denisrondalev/778a667fb9b98768486bb8168de49433 to your computer and use it in GitHub Desktop.
remove-ami
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
#!/bin/bash | |
CURRENT_DATE=$(date -d '-30 day' -u +"%Y-%m-%dT%H:%M:%S.%NZ") | |
echo "Date: $CURRENT_DATE" | |
AMIS=$(aws ec2 describe-images --owners self --query 'Images[?CreationDate<`'"$CURRENT_DATE"'`].{ID:ImageId, Date:CreationDate}' --output json) | |
for AMI in $(echo "${AMIS}" | jq -r '.[].ID'); do | |
echo "Deleting AMI: $AMI" | |
aws ec2 deregister-image --image-id $AMI | |
done | |
SNAPSHOTS=$(aws ec2 describe-snapshots --owner-ids self --query 'Snapshots[?StartTime<`'"$CURRENT_DATE"'`]' --output json) | |
for SNAPSHOT in $(echo "${SNAPSHOTS}" | jq -r '.[].SnapshotId'); do | |
echo "Deleting Snapshot: $SNAPSHOT" | |
aws ec2 delete-snapshot --snapshot-id $SNAPSHOT | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment