Last active
November 25, 2018 14:31
-
-
Save maljolani/0d29a4b52a493939ad1d2234173555ad to your computer and use it in GitHub Desktop.
Bulk AWS instances termination
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 characters
#!/usr/bin/env bash | |
# Created by Mohammad AlJolani <[email protected]> | |
# Github Gist : https://gist.github.com/mohammadjolani/0d29a4b52a493939ad1d2234173555ad | |
# Termination instances ids should be stored in a file line by line like | |
# i-05e31789b5688e970 | |
# i-0999e3c0b69e5c81a | |
LOG_PATH="logs" | |
RUN_LOG="${LOG_PATH}/termination.log" | |
S_LOG="${LOG_PATH}/success_instances.log" | |
F_LOG="${LOG_PATH}/failed_instances.log" | |
PROGRESS_BAR=0 | |
mkdir -p $LOG_PATH | |
cat /dev/null >> $RUN_LOG | |
cat /dev/null >> $S_LOG | |
cat /dev/null >> $F_LOG | |
TARGET_SIZE=$(wc -l $1 | awk '{print $1}' ) | |
PROGRESS_BAR_PERCENT=$(echo "scale=2; 100/${TARGET_SIZE}" | bc) | |
ceil() { | |
echo "define ceil (x) {if (x<0) {return x/1} \ | |
else {if (scale(x)==0) {return x} \ | |
else {return x/1 + 1 }}} ; ceil($1)" | bc | |
} | |
prog() { | |
local w=80 p=$(ceil ${1}); shift | |
# create a string of spaces, then change them to dots | |
printf -v dots "%*s" "$(( $p*$w/100 ))" ""; dots=${dots// /.}; | |
# print those dots on a fixed-width space plus the percentage etc. | |
printf "\r\e[K|%-*s| %3d %% %s" "$w" "$dots" "$p" "$*"; | |
} | |
cat $1 | while read instance_id | |
do | |
PROGRESS_BAR=$(echo "${PROGRESS_BAR}+${PROGRESS_BAR_PERCENT}" | bc) | |
prog "${PROGRESS_BAR}" ${instance_id} | |
aws ec2 terminate-instances --region="us-west-2" --instance-ids $instance_id >> $RUN_LOG 2>&1 | |
if [[ $? == 0 ]] | |
then | |
echo "${instance_id}" >> $S_LOG | |
else | |
echo "${instance_id}" >> $F_LOG | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment