Search code examples
python-3.xamazon-web-servicesdictionaryamazon-rdsamazon-kms

Describe RDS Snapshots across AWS Regions


I am trying to get snapshots and describe the snapshots from db_instances in the dict of regions:

regions = {'us-east-1':'us-east-2', 'us-west-1':'us-west-2'}

DB instances in region:

us-east-1 = testeast1
us-west-1 = testwest1

DB instances are called with this function and I can return all db instances in that region:

def rds_instances():
   inst = []
   for rg in regions.key():
      res = boto3.client('rds', region_name=rg) 
      srcs = res.describe_db_instances()
      for src in srcs['DBInstances']: 
         if src['DBInstanceStatus']  == 'available': 
            rds_db = src['DBInstanceIdentifier']
            inst.append(rds_db)
   return inst

Get snapshots from regions:

def get_kms(): 
   snapshots = []
   for rg in regions:
      src = boto3.client('rds', region_name=rg) 
      instances = rds_instances()
      for instance in instances: 
         src_rds = src.describe_db_instances(DBInstanceIdentifier=instance) 
         if src_rds['DBInstances'][0]['DBInstanceStatus']  == 'available': 
            src_snap = src.describe_db_snapshots(DBInstanceIdentifier=instance, SnapshotType='automated')['DBSnapshots'] 
            for snap in src_snap:
               kms_id = snap['KmsKeyId']
               snapshots.append(kms_id)
   return snapshots 

Issue: I run into an issue when I attempt to get the rds snapshot from each region, it looks for the rds instance and checks for the rds instance in the wrong region and then returns an error:

DBInstanceNotFoundFault: An error occurred (DBInstanceNotFound) when calling the DescribeDBInstances operation: DBInstance testwest1 not found.

How do I modify the code to pass if an rds instance is not in that region, and avoid this error??

I also get the same error when I run the describe kms key command. Same error running this command for destination regions:

   for region in regions:
      kms_client = boto3.client('kms', region_name=region)
      keys = kms_client.describe_key(KeyId=alias_name)

Solution I got this resolved by using a simple try/except to catch the error and pass. Should have thought about this sooner!

      for instance in instances: 
         try:
            src_rds = src.describe_db_instances(DBInstanceIdentifier=instance) 
         except ClientError:
            pass


Solution

  • I got this resolved by using a simple try/except to catch the error and pass.

          for instance in instances: 
             try:
                src_rds = src.describe_db_instances(DBInstanceIdentifier=instance) 
             except ClientError:
                pass