1
1

More than 1 year has passed since last update.

EC2インスタンスのインスタンス停止→インスタンスタイプ変更→インスタンス開始をCloudShellで実行してみた。

Posted at

はじめに

こんにちは、山田です。
今回は、EC2インスタンスのインスタンス停止→インスタンスタイプ変更→インスタンス開始をコマンドで実行してみました。

概要

今回は、CloudShellからコマンドにて、インスタンス停止・インスタンスタイプ変更・インスタンス開始を実施していきます。
WS000024.JPG

今回は、m1.mediumからt2.microにインスタンスタイプを変更していきます。
WS000026.JPG

下記が実行するコマンドです。

aws ec2 describe-instances --query 'Reservations[*].Instances[*].[State.Name, InstanceId]' --output text |
> grep running |
> awk '{print $2}' |
> while read line;
> do aws ec2 stop-instances --instance-ids $line;
> sleep 1m;aws ec2 modify-instance-attribute --instance-id $line --instance-type '{"Value": "t2.micro"}';
> aws ec2 start-instances --instance-ids  $line
> done

①インスタンスの状態がrunningのインスタンスIDを取得する。

aws ec2 describe-instances --query 'Reservations[*].Instances[*].[State.Name, InstanceId]' --output text | grep running | awk '{print $2}'

while文にてインスタンス停止、インスタンスタイプ変更、インスタンス開始の繰り返し処理を実行する。

while read line;
> do aws ec2 stop-instances --instance-ids $line;
> sleep 3m;aws ec2 modify-instance-attribute --instance-id $line --instance-type '{"Value": "t2.micro"}';
> aws ec2 start-instances --instance-ids  $line
> done

動作確認

CloudShellにてコマンドを実行します。
WS000027.JPG

インスタンスタイプがt2.microになっていれば完了です。
WS000028.JPG

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1