AWS: retrieving standalone database instances by filtering out those with cluster membership

The AWS CLI provides a way to query for DB instances using “aws rds describe-db-instances“, but this will return all DB instances including those that are members of a cluster.

If we want to query for just the standalone DB instances (not part of a cluster), here are a couple of options.

You can use the JMESPath capabilities of the aws CLI tool to check for an empty ‘DBClusterIdentifier’ element.

aws rds describe-db-instances --no-cli-pager --query "DBInstances[?DBClusterIdentifier==null].DBInstanceIdentifier" --output text

Or you can fetch the total results, then use jq to select only those entries where ‘DBClusterIdentifier’ does not exist.

aws rds describe-db-instances --no-cli-pager | jq '.DBInstances[] | select(has("DBClusterIdentifier")|not).DBInstanceIdentifier' -r

 

Here is a link to the full script, aws_query_db_instances_for_standalone.sh

 

REFERENCES

aws rds describe-db-instances reference

jq tool