2

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

ghenzi83
  • 109
  • 1
  • 13
  • It looks like your `rds_instances()` function should only do one region at a time, because you are already calling it within an iteration of regions. – jordanm Sep 10 '20 at 20:36
  • Also, the function can be condensed a lot. `inst = [src['DBInstanceIdentifier'] for src in res.describe_db_instances() if src['DBInstanceStatus'] == 'available']` – jordanm Sep 10 '20 at 20:39
  • `rds_instance` returns all db instances in each region correctly. But when the describe function is called, but it errors when it does not find that instance. I need to catch the error and skip to the next region until it finds the rds_instance – ghenzi83 Sep 10 '20 at 21:02

1 Answers1

-1

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
ghenzi83
  • 109
  • 1
  • 13