LoginSignup
3
4

More than 3 years have passed since last update.

AWSコンソールにログインしてインスタンスをポチポチするのはもう嫌なのでターミナルに極振りしたいと思います

Posted at

AWSを複数アカウント扱っていると、コンソールに入って〜インスタンスを起動・停止して〜別のアカウントに切り替えるてコンソールに入って〜インスタンス起動・停止して〜また別のアカウントに切り替えてコンソールに入って〜インスタンス起動・停止して〜・・・・・・

うわああああんっ!!んもぅっいい加減にしてっ!!!!

と言うくらいにもううんざりしてきたので、基本的な操作は全部ターミナルから行うことを固く決意しました。
ということで、EC2/RDS のインスタンス操作を行うための AWS CLI を使ったワンライナーをザザザッと並べていきたいと思います。

EC2

EC2 の操作から見ていきましょう。

確認

単体確認

まずは、基本の確認から。起動/停止するにも情報が必要になりますからね。

EC2単体確認
% aws ec2 describe-instances --profile [profile-name] --instance-ids <instance-id>

複数確認

複数インスタンスを確認したい場合は、--instance-ids オプションにインスタンスIDを複数並べていくだけですね。

EC2複数確認
% aws ec2 describe-instances --profile [profile-name] --instance-ids <instance-id> ... <instance-id>

全確認

次に、全インスタンスの確認。
といっても、全インスタンスの情報を垂れ流しても把握しづらいので、以下の情報だけを取得するようにしています。

  • Nameタグの値
  • インスタンスID
  • パブリックIP
  • プライベートIP
  • インスタンスタイプ
  • 起動時刻
  • インスタンスの状態


AWS CLI の --query オプションでフィルタリングしてもいいのですが、イマイチ分かりづらいので私は jq コマンドでフィルタリングしています。

EC2全インスタンス確認
% aws ec2 describe-instances --profile [profile-name] --instance-ids <instance-id> | jq -r '.Reservations[].Instances[] | { name: .Tags[] | select(.Key == "Name").Value, instanceId: .InstanceId, publicIP: .PublicIpAddress, privateIP: .PrivateIpAddress, InstanceType: .InstanceType, launchTime: .LaunchTime, status: .State.Name }'

describe-instances の詳細は以下を参照。
https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-instances.html

起動

単体起動

まずは、基本的な単体起動から。

EC2単体起動
% aws ec2 start-instances --profile [profile-name] --instance-ids <instance-id>

複数起動

複数インスタンスを起動させたい場合は、--instance-ids オプションにインスタンスIDを複数並べていくだけですね。

EC2複数起動
% aws ec2 start-instances --profile [profile-name] --instance-ids <instance-id> ... <instance-id>

全起動

最後に、停止している全インスタンスの起動。

EC2全起動
% aws ec2 start-instances --profile [profile-name] --instance-ids $(aws ec2 describe-instances | jq -r '[.Reservations[].Instances[] | select(.State.Name == "stopped") | .InstanceId] | join(" ")')

start-instances の詳細は以下を参照。
https://docs.aws.amazon.com/cli/latest/reference/ec2/start-instances.html

停止

単体停止

まずは、基本的な単体停止から。

EC2単体停止
% aws ec2 stop-instances --profile [profile-name] --instance-ids <instance-id>

複数停止

複数インスタンスを停止させたい場合は、起動と同じように --instance-ids オプション にインスタンスIDを複数並べていくだけですね。

EC2複数停止
% aws ec2 stop-instances --profile [profile-name] --instance-ids <instance-id> ... <instance-id>

全停止

最後に、起動している全インスタンスの停止。

EC2全停止
% aws ec2 stop-instances --profile [profile-name] --instance-ids $(aws ec2 describe-instances | jq -r '[.Reservations[].Instances[] | select(.State.Name == "running") | .InstanceId] | join(" ")')

stop-instances の詳細は以下を参照。
https://docs.aws.amazon.com/cli/latest/reference/ec2/stop-instances.html

RDS

RDS の操作も見ていきましょう。

確認

単体確認

まずは、基本的な単体確認から。

RDS単体確認
% aws rds describe-db-instances --profile [profile-name] --db-instance-identifier <db-instance-identifier>

複数(全インスタンス)確認

単体確認で指定されている --db-instance-identifier オプション を外してやれば、複数インスタンスというか全インスタンス確認出来ます。

RDS全インスタンス確認
% aws rds describe-db-instances --profile [profile-name]

といっても、全インスタンスの情報を垂れ流しても把握しづらいので、以下の情報だけを取得するようにしています。

  • インスタンス名
  • インスタンスタイプ
  • DBエンジン
  • DBエンジンバージョン
  • MultiAZの有無
  • ステータス
RDS全インスタンス確認
% aws rds describe-db-instances --profile [profile-name] | jq -r '.DBInstances[] | { instanceIdentifier: .DBInstanceIdentifier, instanceType: .DBInstanceClass, engine: .Engine, engineVersion: .EngineVersion, multiAZ: .MultiAZ, status: .DBInstanceStatus }'

describe-db-instances の詳細は以下を参照。
https://docs.aws.amazon.com/cli/latest/reference/rds/describe-db-instances.html

起動

単体起動

基本的な起動はこちら。

RDS単体起動
% aws rds start-db-instance --profile [profile-name] --db-instance-identifier <db-instance-identifier>

全起動

停止している全インスタンスの起動はこちら。
--db-instance-identifier オプション では複数インスタンスIDを渡すことが出来ないため、私は for-in で回してます。

RDS全起動
% for i in $(aws rds describe-db-instances --profile [profile-name] | jq -r '[.DBInstances[] | select(.DBInstanceStatus == "stopped") | .DBInstanceIdentifier] | join(" ")'); do aws rds start-db-instance --profile [profile-name] --db-instance-identifier $i; done

start-db-instances の詳細は以下を参照。
https://docs.aws.amazon.com/cli/latest/reference/rds/start-db-instance.html

停止

単体停止

基本的な停止はこちら。

RDS単体停止
% aws rds stop-db-instance --profile [profile-name] --db-instance-identifier <db-instance-identifier>

全停止

起動している全インスタンスの停止はこちら。
こちらも起動の時と同じく for-in で回してます。

RDS全停止
% for i in $(aws rds describe-db-instances --profile [profile-name] | jq -r '[.DBInstances[] | select(.DBInstanceStatus == "available") | .DBInstanceIdentifier] | join(" ")'); do aws rds stop-db-instance --profile [profile-name] --db-instance-identifier $i; done

stop-db-instances の詳細は以下を参照。
https://docs.aws.amazon.com/cli/latest/reference/rds/stop-db-instance.html

さいごに

皆様のAWSライフがターミナルでフルになっていただければ幸いでございます。

参考

ec2 — AWS CLI Command Reference
rds — AWS CLI Command Reference
jq Manual (development version)

3
4
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
3
4