【小ネタ】AWS CLIのqueryオプション(JMESPath)でstringな数値をfilterしたい

2017.02.28

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

西澤です。だいぶ小ネタですが、備忘録です。やりたいことは件名の通りですが、正解にたどり着くまでの過程も残しておこうと思います。

AWS CLIでやりたかったこと

SSMについて調査をする為に、下記のようなコマンドで下調べをしていました。

$ aws ssm list-documents \
  --query "DocumentIdentifiers[?Owner==\`Amazon\`].[Name,SchemaVersion]" \
  --max-results 25 \
  --output text
AWS-ApplyPatchBaseline	1.2
AWS-ConfigureAWSPackage	2.0
AWS-ConfigureCloudWatch	1.2
AWS-ConfigureDocker	2.0
AWS-ConfigureWindowsUpdate	1.2
AWS-FindWindowsUpdates	1.2
AWS-GatherSoftwareInventory	2.0
AWS-InstallApplication	1.2
AWS-InstallMissingWindowsUpdates	1.2
AWS-InstallPowerShellModule	1.2
AWS-InstallSpecificWindowsUpdates	1.2
AWS-InstallWindowsUpdates	1.2
AWS-JoinDirectoryServiceDomain	1.2
AWS-ListWindowsInventory	1.2
AWS-RefreshAssociation	2.0
AWS-RunDockerAction	2.0
AWS-RunPowerShellScript	1.2
AWS-RunShellScript	1.2
AWS-UpdateEC2Config	1.2
AWS-UpdateLinuxAmi	0.3
AWS-UpdateSSMAgent	1.2

SSMドキュメントには、SchemaVersionというのがあるらしい、と。それでは、何が違うのかをもう少し調べる為に、SchemaVersionごとに一覧を取得しようとしました。

最初に試したコマンド(失敗編)

最初はこんなコマンドでフィルターしようとしたのですが、何故か思ったような結果を取得することができませんでした。

$ aws ssm list-documents \
  --query "DocumentIdentifiers[?Owner==\`Amazon\` && SchemaVersion==\`2.0\`].[Name,SchemaVersion]" \
  --max-results 25 \
  --output text

試しに、条件をNotEqualにしてみると、全件が出力されました。これは何かがおかしいです。

$ aws ssm list-documents \
  --query "DocumentIdentifiers[?Owner==\`Amazon\` && SchemaVersion!=\`2.0\`].[Name,SchemaVersion]" \
  --max-results 25 \
  --output text
AWS-ApplyPatchBaseline	1.2
AWS-ConfigureAWSPackage	2.0
AWS-ConfigureCloudWatch	1.2
AWS-ConfigureDocker	2.0
AWS-ConfigureWindowsUpdate	1.2
AWS-FindWindowsUpdates	1.2
AWS-GatherSoftwareInventory	2.0
AWS-InstallApplication	1.2
AWS-InstallMissingWindowsUpdates	1.2
AWS-InstallPowerShellModule	1.2
AWS-InstallSpecificWindowsUpdates	1.2
AWS-InstallWindowsUpdates	1.2
AWS-JoinDirectoryServiceDomain	1.2
AWS-ListWindowsInventory	1.2
AWS-RefreshAssociation	2.0
AWS-RunDockerAction	2.0
AWS-RunPowerShellScript	1.2
AWS-RunShellScript	1.2
AWS-UpdateEC2Config	1.2
AWS-UpdateLinuxAmi	0.3
AWS-UpdateSSMAgent	1.2

Nameでは想定通りにフィルターできていますので、何か想定と異なることが起きています。

$ aws ssm list-documents \
  --query "DocumentIdentifiers[?Owner==\`Amazon\` && Name==\`AWS-ApplyPatchBaseline\`].[Name,SchemaVersion]" \
  --max-results 25 \
  --output text
AWS-ApplyPatchBaseline	1.2

型を確認してみた

数字なのが怪しいと思いました。ただし、確認してみてもどちらも同じstringなのでした。

$ aws ssm list-documents \
  --query "DocumentIdentifiers[?Owner==\`Amazon\`]|[0].[type(Name),type(SchemaVersion)]"
[
    "string",
    "string"
]

型を無理矢理変換してみた

SchemaVersionを無理矢理numberに変換してみることにしてみると、、、なぜか目的としていた結果を取得することができました。

$ aws ssm list-documents \
  --query "DocumentIdentifiers[?Owner==\`Amazon\` && to_number(SchemaVersion)==\`2.0\`].[Name,SchemaVersion]" \
  --max-results 25 \
  --output text
AWS-ConfigureAWSPackage	2.0
AWS-ConfigureDocker	2.0
AWS-GatherSoftwareInventory	2.0
AWS-RefreshAssociation	2.0
AWS-RunDockerAction	2.0

要件はクリアできたのですが、すっかり訳がわからなくなってしまいました。

正解はこちら

評価したい等式の右辺が意図せず数値で解釈されていたことに気付きました。なので、こちらが適切なのではないかと思います。

$ aws ssm list-documents \
>   --query "DocumentIdentifiers[?Owner==\`Amazon\` && SchemaVersion==to_string(\`2.0\`)].[Name,SchemaVersion]" \
>   --max-results 25 \
>   --output text
AWS-ConfigureAWSPackage	2.0
AWS-ConfigureDocker	2.0
AWS-GatherSoftwareInventory	2.0
AWS-RefreshAssociation	2.0
AWS-RunDockerAction	2.0

まとめ

jq使った方が良いのかもしれないですが、意地になって調べてみました。同じようなことをしている方が果たしているのか疑問ですが。本来調べたかったSSMについては、またの機会にします。

どこかの誰かのお役に立てば嬉しいです。