我有一个 AWS Lambda 函数,我试图使用它每晚创建 3 个新数据库。它从每台服务器获取最新的快照并运行restore_db_instance_from_db_snapshot
。它旨在让我们的开发人员无需使用生产数据库即可访问生产数据。
但我遇到的问题是,它本来应该迭代并创建 3 个数据库,但它只到达第一个数据库就停止了。这是正常现象吗?如果是,我该如何解决?
这是我的职能 -
import os
import boto3
import botocore
import datetime
import re
import logging
region = 'us-east-1'
db_subnet = 'default'
db_list = [
{ 'cluster': 'true', 'snapshot_base_name': 'main-production', 'new_db_instance_name': 'main-development', 'instance_class': 'db.r4.large' , 'environment': 'main'},
{ 'cluster': 'false', 'snapshot_base_name': 'cs-production', 'new_db_instance_name': 'cs-development', 'instance_class': 'db.t2.micro' , 'environment': 'cs'},
{ 'cluster': 'false', 'snapshot_base_name': 'acc-production', 'new_db_instance_name': 'acc-development', 'instance_class': 'db.t2.micro', 'environment': 'acc' }
]
publicly_accessible = True
multi_az = False
copy_tags_to_snapshot = False
port = 5432
def byTimestamp(snap):
if 'SnapshotCreateTime' in snap:
return datetime.datetime.isoformat(snap['SnapshotCreateTime'])
else:
return datetime.datetime.isoformat(datetime.datetime.now())
def restore_db():
client = boto3.client('rds', region_name=region)
try:
for db in db_list:
is_cluster = db['cluster'] == 'true'
environment = db['environment']
password = os.environ[environment]
if is_cluster:
source_snaps = client.describe_db_cluster_snapshots(DBClusterIdentifier = db['snapshot_base_name'])['DBClusterSnapshots']
source_snap = sorted(source_snaps, key=byTimestamp, reverse=True)[0]['DBClusterSnapshotIdentifier']
response = client.restore_db_cluster_from_snapshot(
DBClusterIdentifier=db['new_db_instance_name'],
SnapshotIdentifier=source_snap,
Port=port,
Engine='aurora-postgresql')
response = client.create_db_instance(
DBInstanceIdentifier=db['new_db_instance_name'],
DBInstanceClass=db['instance_class'],
Engine='aurora-postgresql',
DBClusterIdentifier=db['new_db_instance_name'])
else:
source_snaps = client.describe_db_snapshots(DBInstanceIdentifier = db['snapshot_base_name'])['DBSnapshots']
source_snap = sorted(source_snaps, key=byTimestamp, reverse=True)[0]['DBSnapshotIdentifier']
response = client.restore_db_instance_from_db_snapshot(
DBInstanceIdentifier=db['new_db_instance_name'],
DBSnapshotIdentifier=source_snap,
DBInstanceClass=db['instance_class'],
Port=port,
MultiAZ=multi_az,
PubliclyAccessible=publicly_accessible,
CopyTagsToSnapshot=copy_tags_to_snapshot)
return response
except botocore.exceptions.ClientError as e:
raise Exception("Could not restore: %s" % e)
def lambda_handler(event, context):
return restore_db()