k-masatany.com

  • About
  • Blog
  • Contact
  • About
  • Blog
  • Contact

2018. 12. 6

[小ネタ] EC2 のちょっとした情報が一覧で欲しい時のワンライナー

この記事は Fusic その2 Advent Calendar 2019 7日目の記事です。

オートスケール等を組んでいることにより EC2 の IPアドレスが変化するインフラをメンテナンスしていて、一々マネジメントコンソールや cli コマンドの結果を確認して SSH 接続するのが面倒なので、EC2の代表的な情報を一覧で取得するためのワンライナーを作成してエイリアスコマンドを作りました。

aws ec2 describe-instances | jq -r '\"Name, InstanceId, PublicIpAddress, PrivateIpAddress\" as $title | [ .Reservations[].Instances[] | [.Tags[] | select(.Key == \"Name\").Value][] + \", \" + .InstanceId + \", \" + .PublicIpAddress + \", \" + .PrivateIpAddress] | sort as $rows | [$title] + $rows | .[]' | column -t -s \",\"

実行すると、下記のような結果が得られます(情報は伏せています)。

Name   InstanceId   PublicIpAddress   PrivateIpAddress
---    ---          ---               ---

jq コマンドで取ってくる情報を増やせばカラムは増えます。

aws ec2 describe-instances | jq -r '\"Name, InstanceId, InstanceType, State, PublicIpAddress, PrivateIpAddress, AZ\" as $title | [ .Reservations[].Instances[] | [.Tags[] | select(.Key == \"Name\").Value][] + \", \" + .InstanceId + \", \" + .InstanceType + \", \" + .State.Name + \", \" + .PublicIpAddress + \", \" + .PrivateIpAddress + \", \" + .Placement.AvailabilityZone] | sort as $rows | [$title] + [\"---, ---, ---, ---, ---, ---, ---\"] + $rows | .[]' | column -t -s \",\"
Name   InstanceId   InstanceType   State   PublicIpAddress   PrivateIpAddress   AZ
---    ---          ---            ---     ---               ---                ---

ステータスやインスタンスタイプも併せて確認できるようになりました。 これでIPアドレスがころころ変わる環境でも、ストレスフリーですね。

プロフィールアイコン
k-masatany k-masatany's memorandum
このエントリーをTwitterで共有 このエントリーをLINEで共有 このエントリーをはてなブックマークに追加

Table of Contents

© 2021 k-masatany