How to catch this boto error botocore.errorfactory.DBInstanceNotFoundFault
This is my python3 code :
import boto3
import botocore.exceptions
import botocore.errorfactory
clientrds = boto3.client('rds')
rdsInstances = ['prod-rds']
for rds in rdsInstances :
rds_response = clientrds.describe_db_instances(DBInstanceIdentifier=rds)
DBInstanceStatus = (rds_response["DBInstances"][0]["DBInstanceStatus"])
if DBInstanceStatus == "stopped" :
print(rds," Switched On ")
start_rds_response = clientrds.start_db_instance(DBInstanceIdentifier=rds)
console output / Error :
Traceback (most recent call last):
File "/home/rds.py", line 11, in <module>
rds_response = clientrds.describe_db_instances(DBInstanceIdentifier=rds)
File "/home/.local/lib/python3.8/site-packages/botocore/client.py", line 391, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/home/.local/lib/python3.8/site-packages/botocore/client.py", line 719, in _make_api_call
raise error_class(parsed_response, operation_name)
botocore.errorfactory.DBInstanceNotFoundFault: An error occurred (DBInstanceNotFound) when calling the DescribeDBInstances operation: DBInstance prod-rds not found.
CodePudding user response:
Able to handle the execption using botocore.exceptions
for rds in rdsInstances :
try :
start_rds_response = clientrds.start_db_instance(DBInstanceIdentifier=rds) #RDS.instance.start
except botocore.errorfactory.ClientError as error:
if error.response['Error']['Code'] == 'DBInstanceNotFound':
print("DBInstanceNotFound")
Ref : Link