Search code examples
amazon-rdsaws-clijmespath

How to use JMESPath to query AWS CLI RDS instances by DBInstanceIdentifier


I need a list of RDS DBInstanceIdentifier that match the String "foobar" in their name. I found many solutions with exact match, but not substring matching. My approach looks as follows:

I get a list of all DBInstanceIdentifier using:

aws rds describe-db-instances --query "DBInstances[*].[DBInstanceIdentifier][]" which looks like

[
    "machine-001-alice-abcdefg", 
    "machine-002-bob-abcdefg", 
    "machine-003-foobar-abcdefg"
]

On the list I apply a filter like in the last example of the JMSES Tutorial

aws rds describe-db-instances --query "DBInstances[*].[DBInstanceIdentifier][]|[?contains(@,'dev') =='true']"

If I change the statement to != I get the full list, so it seems I have the filter statement wrong.


Solution

  • true needs to be backticked not quoted and then the backticks need to be escaped it seems - different shells may vary.

    aws rds describe-db-instances --query "DBInstances[*].[DBInstanceIdentifier][]|[?contains(@,'dev')==\`true\`]"
    aws rds describe-db-instances --query "DBInstances[*].[DBInstanceIdentifier][]|[?contains(@,'dev')!=\`true\`]"
    

    You can also omit the comparison to true but I couldn't invert this successfully

    aws --profile pollen-nonprod rds describe-db-instances --query "DBInstances[*].[DBInstanceIdentifier][]|[?contains(@,'dev')]"
    

    (I'd normally do this sort of thing with jq but that's a different solution rather than necessarily a better one)