Recently there was a requirement to check the existence of an RDS instance in AWS account using AWS CLI. The script will be executed in the user-data of the EC2 bootstrap script.

The following shell script performs the same activity using AWS CLI. We just need to pass the DB Instance identifier to this script and this script will search for the specific RDS instance.

#!/usr/bin/env bash

DBINSTANCEIDENTIFIER="my-rds-db"
EXISTINGINSTANCE=$(aws rds describe-db-instances \
    --query 'DBInstances[*].[DBInstanceIdentifier]' \
    --filters Name=db-instance-id,Values=$DBINSTANCEIDENTIFIER \
    --output text \
    )

if [ -z $EXISTINGINSTANCE ]
then
    echo "RDS instance $DBINSTANCEIDENTIFIER does not exist in the account !"
else
    echo "RDS instance $DBINSTANCEIDENTIFIER exists in the account !"
fi

This will check and print whether the RDS instance is existing in the account or not. This snippet can be used as part of any script to check the existence of RDS.

Advertisement