Skip to content

Instantly share code, notes, and snippets.

@bushong1
Created November 17, 2017 14:18
Show Gist options
  • Save bushong1/359f91d02eebe327d264f15d3777674b to your computer and use it in GitHub Desktop.
Save bushong1/359f91d02eebe327d264f15d3777674b to your computer and use it in GitHub Desktop.
A BASH function to shortcut removing an AMI and it's associated Snapshots
function aws-rm-ami-snap() {
if [[ ${1} == ami-* ]]; then
AMI_ID=${1}
SNAPSHOT_IDS=$(aws ec2 describe-images --filters Name=image-id,Values=${1} | jq '.Images[].BlockDeviceMappings[].Ebs.SnapshotId' | sed 's/"//g')
aws ec2 deregister-image --image-id ${AMI_ID}
for snapshot_id in ${SNAPSHOT_IDS}; do
aws ec2 delete-snapshot --snapshot-id ${snapshot_id}
done
else
echo "ERROR: Require \$1 to be an AMI ID, starting with 'ami-'"
return 1
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment