Skip to content

Instantly share code, notes, and snippets.

@danman01
Created August 9, 2013 17:52
Show Gist options
  • Save danman01/6195621 to your computer and use it in GitHub Desktop.
Save danman01/6195621 to your computer and use it in GitHub Desktop.
toggle your staging amazon ec2 instance when you run the script
#!/bin/bash
# make sure to have amazon ec2 command line tools installed
# this script assumes have an elastic ip, and the instance id is static
# enter your settings here
STAGING="instance-id"
STAGING_IP="your elastic ip"
# get the status if your instance
STATUS=`ec2-describe-instances $STAGING | awk 'FNR == 2 {print $6}'`
echo 'STATUS =' $STATUS
# start or stop depending on the status returned
if [ $STATUS == "running" ]; then
echo "instance is currently running"
echo -n "Enter stop or exit > "
read text
if [ $text == "stop" ]; then
echo "stopping instance $STAGING"
ec2-stop-instances $STAGING
else
exit
fi
else
if [ $STATUS == "stopping" ]; then
echo "instance is stopping. exiting script"
exit
fi
if [ $STATUS == "0" ]; then
# status moved to column 4. column 6 is 0
echo "starting instance $STAGING"
ec2-start-instances $STAGING
echo "assigning ip $STAGING_IP"
sleep 15
ec2-associate-address -i $STAGING $STAGING_IP
echo "instance is already running"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment