2020-07-16 20:09:48 +02:00
|
|
|
#!/bin/bash -x
|
|
|
|
set -eu -o pipefail
|
|
|
|
|
2020-12-10 12:00:23 +01:00
|
|
|
# Process AMI_LIST
|
|
|
|
AMI_LIST=($(echo "$AMI_LIST" | tr ',' '\n'))
|
|
|
|
|
2020-07-16 20:09:48 +02:00
|
|
|
# Remove the list of AMIs in each region
|
2020-12-10 12:00:23 +01:00
|
|
|
for line in "${AMI_LIST[@]}"
|
2020-07-16 20:09:48 +02:00
|
|
|
do
|
2020-12-10 11:25:18 +01:00
|
|
|
REGION=$(echo "${line}" | cut -d":" -f1)
|
|
|
|
AMI_ID=$(echo "${line}" | cut -d":" -f2)
|
2020-12-10 11:29:47 +01:00
|
|
|
export AWS_DEFAULT_REGION=${REGION}
|
2020-12-10 12:00:23 +01:00
|
|
|
|
2022-07-17 20:57:03 +02:00
|
|
|
mapfile -t SNAPSHOTS < <(aws ec2 describe-images --image-ids "$AMI_ID" --include-deprecated --output text --query 'Images[*].BlockDeviceMappings[*].Ebs.SnapshotId')
|
2020-12-10 11:25:18 +01:00
|
|
|
echo "Deregistering $AMI_ID"
|
|
|
|
aws ec2 deregister-image --image-id "${AMI_ID}"
|
2020-12-10 11:27:13 +01:00
|
|
|
sleep 1
|
2020-12-10 11:25:18 +01:00
|
|
|
for snapshot in "${SNAPSHOTS[@]}";
|
|
|
|
do
|
|
|
|
echo "Deleting Snapshot $snapshot from $AMI_ID"
|
|
|
|
aws ec2 delete-snapshot --snapshot-id "${snapshot}"
|
2020-12-10 11:27:13 +01:00
|
|
|
sleep 1
|
2020-12-10 11:25:18 +01:00
|
|
|
done
|
2020-07-16 20:09:48 +02:00
|
|
|
done
|