Skip to content

Instantly share code, notes, and snippets.

@denisrondalev
Created October 6, 2023 08:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save denisrondalev/778a667fb9b98768486bb8168de49433 to your computer and use it in GitHub Desktop.
Save denisrondalev/778a667fb9b98768486bb8168de49433 to your computer and use it in GitHub Desktop.
remove-ami
#!/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