Skip to content

Instantly share code, notes, and snippets.

@aaronzirbes
Last active November 11, 2015 01:44
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aaronzirbes/9f79dc61c215e0137162 to your computer and use it in GitHub Desktop.
Save aaronzirbes/9f79dc61c215e0137162 to your computer and use it in GitHub Desktop.
Delete orphaned artifacts left by packer.io
#!/bin/bash
set -e
# Keypairs
echo "Finding Key Pairs..."
KEY_PAIRS_JSON=`aws ec2 describe-key-pairs --filters 'Name=key-name,Values=packer*'`
KEY_PAIRS=`echo "${KEY_PAIRS_JSON}" | grep 'KeyName' |sed -e 's/.*"KeyName": "//' -e 's/",* *$//'`
# Security Groups
echo "Finding Security Groups..."
SECURITY_GROUPS_JSON=`aws ec2 describe-security-groups --filters 'Name=description,Values=Temporary group for Packer'`
SECURITY_GROUPS=`echo "${SECURITY_GROUPS_JSON}" | grep 'GroupId' |sed -e 's/.*"GroupId": "//' -e 's/",* *$//'`
SECURITY_GROUPS_LEFT="${SECURITY_GROUPS}"
# Instances
INSTANCES=''
QUERY_COUNT=1
INSTANCE_QUERY_JSON="security_groups${QUERY_COUNT}.json"
while [ "${SECURITY_GROUPS_LEFT}" != "" ]; do
echo "Finding Instances (pass ${QUERY_COUNT}, potential 200 security group limit)..."
FILTER_COUNT=0
REMAINING=''
echo '[' > ${INSTANCE_QUERY_JSON}
echo ' { "Name": "group-id", "Values":' >> ${INSTANCE_QUERY_JSON}
echo -n ' [' >> ${INSTANCE_QUERY_JSON}
COMMA=''
for SECURITY_GROUP in ${SECURITY_GROUPS_LEFT}; do
FILTER_COUNT=$((FILTER_COUNT + 1))
if ((( $FILTER_COUNT < 200 ))); then
echo "${COMMA}" >> ${INSTANCE_QUERY_JSON}
echo -n " \"${SECURITY_GROUP}\"" >> ${INSTANCE_QUERY_JSON}
COMMA=","
else
REMAINING="${REMAINING} ${SECURITY_GROUP}"
fi
done
echo '' >> ${INSTANCE_QUERY_JSON}
echo ' ]' >> ${INSTANCE_QUERY_JSON}
echo ' }' >> ${INSTANCE_QUERY_JSON}
echo ']' >> ${INSTANCE_QUERY_JSON}
THESE_INSTANCES_JSON=`aws ec2 describe-instances --filters file://${INSTANCE_QUERY_JSON}`
THESE_INSTANCE_IDS=`echo "${THESE_INSTANCES_JSON}" | grep 'InstanceId' |sed -e 's/.*"InstanceId": "//' -e 's/",* *$//'`
INSTANCES="${THESE_INSTANCE_IDS}
${INSTANCES}"
SECURITY_GROUPS_LEFT="${REMAINING}"
QUERY_COUNT=$((QUERY_COUNT + 1))
rm "${INSTANCE_QUERY_JSON}"
done
KEY_PAIRS_NO_PACKER=`echo ${KEY_PAIRS} |sed -e 's/packer //g'`
INSTANCES_COMMA=`echo ${INSTANCES} |sed -e 's/ /,/g'`
echo KEYS: ${KEY_PAIRS_NO_PACKER}
echo GROUPS: ${SECURITY_GROUPS}
echo INSTANCES: ${INSTANCES_COMMA}
if [ "$1" == "--apply" ]; then
if [ "${INSTANCES}" != "" ]; then
echo "aws ec2 terminate-instances --instance-ids $INSTANCES"
aws ec2 terminate-instances --instance-ids $INSTANCES |tee terminated-instances.txt
fi
if [ "${SECURITY_GROUPS}" != "" ]; then
echo '# Deleting Security Groups...' | tee deleted-security-groups.txt
for SECURITY_GROUP in ${SECURITY_GROUPS}; do
echo "aws ec2 delete-security-group --group-id ${SECURITY_GROUP}" |tee -a deleted-security-groups.txt
aws ec2 delete-security-group --group-id ${SECURITY_GROUP}
done
fi
if [ "${KEY_PAIRS}" != "" ]; then
echo '# Deleting Key Pairs...' | tee deleted-key-pairs.txt
for KEY_PAIR in ${KEY_PAIRS_NO_PACKER}; do
echo "aws ec2 delete-key-pair --key-name \"packer ${KEY_PAIR}\""
aws ec2 delete-key-pair --key-name "packer ${KEY_PAIR}" |tee -a deleted-key-pairs.txt
done
fi
else
echo "Dry run only. Run with '--apply' to ACTUALLY delete objects."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment