aws cliでEC2 RDSの一覧を取得する


AWS CLIでEC2/RDSの一覧をCSV or TSVで吐き出す

やりたいこと

EC2/RDSの一覧及びインスタンスサイズ、プライベートIPとか、稼働状況とか一覧で欲しい。
(インスタンスサイズでの簡単な見積もりとかしたい時)

AWSのコンソールだとEC2やRDSのインスタンス一覧とか表示はできるが
CSVとかExcelには落とせないのでAWS CLIで一括で出力しました。

今回使用するAWS CLIコマンド

EC2インスタンス情報取得
aws ec2 describe-instances
http://docs.aws.amazon.com/cli/latest/reference/ec2/describe-instances.html

RDSインスタンス情報取得
aws rds describe-db-instances
http://docs.aws.amazon.com/cli/latest/reference/rds/describe-db-instances.html

具体的なコマンド

JSONで結果が吐き出されるので整形のためjqを使いました。
本番環境だとjq入れられなかったりすると思うのでその場合はJSONをまるごと出力してローカルに持ってくるとかしましょう。

EC2のインスタンス一覧出力

`aws ec2 describe-instances --region=ap-northeast-1 --filter "Name=instance-state-name,Values=running" | jq -r '.Reservations[].Instances[]|{PrivateIpAddress, InstanceType, Zone: .Placement.AvailabilityZone, State: .State.Name, KeyName, TagName: .Tags|map(select(.Key == "Name"))[0]|.Value}|@text "\(.PrivateIpAddress)\t\(.TagName)\t\(.InstanceType)\t\(.State)"'

東京リージョンで稼働中のインスタンスの
プライベートIPアドレス、タグ名(インスタンス名)、インスタンスタイプ、稼働状況がタブ区切りで出力されます

10.10.10.111    xxxx-yyyy-1 t2.micro    running
10.10.10.122    xxxx-yyyy-2 r3.large    running
10.20.10.133    xxxx-yyyy-3 r3.large    running
10.20.10.144    xxxx-yyyy-4 c3.large    running

RDSのインスタンス一覧出力

aws rds describe-db-instances --region=ap-northeast-1 |jq -r '.DBInstances[] |{DBInstanceIdentifier,DBInstanceClass,MultiAZ,AllocatedStorage}|@text "\(.DBInstanceIdentifier)\t\(.DBInstanceClass)\t\(.MultiAZ)\t\(.AllocatedStorage)"'

インスタンス名、インスタンスタイプ、MultiAZ使用有無、ストレージサイズ(GB) がタブ区切りで出力されます。

xxx-rds1-replica    db.t2.medium    false   20
xxx-rds1    db.m3.medium    true    20
xxx-rds2    db.t2.micro true    5

上記以外のパラーメータが必要ならよしなにやってください