awscli, jq が利用できることが前提です。
以下のようなシェルスクリプトを組むだけで、特定のインスタンスのインスタンスタイプを変更できます。
(ついでに EIP の付与も)
This file contains 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/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 |
コメントを残す