aws-clijmespath

How do you use aws cli --query to filter items using JMESPath functions?


I've been reading the documentation at https://docs.aws.amazon.com/cli/v1/userguide/cli-usage-filter.html and https://jmespath.org/specification.html but it doesn't work.

For example, I want to use starts_with to find db instances whose DBInstances.DBInstanceIdentifier starts with "foo".

aws rds describe-db-instances --query 'DBInstances.DBInstanceIdentifier[?starts_with(@, "foo")]'
null
---
aws rds describe-db-instances --query 'DBInstances[?starts_with("DBInstanceIdentifier", "foo")]'

In function starts_with(), invalid type for value: None, expected one of: ['string'], received: "null"
---
aws rds describe-db-instances --query 'DBInstances[?starts_with(DBInstanceIdentifier, "foo")]'

In function starts_with(), invalid type for value: None, expected one of: ['string'], received: "null"

I would also like to use functions like contains etc to do other things but none of the functions seem to work.

I am aware I can just pass the output through jq and it's an easy solution, just trying to understand more tools

aws rds describe-db-instances | jq '.DBInstances[] | select(.DBInstanceIdentifier | startswith("foo"))'

Solution

  • You can use:

    aws rds describe-db-instances --query 'DBInstances[?starts_with(DBInstanceIdentifier, `foo`)]'
    

    Note that the value is surrounded by back-ticks and the starts_with is on the higher-level field.

    This works on a Mac. You might need to use different quotation marks on Windows.