Last active
March 7, 2023 12:19
-
-
Save elasticdog/11152144 to your computer and use it in GitHub Desktop.
Revisions
-
elasticdog revised this gist
Dec 9, 2015 . 1 changed file with 62 additions and 17 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -28,16 +28,61 @@ die() { exit "$st" } # print this script's usage message to stderr usage() { cat <<-EOF >&2 Usage: delete-ami -r REGION -a AMI-ID [-h] Deregister the given AMI and delete its root device snapshot EOF } ##### Main # reset all variables that might be set region='' ami_id='' # parse command line options while [[ "$1" != '' ]] do case $1 in -r | --region) region=$2 shift ;; --region=*) region=${1#*=} ;; -a | --ami-id) ami_id=$2 shift ;; --ami-id=*) ami_id=${1#*=} ;; -h | --help | -\?) usage exit 0 ;; --*) warn "unknown option -- ${1#--}" usage exit 1 ;; *) warn "unknown option -- ${1#-}" usage exit 1 ;; esac shift done # check for required command line options if [[ ! $region ]]; then die 1 "option '--region=REGION' not given; see --help" elif [[ ! $ami_id ]]; then die 1 "option '--ami-id=AMI_ID' not given; see --help" fi # check for dependencies @@ -47,24 +92,24 @@ for cmd in {aws,jq}; do fi done AMI_DESCRIPTION=$(aws --region "$region" ec2 describe-images --image-ids "$ami_id" 2> /dev/null) if [[ -z $AMI_DESCRIPTION ]]; then warn 'unable to find an AMI with the ID "%s"' "$ami_id" die 1 'run the following command to debug:\n aws --region %s ec2 describe-images --image-ids "%s"' "$region" "$ami_id" fi ROOT_DEVICE_NAME=$(printf "$AMI_DESCRIPTION" | jq --raw-output '.[][].RootDeviceName') SNAPSHOT_ID=$(printf "$AMI_DESCRIPTION" | jq --raw-output ".[][].BlockDeviceMappings[] | select(.DeviceName == \"$ROOT_DEVICE_NAME\") | .Ebs.SnapshotId") if [[ -z $SNAPSHOT_ID ]]; then warn 'unable to find a Snapshot ID for the "%s" root device on %s' "$ROOT_DEVICE_NAME" "$ami_id" die 1 'run the following command to debug:\n aws --region %s ec2 describe-images --image-ids "%s"' "$region" "$ami_id" else if ! aws --region "$region" ec2 deregister-image --image-id "$ami_id" > /dev/null; then die 'image deregistration failed; run the following command to debug:\n aws --region %s ec2 deregister-image --image-id "%s"' "$region" "$ami_id" fi if ! aws --region "$region" ec2 delete-snapshot --snapshot-id "$SNAPSHOT_ID" > /dev/null; then die 'snapshot deletion failed; run the following command to debug:\n aws --region %s ec2 delete-snapshot --snapshot-id "%s"' "$region" "$SNAPSHOT_ID" fi fi -
elasticdog created this gist
Apr 21, 2014 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,71 @@ #!/usr/bin/env bash # # delete-ami # # A script to deregister an Amazon Machine Image (AMI) and # delete its corresponding root device snapshot. # ##### Functions # print a message to stderr warn() { local fmt="$1" shift printf "delete-ami: $fmt\n" "$@" >&2 } # print a message to stderr and exit with either # the given status or that of the most recent command die() { local st="$?" if [[ "$1" != *[^0-9]* ]]; then st="$1" shift fi warn "$@" exit "$st" } ##### Main # check command line arguments if [[ -z $1 ]]; then printf 'Usage: delete-ami AMI-ID\n' printf 'Deregister the given AMI and delete its root device snapshot\n' exit 1 else AMI_ID=$1 fi # check for dependencies for cmd in {aws,jq}; do if ! command -v $cmd > /dev/null; then die 1 'required command "%s" was not found' "$cmd" fi done AMI_DESCRIPTION=$(aws ec2 describe-images --image-ids "$AMI_ID" 2> /dev/null) if [[ -z $AMI_DESCRIPTION ]]; then warn 'unable to find an AMI with the ID "%s"' "$AMI_ID" die 1 'run the following command to debug:\n aws ec2 describe-images --image-ids "%s"' "$AMI_ID" fi ROOT_DEVICE_NAME=$(printf "$AMI_DESCRIPTION" | jq --raw-output '.[][].RootDeviceName') SNAPSHOT_ID=$(printf "$AMI_DESCRIPTION" | jq --raw-output ".[][].BlockDeviceMappings | .[] | select(.DeviceName == \"$ROOT_DEVICE_NAME\") | .Ebs.SnapshotId") if [[ -z $SNAPSHOT_ID ]]; then warn 'unable to find a Snapshot ID for the "%s" root device on %s' "$ROOT_DEVICE_NAME" "$AMI_ID" die 1 'run the following command to debug:\n aws ec2 describe-images --image-ids "%s"' "$AMI_ID" else if ! aws ec2 deregister-image --image-id "$AMI_ID" > /dev/null; then die 'image deregistration failed; run the following command to debug:\n aws ec2 deregister-image --image-id "%s"' "$AMI_ID" fi if ! aws ec2 delete-snapshot --snapshot-id "$SNAPSHOT_ID" > /dev/null; then die 'snapshot deletion failed; run the following command to debug:\n aws ec2 delete-snapshot --snapshot-id "%s"' "$SNAPSHOT_ID" fi fi exit 0