微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

描述跨AWS区域的RDS快照

如何解决描述跨AWS区域的RDS快照

我正在尝试获取快照并在区域的字典中描述来自db_instances的快照:

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

区域中的数据库实例:

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

使用此函数调用数据库实例,我可以返回该区域中的所有数据库实例:

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

从区域获取快照:

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 

问题:我尝试从每个区域获取rds快照时遇到问题,它会寻找rds实例并检查错误区域中的rds实例,然后返回错误

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

如果rds实例不在该区域中,如何修改代码以通过,并避免出现此错误

运行describe kms key command时也会遇到相同的错误。在目标区域运行此命令时发生相同的错误

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

解决方,我通过使用简单的try / except捕获错误并通过解决了此问题。应该早点想到这个!

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

解决方法

我通过使用简单的try / except捕获错误并通过解决了这个问题。

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。