Skip to content

Instantly share code, notes, and snippets.

@shim0mura
Last active January 3, 2016 23:19
Show Gist options
  • Save shim0mura/8534644 to your computer and use it in GitHub Desktop.
Save shim0mura/8534644 to your computer and use it in GitHub Desktop.
awsのインスタンスをONにしたりOFFにしたりする奴、stopするとElasticIPが外されちゃうのでstartするときにassociateもする 事前にaws-cliのインストール(sudo pip install awscli)とjqコマンドのパス内への設置、 アクセスキーなどの取得(https://portal.aws.amazon.com/gp/aws/securityCredentials)とElasticIP取得が必要
#!/bin/bash
INSTANCE_ID=xxxxx
ELASTIC_IP=xxxxx
RUNNING_STATUS=1
NOT_RUNNING_STATUS=0
function is_not_instance_running() {
status=$(aws ec2 describe-instance-status --instance-ids $INSTANCE_ID | ~/jq '.InstanceStatuses[].InstanceState.Name')
if [ "${status}" = '"running"' ]; then
return $RUNNING_STATUS
else
return $NOT_RUNNING_STATUS
fi
}
case $1 in
"start" ) aws ec2 start-instances --instance-ids $INSTANCE_ID > /dev/null
if [ $? -ne 0 ]; then touch ~/failed_to_start; fi;
while is_not_instance_running
do
echo "waiting...."
sleep 60
done
aws ec2 associate-address --instance-id $INSTANCE_ID --public-ip $ELASTIC_IP > /dev/null
if [ $? -ne 0 ]; then touch ~/failed_to_associate_ip; fi;
;;
"stop" ) aws ec2 stop-instances --instance-ids $INSTANCE_ID > /dev/null
if [ $? -ne 0 ]; then touch ~/failed_to_stop; fi;
;;
"*" ) echo "usage: sh aws_switcher.sh < start | stop >";;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment