2022-04-14 13:35:10 +00:00
|
|
|
#!/bin/bash
|
2023-04-27 07:34:14 +00:00
|
|
|
# set -x
|
2022-04-14 13:35:10 +00:00
|
|
|
|
2024-07-15 12:52:22 +00:00
|
|
|
TAG_FILTER="Name=tag:Name,Values=zdt-alpine-3.20*dev*" #-dev" #kubezero*"
|
2024-04-08 13:01:20 +00:00
|
|
|
#TAG_FILTER="Name=tag:Name,Values=zdt-alpine-*kubezero*1.28*"
|
2022-04-14 13:35:10 +00:00
|
|
|
|
2023-04-27 07:34:14 +00:00
|
|
|
echo "Are you really sure to delete AMIs matching \"$TAG_FILTER\" ?"
|
|
|
|
read
|
|
|
|
|
2022-04-14 13:35:10 +00:00
|
|
|
#for r in $(aws ec2 describe-regions --query "Regions[].{Name:RegionName}" --output text); do
|
2022-10-31 16:20:05 +00:00
|
|
|
for r in eu-central-1 us-west-2 ap-southeast-2 ca-central-1 us-east-1 us-west-1; do
|
2022-04-14 13:35:10 +00:00
|
|
|
amis=$(aws ec2 describe-images --region $r --owners self --output json --filters $TAG_FILTER | jq -r '.Images[].ImageId')
|
|
|
|
for a in $amis; do
|
|
|
|
aws ec2 deregister-image --region $r --image-id $a && echo "Deleted AMI $a in $r"
|
|
|
|
done
|
|
|
|
|
2023-05-24 09:54:05 +00:00
|
|
|
# failed AMIs
|
2022-04-14 13:35:10 +00:00
|
|
|
amis=$(aws ec2 describe-images --region $r --owners self --output json --filters Name=state,Values=failed | jq -r '.Images[].ImageId')
|
|
|
|
for a in $amis; do
|
|
|
|
aws ec2 deregister-image --region $r --image-id $a && echo "Deleted AMI $a in $r"
|
|
|
|
done
|
2023-05-24 09:54:05 +00:00
|
|
|
amis=$(aws ec2 describe-images --region $r --owners self --output json --query 'Images[? ! not_null(Tags)].{ID:ImageId,Name:Tags[?Key==`Name`]|[0].Value}' | jq -r '.[].ID')
|
|
|
|
for a in $amis; do
|
|
|
|
aws ec2 deregister-image --region $r --image-id $a && echo "Deleted AMI $a in $r"
|
|
|
|
done
|
2022-04-14 13:35:10 +00:00
|
|
|
|
|
|
|
snapshots=$(aws ec2 describe-snapshots --region $r --owner-ids self --output json --filters $TAG_FILTER | jq -r '.Snapshots[].SnapshotId')
|
|
|
|
for s in $snapshots; do
|
|
|
|
aws ec2 delete-snapshot --snapshot-id $s --region $r && echo "Deleted snapshot $s in $r"
|
|
|
|
done
|
2023-05-24 09:54:05 +00:00
|
|
|
|
|
|
|
# delete orphaned snapshots
|
|
|
|
snapshots=$(aws ec2 describe-snapshots --region $r --owner-ids self --output json --query 'Snapshots[? ! not_null(Tags)].{ID:SnapshotId,Time:StartTime,Name:Tags[?Key==`Name`]|[0].Value}' | jq -r '.[].ID')
|
|
|
|
for s in $snapshots; do
|
|
|
|
aws ec2 delete-snapshot --snapshot-id $s --region $r && echo "Deleted snapshot $s in $r"
|
|
|
|
done
|
2022-04-14 13:35:10 +00:00
|
|
|
done
|