How to Check if an AWS RDS Instance Exists Using CLI Without Error Messages


2 views

When checking for RDS instance existence using the AWS CLI, many developers encounter the frustrating DBInstanceNotFound error. The standard approach fails because AWS CLI treats missing resources as errors rather than returning empty results.

The proper way to check for RDS instance existence while handling the error case gracefully is to:

#!/bin/bash

DB_INSTANCE_ID="my-database"

# Use query filtering and error suppression
aws rds describe-db-instances \
    --query "DBInstances[?DBInstanceIdentifier=='${DB_INSTANCE_ID}'].[DBInstanceIdentifier]" \
    --output text \
    2>/dev/null

if [ $? -eq 0 ]; then
    echo "Instance exists"
else
    echo "Instance does not exist"
fi

Method 1: Using jq for JSON Processing

EXISTS=$(aws rds describe-db-instances \
         --query "DBInstances[*].DBInstanceIdentifier" \
         --output json | jq -e ".[] | select(. == \"$DBINSTANCEIDENTIFIER\")" >/dev/null; echo $?)
         
if [ "$EXISTS" -eq 0 ]; then
    echo "Found"
fi

Method 2: List All Instances First

INSTANCES=$(aws rds describe-db-instances --query "DBInstances[*].DBInstanceIdentifier" --output text)

if [[ $INSTANCES =~ (^|[[:space:]])$DBINSTANCEIDENTIFIER($|[[:space:]]) ]]; then
    echo "Found"
fi

For production scripts, consider these improvements:

#!/bin/bash

check_rds_instance() {
    local instance_id=$1
    local result
    
    result=$(aws rds describe-db-instances \
        --db-instance-identifier "$instance_id" \
        --query "DBInstances[0].DBInstanceIdentifier" \
        2>&1)
    
    case $? in
        0) return 0 ;; # Found
        255) 
            if [[ $result == *"DBInstanceNotFound"* ]]; then
                return 1 # Not found
            else
                return 2 # Other error
            fi ;;
        *) return 3 # Unknown error
    esac
}

For environments with many RDS instances, filtering server-side is more efficient:

aws rds describe-db-instances \
    --query "length(DBInstances[?DBInstanceIdentifier=='${DB_INSTANCE_ID}'])"

This returns a count (0 or 1) without transferring full instance details.


When scripting AWS infrastructure management, we often need to verify if an RDS instance exists before taking actions like creation or deletion. The naive approach using describe-db-instances fails ungracefully:

# This throws DBInstanceNotFound error for non-existent instances
aws rds describe-db-instances --db-instance-identifier="nonexistent-db"

Method 1: Using Query Filtering

The cleanest approach leverages JMESPath queries to handle missing instances gracefully:

#!/bin/bash

DB_INSTANCE="greatdb"
EXISTS=$(aws rds describe-db-instances \
    --query 'DBInstances[?DBInstanceIdentifier=='"$DB_INSTANCE"'].DBInstanceIdentifier' \
    --output text)

if [ -z "$EXISTS" ]; then
    echo "Instance does not exist"
else
    echo "Instance exists: $EXISTS"
fi

Method 2: Error Suppression with Status Check

For older AWS CLI versions, we can redirect stderr and check the command status:

aws rds describe-db-instances \
    --db-instance-identifier="$DB_INSTANCE" \
    >/dev/null 2>&1

if [ $? -eq 0 ]; then
    echo "Instance exists"
else
    echo "Instance does not exist (or other error occurred)"
fi

Handling Multiple Regions

For cross-region checks, always specify the region explicitly:

REGIONS="us-east-1 us-west-2 eu-west-1"
TARGET_INSTANCE="prod-database"

for region in $REGIONS; do
    echo "Checking $region..."
    aws rds describe-db-instances \
        --region $region \
        --query 'contains(DBInstances[*].DBInstanceIdentifier, '"$TARGET_INSTANCE"')' \
        --output text
done

Performance Optimization

For frequent checks in large environments, use filters to minimize API response size:

aws rds describe-db-instances \
    --query 'DBInstances[?DBInstanceIdentifier=='"$DB_INSTANCE"'].{ID:DBInstanceIdentifier, Status:DBInstanceStatus}' \
    --output json
  • Always add --region parameter in scripts
  • Consider implementing exponential backoff for automation scripts
  • Use AWS CLI v2 for improved JMESPath query support
  • Cache results when possible to avoid rate limits