Python 3.x 描述AWS地区的RDS快照
我正在尝试获取快照,并在区域目录中描述来自db_实例的快照:Python 3.x 描述AWS地区的RDS快照,python-3.x,amazon-web-services,dictionary,amazon-rds,aws-kms,Python 3.x,Amazon Web Services,Dictionary,Amazon Rds,Aws Kms,我正在尝试获取快照,并在区域目录中描述来自db_实例的快照: regions={'us-east-1':'us-east-2','us-west-1':'us-west-2'} 区域中的DB实例: us-east-1 = testeast1 us-west-1 = testwest1 使用此函数调用DB实例,我可以返回该区域中的所有DB实例: def rds_instances(): inst = [] for rg in regions.key(): res = b
regions={'us-east-1':'us-east-2','us-west-1':'us-west-2'}
区域中的DB实例:
us-east-1 = testeast1
us-west-1 = testwest1
使用此函数调用DB实例,我可以返回该区域中的所有DB实例:
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:调用DescribedBinInstances操作时发生错误(DBInstanceNotFound):DBInstance testwest1未找到。
如果rds实例不在该区域,如何修改代码以通过,并避免此错误
运行descripe kms key命令时,我也会遇到相同的错误。对目标区域运行此命令时出现相同错误:
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
看起来您的rds\u instances()
函数一次只能执行一个区域,因为您已经在区域的迭代中调用了它。此外,该函数还可以进行大量压缩inst=[src['DBInstanceIdentifier']用于res中的src。如果src['DBInstanceStatus']=='available']
rds\U instance
正确返回每个区域中的所有db实例,请描述数据库实例()。但是当调用descripe函数时,当它找不到该实例时会出错。我需要捕捉错误并跳到下一个区域,直到它找到rds\u实例