I want my bash script to detect, if an AWS RDS instance with a specific name exists already.
This is what I tried:
#!/usr/bin/env bash DBINSTANCEIDENTIFIER=greatdb EXISTINGINSTANCE=$(aws rds describe-db-instances \ --db-instance-identifier="$DBINSTANCEIDENTIFIER" \ --output text\ )
I would expect an empty list, or a zero as a result – but I receive an error message:
An error occurred (DBInstanceNotFound) when calling the DescribeDBInstances operation: DBInstance greatdb not found.
Does anyone know how to properly find out if an instance exists or not, without an error message?
Best Answer
Notice the constraint in the docs when using
--db-instance-indentifier
:So you can only use this option if you know the DB in fact exists.
Using queries
To search for a DB that may or may not exist you'll have to use the
--query
option:The
DBINstances
JSON structure is accessible in the awscli help:Using Filters
Another simple solution for the initial question is, to use the
--filters
parameter. The query will return either the instance identifier (if the instance exists), or an empty string (if it does not exist):References