Search code examples
amazon-web-servicesamazon-rdsaws-cli

How to use the --query and --filters with aws rds describe-db-instances to retrieve the OptionGroupName value of an RDS


I would like to retrieve the DB Option Group (OptionGroupName) for an RDS by using the aws rds describe-db-instances --query and --filters.

Just FYI I can not use jq to filter the json response from aws rds describe-db-instances.


Solution

  • The Output of describe-db-instances looks something like:

    {
        "DBInstances": [
            {
                "PubliclyAccessible": true, 
                "MasterUsername": "bos", 
                "OptionGroupMemberships": [
                    {
                        "Status": "in-sync", 
                        "OptionGroupName": "default:postgres-9-6"
                    }
                ], 
                ...
    

    The OptionGroupMemberships element is a list, so it might contain multiple values. If you are only seeking the value of the first item in the list, you would use:

    --query 'DBInstances[*].OptionGroupMemberships[0].OptionGroupName'
    

    You can combine it with other elements like this:

    --query 'DBInstances[*].[MasterUsername,OptionGroupMemberships[0].OptionGroupName]'
    

    You can also append --output text to obtain the value without JSON formatting.

    If I ever need to experiment with the --query values, I go to JMESPath Tutorial — JMESPath and paste my JSON into the examples. I can then interactively attempt to write the correct query (which is how I derived the above answers).