【ふりかえり】quelyとfilter

# はじめに
`--quely`と`--filter`の基本的な部分を備忘録の意味も込めて、アウトプット資料としてまとめます。

# quely
* 表示するvalueを絞る

* `aws ec2 describe-instances`を実行した際の出力結果例
```

    "Reservations": [
        {
            "Instances": [
                {
                    "InstanceId": "i-xxxxxxxx",
                    "InstanceType": "t3.micro",
                    "State": {
                        "Name": "stopped"
                    },
                }
            ],
        },
        {
            "Instances": [
                {
                    "InstanceId": "i-xxxxxxxx",
                    "InstanceType": "t3.micro",
                    "State": {
                        "Name": "stopped"
                    },
                }
            ]
        }
    ]
}
```
`InstanceId`だけを出力させたいとき
```
aws ec2 describe-instances 
--query 'Reservations[].Instances[].InstanceId'
```
* `Reservations`や`Instances`の中身は[]で囲われているので`Reservation[]`と入力する
* `{ }`で囲われている部分は` . `と入力する

実行結果の例は以下の通り
```
[
   "i-xxxxxxxx",
   "i-xxxxxxxx"
]

```
## 複数の項目を知りたいとき
* `[ ] `で囲んだ後に、` , `で区切る

```
--query 'Reservations[].Instances[].[InstanceId,InstanceType]'
```
出力される結果
```
[
    "i-xxxxxxxx",
    "t3.micro"
],
[
    "i-xxxxxxxx",
    "t3.micro"
],
```

# filter
* 表示する要素を絞る
* `Name=キー,Values=値`と入力する


こちらの記事がとても分かりやすかったので参考にしています。
 [【参考】AWS CLIのフィルターとクエリーの使い方についてまとめてみた](https://dev.classmethod.jp/articles/aws-cli-filter-and-query-howto/#toc-4)

## ためしてみる
`ResourceId`で絞る場合
```
aws ec2 describe-tags 
--filters "Name=resource-id,Values=instance"
```
* Nameの値がResourceIdではなく`resource-id`となり、**結果に表示されているものとは違う**
※出力結果の例はこちら
```
{
    "Tags": [
        {
            "ResourceType": "instance",
            "ResourceId": "i-xxxxxxxx",
            "Value": "xxxx",
            "Key": "Name"
        },
        ：
        ：
}
```
### フィルターに指定できる文字列の探し方
* 上記のように、Nameの値が結果に表示されているものと異なるとき、、サブコマンドに続けて`help`を入力してfilter指定する文字列を探す。
```
aws ec2 describe-tags help
```
```
DESCRIBE-TAGS()                                                DESCRIBE-TAGS()



NAME
       describe-tags -

DESCRIPTION
       Describes the specified tags for your EC2 resources.

       For  more  information about tags, see Tag your Amazon EC2 resources in
       the Amazon Elastic Compute Cloud User Guide .

       See also: AWS API Documentation

       describe-tags is a paginated  operation.  Multiple  API  calls  may  be
       issued  in  order  to  retrieve the entire data set of results. You can
       disable pagination by providing the --no-paginate argument.  When using
       --output  text  and  the  --query argument on a paginated response, the
       --query argument must extract data from the results  of  the  following
       query expressions: Tags

SYNOPSIS
            describe-tags
          [--dry-run | --no-dry-run]
          [--filters <value>]
          [--cli-input-json | --cli-input-yaml]
          [--starting-token <value>]
          [--page-size <value>]
          [--max-items <value>]
          [--generate-cli-skeleton <value>]
          [--debug]
          [--endpoint-url <value>]
          [--no-verify-ssl]
          [--no-paginate]
          [--output <value>]
          [--query <value>]
          [--profile <value>]
          [--region <value>]
          [--version <value>]
          [--color <value>]
          [--no-sign-request]
          [--ca-bundle <value>]
          [--cli-read-timeout <value>]
          [--cli-connect-timeout <value>]
          [--cli-binary-format <value>]
          [--no-cli-pager]
          [--cli-auto-prompt]
          [--no-cli-auto-prompt]

OPTIONS
       --dry-run | --no-dry-run (boolean)
          Checks  whether  you  have  the required permissions for the action,
          without actually making the request, and provides an error response.
          If  you have the required permissions, the error response is DryRun-
          Operation . Otherwise, it is UnauthorizedOperation .

:
```
/--filtersでこのあたりから読み取れそう
```
OPTIONS
       --dry-run | --no-dry-run (boolean)
          Checks  whether  you  have  the required permissions for the action,
          without actually making the request, and provides an error response.
          If  you have the required permissions, the error response is DryRun-
          Operation . Otherwise, it is UnauthorizedOperation .

       --filters (list)
          The filters.

          o key - The tag key.

          o resource-id - The ID of the resource.

          o resource-type  -  The  resource  type  (customer-gateway  |  dedi-
            cated-host  |  dhcp-options  |  elastic-ip  | fleet | fpga-image |
            host-reservation | image | instance | internet-gateway |  key-pair
            | launch-template | natgateway | network-acl | network-interface |
            placement-group  |  reserved-instances  |  route-table   |   secu-
            rity-group | snapshot | spot-instances-request | subnet | volume |
            vpc | vpc-endpoint | vpc-endpoint-service | vpc-peering-connection
            | vpn-connection | vpn-gateway ).

          o tag  :<key>  -  The key/value combination of the tag. For example,
            specify "tag:Owner" for the filter name and "TeamA" for the filter
            value to find resources with the tag "Owner=TeamA".

          o value - The tag value.

          (structure)
              A  filter name and value pair that is used to return a more spe-
              cific list of results from a describe operation. Filters can  be
              used  to  match a set of resources by specific criteria, such as
              tags, attributes, or IDs.

              If you specify multiple filters, the filters are joined with  an
              AND , and the request returns only results that match all of the
              specified filters.

              Name -> (string)
                 The name of the filter. Filter names are case-sensitive.

              Values -> (list)
                 The filter values. Filter values are case-sensitive.  If  you
                 specify  multiple  values for a filter, the values are joined
                 with an OR , and the request returns all results  that  match
                 any of the specified values.

                 (string)

       Shorthand Syntax:

          Name=string,Values=string,string ...

       JSON Syntax:

/--filters
```