【AWS】AWS CLI ~ --query / JMESPath ~

■ はじめに

https://dk521123.hatenablog.com/entry/2024/01/14/005148

からの分冊。

AWS CLI には、--query ってのがあって、
~~~~
--query (string)
A JMESPath query to use in filtering the response data.
~~~~
ってなっていたので、「JMESPath」ってのを調べてみた
 => 以下の「【2】サンプル」から分かる通り、ある程度、
  使いこなせた方が便利そう

目次

【1】JMESPath
【2】サンプル
 例1:aws codeartifact list-repositories-in-domain
 例2:describe-image-scan-findings
【3】学び方・動作確認
 例1:describe-image-scan-findings の結果

【1】JMESPath

* JSON用のクエリ言語
* Jamesさんが作ったから JMES Path

【2】サンプル

* AWS 公式ドキュメントのサンプルは以下。

https://docs.aws.amazon.com/ja_jp/cli/latest/userguide/cli-usage-filter.html#cli-usage-filter-client-side

例1:aws codeartifact list-repositories-in-domain

aws codeartifact list-repositories-in-domain \
  --domain my_domain --domain-owner 111122223333 \
  --query 'repositories[*].[name,administratorAccount]' \
  --output text

Output (--queryなし)

{
  "repositories": [
    {
      "name": "repo1",
      "administratorAccount": "123456789012",
      "domainName": "my_domain",
      "domainOwner": "111122223333",  
      "arn": "arn:aws:codeartifact:region-id:111122223333:repository/my_domain/repo1",
      "description": "Description of repo1"
    },
    {
      "name": "repo2",
      "administratorAccount": "444455556666",
      "domainName": "my_domain",
      "domainOwner": "111122223333",
      "arn": "arn:aws:codeartifact:region-id:111122223333:repository/my_domain/repo2",
      "description": "Description of repo2"
    },
    {
      "name": "repo3",
      "administratorAccount": "444455556666",
      "domainName": "my_domain",
      "domainOwner": "111122223333",
      "arn": "arn:aws:codeartifact:region-id:111122223333:repository/my_domain/repo3",
      "description": "Description of repo3"
    }
  ]
}

Output (--queryあり)

repo1 710221105108
repo2 710221105108
repo3 532996949307

【3】学び方・動作確認

* 以下の公式サイトのTutorial で
 動かしながら学んでいくのが良さそう。

https://jmespath.org/tutorial.html

例1:describe-image-scan-findings の結果

* 以下を公式ドキュメントのサンプル出力を少し改変
 => 残念ながらKeyでフィルタリング(例えば、以下の例だとCRITICAL/HIGHでフィルタなど)
  はできないっぽい

https://docs.aws.amazon.com/cli/latest/reference/ecr/describe-image-scan-findings.html

{
    "imageScanFindings": {
      "imageScanCompletedAt": 1579839105.0,
      "vulnerabilitySourceUpdatedAt": 1579811117.0,
      "findingSeverityCounts": {
          "CRITICAL": 1,
          "HIGH": 2,
          "MEDIUM": 3,
          "LOW": 4,
          "UNTRIGED": 5
      }
  },
  "registryId": "123456789012",
  "repositoryName": "sample-repo",
  "imageId": {
      "imageDigest": "sha256:74b2c688c700ec95a93e478cdb959737c148df3fbf5ea706abe0318726e885e6"
  },
  "imageScanStatus": {
      "status": "COMPLETE",
      "description": "The scan was completed successfully."
  }
}

Case1:imageScanFindings.findingSeverityCounts

{
  "CRITICAL": 1,
  "HIGH": 2,
  "MEDIUM": 3,
  "LOW": 4,
  "UNTRIGED": 5
}

Case2:imageScanFindings.findingSeverityCounts.HIGH

2

関連記事

AWS CodeArtifact ~ AWS CLI
https://dk521123.hatenablog.com/entry/2024/01/14/005148
Amazon ECR ~ AWS CLI
https://dk521123.hatenablog.com/entry/2024/01/05/000000
jq コマンド ~ JSON を扱う ~
https://dk521123.hatenablog.com/entry/2020/02/01/000000