AWSCLI聚合来自多个cli调用的结果

时间:2018-03-15 18:40:21

标签: json amazon-web-services aws-cli

我在Windows上使用AWSCLI通过RDP启动,查询和连接实例。我想在一个查询中返回三个值,但我无法弄清楚如何做到这一点。

你能证明怎么做吗?

以下是三个查询中返回的三个值:

aws ec2 describe-instance-status --instance-id i-xxxxxxxx --query InstanceStatuses[*].InstanceState.Code
aws ec2 describe-instance-status --instance-id i-xxxxxxxx --query InstanceStatuses[*].SystemStatus.Status
aws ec2 describe-instance-status --instance-id i-xxxxxxxx --query InstanceStatuses[*].InstanceStatus.Status

输出如下:

{
"InstanceStatuses": [
    {
        "InstanceId": "i-1234567890abcdef0",
        "InstanceState": {
            "Code": 16,
            "Name": "running"
        },
        "AvailabilityZone": "us-east-1d",
        "SystemStatus": {
            "Status": "ok",
            "Details": [
                {
                    "Status": "passed",
                    "Name": "reachability"
                }
            ]
        },
        "InstanceStatus": {
            "Status": "ok",
            "Details": [
                {
                    "Status": "passed",
                    "Name": "reachability"
                }
            ]
        }
    }
]
}

并在此处记录:describe-instance-status

由于

1 个答案:

答案 0 :(得分:2)

您可以在--query参数中请求多个属性。例如:

aws ec2 describe-instance-status \
    --instance-id i-01122233344455566 \
    --query "InstanceStatuses[0].{code:InstanceState.Code,sstatus:SystemStatus.Status,istatus:InstanceStatus.Status}"

这将导致类似:

{
   "istatus": "ok",
   "code": 16,
   "sstatus": "ok"
}

请注意以下更改:

  • 检索InstanceStatuses [0]而不是InstanceStatuses [*]
  • 大括号内请求的多个值
  • 使用别名istatus,code,sstatus(随意更改这些)