Created
March 23, 2016 08:11
-
-
Save tkuchiki/3151ab03d7f4a864a847 to your computer and use it in GitHub Desktop.
EC2 に force stop をかけて、stopped になったら start する
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
INSTANCE="${1}" | |
instance_id() { | |
CODE="${2}" | |
if [ "${CODE}" = "" ]; then | |
aws ec2 describe-instances --filters "Name=tag:Name,Values=${1}" | jq -r ".Reservations[].Instances[].InstanceId" | |
else | |
aws ec2 describe-instances --filters "Name=tag:Name,Values=${1}" "Name=instance-state-code,Values=${CODE}" | jq -r ".Reservations[].Instances[].InstanceId" | |
fi | |
} | |
INSTANCE_ID=$(instance_id ${INSTANCE}) | |
aws ec2 stop-instances --instance-ids ${INSTANCE_ID} --force | |
sleep 5 | |
while :; do | |
STOPPED=$(instance_id ${INSTANCE} 80 | wc -c) | |
[ $STOPPED -gt 0 ] && break | |
sleep 30 | |
done | |
aws ec2 start-instances --instance-ids ${INSTANCE_ID} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment