【问题标题】:aws cli query multiple attributes when these attributes are on the same level当这些属性在同一级别时,aws cli 查询多个属性
【发布时间】:2021-02-27 23:43:36
【问题描述】:

我正在使用following 查找有关与特定安全组关联的实例的信息

aws ec2 describe-network-interfaces --filters Name=group-id,Values=sg-123456 --output json

返回(部分输出)

{
    "NetworkInterfaces": [
        {
            "Attachment": {
                "AttachTime": "2019-10-09T07:15:44+00:00",
                "AttachmentId": "eni-attach-01234567",
                "InstanceId": "i-12345678",
                "InstanceOwnerId": "123456789",
                "Status": "attached"
            },
            "AvailabilityZone": "us-east-1c",
            "Description": "Primary network interface",
            "Groups": [
                {
                    "GroupName": "sg-number1",
                    "GroupId": "sg-123456"
                },
                {
                    "GroupName": "sg-number_2",
                    "GroupId": "sg-654321"
                }
            ],

如果我只想获取实例 ID,那么我可以使用 --query 并沿着树向下走

--query 'NetworkInterfaces[*].Attachment.InstanceId'

我也可以做类似的事情来获得所有的SG

--query 'NetworkInterfaces[*].Groups[*].GroupId'

我的问题是如何在一个查询中获取 InstanceID 和 Group.GroupName ?

或者更好的是,当这些属性在同一级别时,我如何查询多个属性?

以下操作无效:

--query 'NetworkInterfaces[*].Attachment.InstanceId','NetworkInterfaces[*].Groups[*].GroupName'

【问题讨论】:

  • 我承认我可能没有使用正确的 json/JMESPath 术语,并且可以编辑问题和标题以提高清晰度。我只是不知道如何表达它。

标签: amazon-web-services amazon-ec2 aws-cli aws-security-group jmespath


【解决方案1】:

当然可以,您必须使用示例中 JMESPath 所描述的 filters and multiselect hashes

有了这个,你可以重新创建一个对象,查询沿着树向下。

在您的情况下,使用查询:

NetworkInterfaces[*].{ InstanceId: Attachment.InstanceId, GroupNames: Groups[*].GroupName }

以及 JSON 数据:

{
    "NetworkInterfaces": [
        {
            "Attachment": {
                "AttachTime": "2019-10-09T07:15:44+00:00",
                "AttachmentId": "eni-attach-01234567",
                "InstanceId": "i-12345678",
                "InstanceOwnerId": "123456789",
                "Status": "attached"
            },
            "AvailabilityZone": "us-east-1c",
            "Description": "Primary network interface",
            "Groups": [
                {
                    "GroupName": "sg-number1",
                    "GroupId": "sg-123456"
                },
                {
                    "GroupName": "sg-number_2",
                    "GroupId": "sg-654321"
                }
            ]
        }
    ]
}

它给出:

[
  {
    "InstanceId": "i-12345678",
    "GroupNames": [
      "sg-number1",
      "sg-number_2"
    ]
  }
]

所以你的命令最终是:

aws ec2 describe-network-interfaces --filters Name=group-id,Values=sg-123456 --output json --query 'NetworkInterfaces[*].{ InstanceId: Attachment.InstanceId, GroupNames: Groups[*].GroupName }'

【讨论】:

  • 没错!像宣传的那样工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-15
  • 1970-01-01
  • 1970-01-01
  • 2021-09-17
  • 2020-07-16
相关资源
最近更新 更多