Skip to content

Instantly share code, notes, and snippets.

@wokamoto
Last active September 16, 2016 02:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wokamoto/12706478f84d675dad96 to your computer and use it in GitHub Desktop.
Save wokamoto/12706478f84d675dad96 to your computer and use it in GitHub Desktop.
awscli と jq を使ってインスタンスタイプを変更する奴
#!/bin/sh
AWS_PROFILE='YOUR aws-cli PROFILE HERE'
INSTANCE_ID='YOUR INSTANCE ID HERE'
INSTANCE_TYPE='YOUR INSTANCE TYPE HERE'
PUBLIC_IP='YOUR INSTANCE EIP HERE'
while :
do
EC2_STATE=`aws ec2 describe-instances --profile ${AWS_PROFILE} --instance-ids ${INSTANCE_ID} | jq -r '.Reservations [] .Instances [] .State | .Name'`
if [ $EC2_STATE = 'running' ]; then
aws ec2 stop-instances --profile ${AWS_PROFILE} --instance-ids ${INSTANCE_ID}
fi
if [ $EC2_STATE = 'stopped' ]; then
break
fi
echo "${INSTANCE_ID} : ${EC2_STATE}"
sleep 10
done
aws ec2 modify-instance-attribute --profile ${AWS_PROFILE} --instance-id ${INSTANCE_ID} --instance-type ${INSTANCE_TYPE}
aws ec2 start-instances --profile ${AWS_PROFILE} --instance-ids ${INSTANCE_ID}
while :
do
EC2_STATE=`aws ec2 describe-instances --profile ${AWS_PROFILE} --instance-ids ${INSTANCE_ID} | jq -r '.Reservations [] .Instances [] .State | .Name'`
if [ $EC2_STATE = 'running' ]; then
break
fi
echo "${INSTANCE_ID} : ${EC2_STATE}"
sleep 10
done
EC2_EIP=`aws ec2 describe-instances --profile ${AWS_PROFILE} --instance-ids ${INSTANCE_ID} | jq -r '.Reservations [] .Instances [] .PublicIpAddress'`
if [ $EC2_EIP != $PUBLIC_IP ]; then
aws ec2 associate-address --profile ${AWS_PROFILE} --instance-id ${INSTANCE_ID} --public-ip ${PUBLIC_IP}
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment