0

I ran the AWS-RunPatchBaseline run command and few of my instance are successful and few of them are timed out. I want to filter the instance that were timed out using the aws cli list-command-inovcations command.

When I ran the below CLI command:

aws ssm list-command-invocations --command-id 7894b7658-a156-4e5g-97t2-2a9ab5498e1d

It displays a ouput attached here

enter image description here

Next, from the above output, I want to filter all the instance that have the "Status": "Timedout", "StatusDetails": "DeliveryTimedOut" (or, actually, everything other than "Status": "Success")

I tried:

aws ssm list-command-invocations --command-id 7894b7658-a156-4e5g-97t2-2a9ab5498e1d --output text  --query '@[?(CommandInvocations.Status != 'Success')]'

it is returning None.

I also tried

aws ssm list-command-invocations --command-id 7894b7658-a156-4e5g-97t2-2a9ab5498e1d --output text  --query '@[?(@.Status != 'Success')]'

which is returning None, as too.

And, with

aws ssm list-command-invocations --command-id 7894b7658-a156-4e5g-97t2-2a9ab5498e1d --output text  --query 'CommandInvocations[?(@.Status != 'Success')]' 

it is not filtered, returning the complete output.

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
  • Please have a look at [ask], especially the part reading _"**DO NOT post images of code, data, error messages, etc.** — copy or type the text into the question. Please reserve the use of images for diagrams or demonstrating rendering bugs, things that are impossible to describe accurately via text. For more information please see the Meta FAQ entry [Why not upload images of code/errors when asking a question?](https://meta.stackoverflow.com/a/285557)"_. – β.εηοιτ.βε Jan 27 '23 at 13:49

1 Answers1

0

Since you did not provide an example of output one can copy / paste for testing purpose, this example is based on the output from the AWS documentation, where I changed the Status of the command of ID ef7fdfd8-9b57-4151-a15c-db9a12345678, which I also cleaned a bit from the excess data, so:

{
    "CommandInvocations": [
        {
            "CommandId": "ef7fdfd8-9b57-4151-a15c-db9a12345678",
            "InstanceId": "i-02573cafcfEXAMPLE",
            "InstanceName": "",
            "DocumentName": "AWS-UpdateSSMAgent",
            "DocumentVersion": "",
            "RequestedDateTime": 1582136283.089,
            "Status": "TimedOut",
            "StatusDetails": "DeliveryTimeOut"
        },
        {
            "CommandId": "ef7fdfd8-9b57-4151-a15c-db9a12345678",
            "InstanceId": "i-0471e04240EXAMPLE",
            "InstanceName": "",
            "DocumentName": "AWS-UpdateSSMAgent",
            "DocumentVersion": "",
            "RequestedDateTime": 1582136283.02,
            "Status": "Success",
            "StatusDetails": "Success"
        }
    ]
}

Given this JSON, the filter to apply is quite like the one you can find in the tutorial chapter "Filter Projections".

You just need to select the property under where the array is, in your case, CommandInvocations, and apply your condition, Status != `Success`, inside the brackets [? ].

So, with the query:

CommandInvocations[?Status != `Success`]

On the above JSON, we end up with the expected:

[
  {
    "CommandId": "ef7fdfd8-9b57-4151-a15c-db9a12345678",
    "InstanceId": "i-02573cafcfEXAMPLE",
    "InstanceName": "",
    "DocumentName": "AWS-UpdateSSMAgent",
    "DocumentVersion": "",
    "RequestedDateTime": 1582136283.089,
    "Status": "TimedOut",
    "StatusDetails": "DeliveryTimeOut"
  }
]

And, so, your AWS command should be:

aws ssm list-command-invocations \
  --command-id 7894b7658-a156-4e5g-97t2-2a9ab5498e1d \
  --output text \
  --query 'CommandInvocations[?Status != `Success`]' 
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83