25
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

AWS CLIを使ってEC2インスタンス一覧を取得する

Last updated at Posted at 2019-11-03

事前準備

  1. クライアントにAWS CLIを設定
    AWS CLIの設定方法はこちら
    「aws configure」で必要なアクセスキーの作成はこちら

取得項目

  • InstanceId: EC2インスタンスID
  • PrivateIp: プライベートIP。複数ネットワークインターフェース(以降、NIC)付与していてもカンマ区切りで一覧表示される。
    (※セカンダリIPアドレスは表示されません)
  • GlobalIP: パブリック IP。複数NIC付与していてもカンマ区切りで一覧表示される。
  • Platform: OS。Linuxの場合は『None』、windowsの場合は『windows』と表示される。
  • State: ステータス。
  • SecurityGroupId: セキュリティグループID一覧。複数個付与していてもカンマ区切りで一覧表示される。
  • Name: 『Name』タグの値を表示。

EC2インスタンス一覧を取得するコマンド

全EC2インスタンスの取得

全EC2インスタンスの取得
$ aws ec2 describe-instances --output=table --query 'Reservations[].Instances[].{InstanceId: InstanceId, PrivateIp: join(`, `, NetworkInterfaces[].PrivateIpAddress), GlobalIP: join(`, `, NetworkInterfaces[].Association.PublicIp), Platform:Platform, State: State.Name, SecurityGroupId: join(`, `, SecurityGroups[].GroupId) ,Name: Tags[?Key==`Name`].Value|[0]}'
例)全EC2インスタンスの取得の結果
----------------------------------------------------------------------------------------------------------------------------------------------
|                                                              DescribeInstances                                                             |
+----------------+----------------------+-------+-----------+------------------------------+-------------------------------------+-----------+
|    GlobalIP    |     InstanceId       | Name  | Platform  |          PrivateIp           |           SecurityGroupId           |   State   |
+----------------+----------------------+-------+-----------+------------------------------+-------------------------------------+-----------+
|  52.0.0.10     |  i-0123456789abcdef1 |  ex1  |  None     |  172.31.10.10, 172.31.0.10   |  sg-01234567, sg-1123456789abcdef1  |  running  |
|  52.0.0.11     |  i-1123456789abcdef1 |  ex2  |  None     |  172.31.10.11, 172.31.0.11   |  sg-01234567, sg-1123456789abcdef1  |  stopped  |
|                |  i-2123456789abcdef1 |  ex3  |  None     |  172.31.1.10                 |  sg-01234567                        |  stopped  |
|                |  i-3123456789abcdef1 |  ex4  |  windows  |  172.31.11.10                |  sg-1123456789abcdef1               |  stopped  |
+----------------+----------------------+-------+-----------+------------------------------+-------------------------------------+-----------+

windowsのEC2インスタンスの取得

windowsのEC2インスタンスの取得
$ aws ec2 describe-instances --output=table --query 'Reservations[].Instances[?Platform==`windows`][].{InstanceId: InstanceId, PrivateIp: join(`, `, NetworkInterfaces[].PrivateIpAddress), GlobalIP: join(`, `, NetworkInterfaces[].Association.PublicIp), Platform:Platform, State: State.Name, SecurityGroupId: join(`, `, SecurityGroups[].GroupId) ,Name: Tags[?Key==`Name`].Value|[0]}'
例)windowsのEC2インスタンスの取得の結果
-------------------------------------------------------------------------------------------------------------
|                                             DescribeInstances                                             |
+----------+----------------------+-------+-----------+----------------+------------------------+-----------+
| GlobalIP |     InstanceId       | Name  | Platform  |   PrivateIp    |    SecurityGroupId     |   State   |
+----------+----------------------+-------+-----------+----------------+------------------------+-----------+
|          |  i-3123456789abcdef1 |  ex4  |  windows  |  172.31.11.10  |  sg-1123456789abcdef1  |  stopped  |
+----------+----------------------+-------+-----------+----------------+------------------------+-----------+

LinuxのEC2インスタンスの取得

LinuxのEC2インスタンスの取得
aws ec2 describe-instances --output=table --query 'Reservations[].Instances[?Platform!=`windows`][].{InstanceId: InstanceId, PrivateIp: join(`, `, NetworkInterfaces[].PrivateIpAddress), GlobalIP: join(`, `, NetworkInterfaces[].Association.PublicIp), Platform:Platform, State: State.Name, SecurityGroupId: join(`, `, SecurityGroups[].GroupId) ,Name: Tags[?Key==`Name`].Value|[0]}'
例)LinuxのEC2インスタンスの取得の結果
----------------------------------------------------------------------------------------------------------------------------------------------
|                                                              DescribeInstances                                                             |
+----------------+----------------------+-------+-----------+------------------------------+-------------------------------------+-----------+
|    GlobalIP    |     InstanceId       | Name  | Platform  |          PrivateIp           |           SecurityGroupId           |   State   |
+----------------+----------------------+-------+-----------+------------------------------+-------------------------------------+-----------+
|  52.0.0.10     |  i-0123456789abcdef1 |  ex1  |  None     |  172.31.10.10, 172.31.0.10   |  sg-01234567, sg-1123456789abcdef1  |  running  |
|  52.0.0.11     |  i-1123456789abcdef1 |  ex2  |  None     |  172.31.10.11, 172.31.0.11   |  sg-01234567, sg-1123456789abcdef1  |  stopped  |
|                |  i-2123456789abcdef1 |  ex3  |  None     |  172.31.1.10                 |  sg-01234567                        |  stopped  |
+----------------+----------------------+-------+-----------+------------------------------+-------------------------------------+-----------+

タグ検索してEC2インスタンスを取得

例)タグ『product:sample』の場合
aws ec2 describe-instances --output=table --query 'Reservations[].Instances[?Tags[?Key==`product`].Value|[0]==`sample`][].{InstanceId: InstanceId, PrivateIp: join(`, `, NetworkInterfaces[].PrivateIpAddress), GlobalIP: join(`, `, NetworkInterfaces[].Association.PublicIp), Platform:Platform, State: State.Name, SecurityGroupId: join(`, `, SecurityGroups[].GroupId) ,Name: Tags[?Key==`Name`].Value|[0]}'
例)タグ『product:sample』の場合の結果
----------------------------------------------------------------------------------------------------------------------------------------------
|                                                              DescribeInstances                                                             |
+----------------+----------------------+-------+-----------+------------------------------+-------------------------------------+-----------+
|    GlobalIP    |     InstanceId       | Name  | Platform  |          PrivateIp           |           SecurityGroupId           |   State   |
+----------------+----------------------+-------+-----------+------------------------------+-------------------------------------+-----------+
|  52.0.0.10     |  i-0123456789abcdef1 |  ex1  |  None     |  172.31.10.10, 172.31.0.10   |  sg-01234567, sg-1123456789abcdef1  |  running  |
|  52.0.0.11     |  i-1123456789abcdef1 |  ex2  |  None     |  172.31.10.11, 172.31.0.11   |  sg-01234567, sg-1123456789abcdef1  |  stopped  |
+----------------+----------------------+-------+-----------+------------------------------+-------------------------------------+-----------+

参考資料

25
15
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
25
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?