Skip to content

Instantly share code, notes, and snippets.

@koleror
Created July 2, 2018 19:47
Show Gist options
  • Save koleror/da373b0393052ff7867f27cca7d592a7 to your computer and use it in GitHub Desktop.
Save koleror/da373b0393052ff7867f27cca7d592a7 to your computer and use it in GitHub Desktop.
Manage AWS instance from command line
#!/bin/bash
AWS_PROFILE=<ENTER AWS PROFILE NAME HERE>
AWS_REGION=<ENTER INSTANCE REGION>
AWS_INSTANCE_ID=<ENTER INSTANCE ID HERE>
red=`tput setaf 1`
green=`tput setaf 2`
reset=`tput sgr0`
command -v aws > /dev/null
if [[ $? != 0 ]]; then
echo "${red}Please install awscli and try again${reset}"
exit 1;
fi
set -e
get_status () {
status=`aws ec2 --profile $AWS_PROFILE --region $AWS_REGION describe-instances --instance-ids $AWS_INSTANCE_ID`;
status_name=`python -c "import json; print json.loads(\"\"\"$status\"\"\")['Reservations'][0]['Instances'][0]['State']['Name']"`
echo $status_name
}
status () {
status_name=`get_status`
if [ "$status_name" = "stopped" ]; then
echo "${red}Server status: stopped.${reset}"
elif [ "$status_name" = "stopping" ]; then
echo "${red}Server status: stopping...${reset}"
elif [ "$status_name" = "running" ]; then
echo "${green}Server status: running.${reset}"
else
echo "Server status: $status_name."
fi
}
wait_stopping () {
status_name=`get_status`;
if [ "$status_name" = "stopping" ]; then
printf "."
wait_stopping inner
if [ "$1" != "inner" ]; then printf "\n"; fi
fi
}
start () {
try_again="_try_again_"
wait_stopping;
status=`aws ec2 --profile $AWS_PROFILE --region $AWS_REGION start-instances --instance-ids $AWS_INSTANCE_ID`;
current_state=`python -c "import json; print json.loads(\"\"\"$status\"\"\")['StartingInstances'][0]['CurrentState']['Name']"`
previous_state=`python -c "import json; print json.loads(\"\"\"$status\"\"\")['StartingInstances'][0]['PreviousState']['Name']"`
if [ "$previous_state" = "running" ] && [ "$current_state" = "running" ]; then
if [ "$1" = $try_again ]; then printf "\n"; fi
echo "${green}Server already started.${reset}"
elif [ "$previous_state" = "stopped" ] && [ "$current_state" = "pending" ]; then
echo "${green}Server is starting...${reset}"
elif [ "$previous_state" = "pending" ] && [ "$current_state" = "pending" ]; then
printf "."
start $try_again;
else
echo "Previous state: $previous_state, current state: $current_state"
fi
}
stop () {
status=`aws ec2 --profile $AWS_PROFILE --region $AWS_REGION stop-instances --instance-ids $AWS_INSTANCE_ID`;
current_state=`python -c "import json; print json.loads(\"\"\"$status\"\"\")['StoppingInstances'][0]['CurrentState']['Name']"`
previous_state=`python -c "import json; print json.loads(\"\"\"$status\"\"\")['StoppingInstances'][0]['PreviousState']['Name']"`
if [ "$previous_state" = "stopped" ] && [ "$current_state" = "stopped" ]; then
echo "${red}Server already stopped.${reset}"
elif [ "$previous_state" = "running" ] && [ "$current_state" = "stopping" ]; then
echo "${green}Server is stopping...${reset}"
elif [ "$previous_state" = "pending" ] && [ "$current_state" = "stopping" ]; then
echo "${green}Server is stopping...${reset}"
elif [ "$previous_state" = "stopping" ] && [ "$current_state" = "stopping" ]; then
echo "${red}Server is already stopping.${reset}"
else
echo "Previous state: $previous_state, current state: $current_state"
fi
}
usage () {
echo "`basename $0` [status|start|stop|help]"
echo " * status: Show server status"
echo " * start: Start server"
echo " * stop: Stop server"
echo " * help: Show usage"
}
if [ "$1" = "status" ]; then status;
elif [ "$1" = "start" ]; then start;
elif [ "$1" = "stop" ]; then stop;
else usage;
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment