Skip to content

Instantly share code, notes, and snippets.

@alexstaravoitau
Last active November 23, 2016 13:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexstaravoitau/17c8639d8a4225268f64d5c1b59a4a50 to your computer and use it in GitHub Desktop.
Save alexstaravoitau/17c8639d8a4225268f64d5c1b59a4a50 to your computer and use it in GitHub Desktop.
Stops your AWS instance. Check out my post about usage of this script: http://navoshta.com/aws-tensorflow/
#!/bin/bash
# Checks current AWS instance state and stops it if it's running.
AWS_INSTANCE_ID="<Your instance ID here>"
AWS_STATE=$(aws ec2 describe-instances --instance-ids $AWS_INSTANCE_ID --query "Reservations[*].Instances[*].State.Name" --output text)
if [ "$AWS_STATE" == "running" ]; then
aws ec2 stop-instances --instance-ids $AWS_INSTANCE_ID >/dev/null
echo -n "The AWS instance is now stopping. It usually takes a while, so feel free to CTRL+C if you don't want to wait till the instance has fully stopped."
echo "Stopping instance"
# Wait till the instance has actually stopped.
while AWS_STATE=$(aws ec2 describe-instances --instance-ids $AWS_INSTANCE_ID --query "Reservations[*].Instances[*].State.Name" --output text); test "$AWS_STATE" != "stopped"; do
sleep 5; echo -n '.'
done
echo " AWS instance stopped. "
else
echo "AWS instance is not running."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment