2015-10-29 80 views
1

我想运行一个EC2描述-​​instances命令,并以表的形式得到输出如下(其中名称是标签与重点“名称”的值):AWS CLI EC2描述-​​情况表输出

---------------------------------------------------------- 
|     DescribeInstances     | 
+-------------+----------------+--------------+----------+ 
| instance_id | ip_address | name  | state | 
+-------------+----------------+--------------+----------+ 
| i-g93g494d | 99.99.99.01 | name1  | running | 
| i-a93f754c | 99.99.99.02 | name2  | running | 
+-------------+----------------+--------------+----------+ 

我可以运行下面的命令:

aws ec2 describe-instances --instance-ids i-g93g494d i-a93f754c --query "Reservations[*].Instances[*].{name: Tags[?Key=='Name'].Value, instance_id: InstanceId, ip_address: PrivateIpAddress, state: State.Name}" --output json 

,将获得的输出:

[ 
    [ 
     { 
      "instance_id": "i-g93g494d", 
      "state": "running", 
      "ip_address": "99.99.99.01", 
      "name": [ 
       "name1" 
      ] 
     } 
    ], 
    [ 
     { 
      "instance_id": "i-a93f754c", 
      "state": "running", 
      "ip_address": "99.99.99.02", 
      "name": [ 
       "name2" 
      ] 
     } 
    ] 
] 

然而,当我运行第与--output表相同的命令,而不是 - 输出json我得到一个错误。

命令:

aws ec2 describe-instances --instance-ids i-g93g494d i-a93f754c --query "Reservations[*].Instances[*].{name: Tags[?Key=='Name'].Value, instance_id: InstanceId, ip_address: PrivateIpAddress, state: State.Name}" --output json 

输出:

list index out of range 

我想表输出看起来像上面的例子,但我有困难,解决这个。我非常感谢任何人都可以提供这方面的帮助。

回答

6

您需要使用管式过滤标签结果和得到的第一个值,如:

aws ec2 describe-instances --instance-ids i-g93g494d i-a93f754c --query "Reservations[*].Instances[*].{name: Tags[?Key=='Name'] | [0].Value, instance_id: InstanceId, ip_address: PrivateIpAddress, state: State.Name}" --output table 

有一个很好的相关的博客帖子在这里:Get a list of instance with id, name and type

+0

谢谢!完美的作品,这正是我所追求的。我曾尝试使用“名称:标签[?Key =='Name'] | []”来扁平化管道,我认为它应该起作用(因此问题),但是我没有尝试过“name:Tags [?Key == '名称'] | [0]“ – Stuart