Skip to content

Instantly share code, notes, and snippets.

@tkuchiki
Created March 23, 2016 08:11
Show Gist options
  • Save tkuchiki/3151ab03d7f4a864a847 to your computer and use it in GitHub Desktop.
Save tkuchiki/3151ab03d7f4a864a847 to your computer and use it in GitHub Desktop.
EC2 に force stop をかけて、stopped になったら start する
#!/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