Skip to content

Instantly share code, notes, and snippets.

@joshspicer
Created November 7, 2018 11:42
Show Gist options
  • Save joshspicer/db1fcec8988afd36a320062570eca219 to your computer and use it in GitHub Desktop.
Save joshspicer/db1fcec8988afd36a320062570eca219 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Use with `launchService.sh`
if [ "$#" -ne 2 ]; then
echo "[-] Usage: ./removeIntance.sh <aws profile name> <identifier.txt>"
echo "Identifier must be in this directory. Include the .txt"
exit 1
fi
rm debugRemove.txt &> /dev/null
# Set profile name passed in.
profileName=$1
instanceID=$(sed -n '1p' ./"$2")
volumeID=$(sed -n '2p' ./"$2")
# Check if we have a valid identifer file.
if [ $? -ne 0 ]; then
echo "Please choose a valid identifier file"
exit 1
fi
read -p "[+] Are you sure you'd like to terminate this instance? (Y) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]
then
#Terminate the instance.
aws ec2 terminate-instances --profile "$profileName" --instance-ids "$instanceID" >> debugRemove.txt
echo "[+] Waiting for instance to be terminated...."
keepWaiting=true
while $keepWaiting; do
status=$(aws ec2 describe-instance-status --profile "$profileName" --include-all-instances --instance-id "$instanceID" | jq -r '.InstanceStatuses[].InstanceState.Name')
if [[ $status == "terminated" ]]; then
keepWaiting=false
break
fi
echo "[+] Still working. Status: $status"
sleep 8s
done
echo "[+] Removing volume."
aws ec2 --profile "$profileName" delete-volume --volume-id "$volumeID"
echo "[+] Process Complete."
else
echo "[-] Termination canceled."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment