aws / aws-cli

Universal Command Line Interface for Amazon Web Services
Other
15.34k stars 4.08k forks source link

`--query` does not work properly for iot device shadow #7679

Open narutaro opened 1 year ago

narutaro commented 1 year ago

Describe the bug

I have a following device shadow for thing 8277faad :

{
  "state": {
    "desired": {
      "welcome": "aws-iot"
    },
    "reported": {
      "welcome": "aws-iot"
    }
  },
  "metadata": {
    "desired": {
      "welcome": {
        "timestamp": 1676682183
      }
    },
    "reported": {
      "welcome": {
        "timestamp": 1676682183
      }
    }
  },
  "version": 1,
  "timestamp": 1676684517
}

I run aws iot-data get-thing-shadow --thing-name 8277faad --query "state" out.json but the filter does not work.

Expected Behavior

The expected return is following contents in the out.json file

{
    "desired": {
      "welcome": "aws-iot"
    },
    "reported": {
      "welcome": "aws-iot"
    }
}

Current Behavior

The command returns null

aws iot-data get-thing-shadow --thing-name 8277faad --query "state" out.json
null

out.json is not filtered by the query. The out.json is:

{
  "state": {
    "desired": {
      "welcome": "aws-iot"
    },
    "reported": {
      "welcome": "aws-iot"
    }
  },
  "metadata": {
    "desired": {
      "welcome": {
        "timestamp": 1676682183
      }
    },
    "reported": {
      "welcome": {
        "timestamp": 1676682183
      }
    }
  },
  "version": 1,
  "timestamp": 1676685039
}

Reproduction Steps

  1. Create a shadow with the above contents.
  2. Run the aws command with query.

Possible Solution

No response

Additional Information/Context

No response

CLI version used

aws-cli/2.10.1 Python/3.9.11 Darwin/21.6.0 exe/x86_64 prompt/off

Environment details (OS name and version, etc.)

Mac

tim-finnigan commented 1 year ago

Hi @narutaro thanks for reaching out. Here is the AWS CLI documentation on client-side filtering for reference: https://docs.aws.amazon.com/cli/latest/userguide/cli-usage-filter.html#cli-usage-filter-client-side

I think what you're describing is the expected behavior, as the GetThingShadow API only returns an HTTP 200 response if successful. For commands that return data you can output the --query filter results like this: aws ec2 describe-instances --query 'Reservations[].Instances[].InstanceId' > instances-ids.json

narutaro commented 1 year ago

Updated the issue title from --filter to --query

Hi @tim-finnigan thank you for your comment. I understand that GetThingShadow API returns payload which is only HTTP state - 200 in my case.

Response Elements If the action is successful, the service sends back an HTTP 200 response. The response returns the following as the HTTP body. payload The state information, in JSON format.

On the other hand, I can get the whole shadow document with the command I wrote before:

aws iot-data get-thing-shadow --thing-name 8277faad --query "state" out.json

Is there a reason that the GetThingShadow API does not return shadow document as return data? (like other APIs do?)

erikfinnman commented 1 year ago

If this is the expected behaviour due to that the API only returns HTTP 2000, should the --query option really be available for this CLI command? At least to me it was very confusing when trying to understand why it didn't do anything. Perhaps at least some kind of error/warning message could be printed to the console instead of only null?

tim-finnigan commented 1 year ago

Thanks for following up and elaborating on the issue. I can see how the behavior is confusing, although I don't think making the --query option available or not depending on the command used would be considered.

Maybe there could be some sort of error message for scenarios like this, but it seems somewhat rare that a command doesn't return anything. If others run into this issue please 👍 the issue and leave a a comment if you have additional information to share.