I have an AWS Lambda function that I'm trying to use to create 3 new databases every night. It takes the most recent snapshot from each server and runs restore_db_instance_from_db_snapshot
. Its meant to give our devs access to production data without obviously using the production database.
The problem I'm having though is that its meant to iterate through and create 3 databases, however its only getting to the first database and stopping. Is this normal behavior and if so how do I fix it?
Here is my function -
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()