Skip to content

Instantly share code, notes, and snippets.

@Twinuma
Last active September 18, 2019 04:10
Show Gist options
  • Save Twinuma/d857a902b8f0b075d9fd to your computer and use it in GitHub Desktop.
Save Twinuma/d857a902b8f0b075d9fd to your computer and use it in GitHub Desktop.
特定のEC2インスタンスのインスタンスサイズを変更&EIP紐付け
#!/bin/sh
AWS_PROFILE='YOUR awscli PROFILE NAME 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 '.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 '.Reservations [] .Instances [] .State | .Name'`
if [ $EC2_STATE = '"running"' ]; then
break
fi
echo "${INSTANCE_ID} : ${EC2_STATE}"
sleep 10
done
aws ec2 associate-address --profile ${AWS_PROFILE} --instance-id ${INSTANCE_ID} --public-ip ${PUBLIC_IP}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment