in lambda/copy_snapshots_no_x_account_aurora/lambda_function.py [0:0]
def lambda_handler(event, context):
# Describe all snapshots
pending_copies = 0
client = boto3.client('rds', region_name=REGION)
response = paginate_api_call(client, 'describe_db_cluster_snapshots', 'DBClusterSnapshots')
source_snapshots = get_own_snapshots_source(PATTERN, response)
own_snapshots_encryption = get_own_snapshots_dest(PATTERN, response)
# Get list of snapshots in DEST_REGION
client_dest = boto3.client('rds', region_name=DESTINATION_REGION)
response_dest = paginate_api_call(client_dest, 'describe_db_cluster_snapshots', 'DBClusterSnapshots')
dest_snapshots = get_own_snapshots_dest(PATTERN, response_dest)
for source_identifier, source_attributes in source_snapshots.items():
creation_date = get_timestamp(source_identifier, source_snapshots)
if creation_date:
time_difference = datetime.now() - creation_date
days_difference = time_difference.total_seconds() / 3600 / 24
# Only copy if it's newer than RETENTION_DAYS
if days_difference < RETENTION_DAYS:
# Copy to DESTINATION_REGION
if source_identifier not in dest_snapshots.keys() and REGION != DESTINATION_REGION:
if source_snapshots[source_identifier]['Status'] == 'available':
try:
copy_remote(source_identifier, own_snapshots_encryption[source_identifier])
except Exception as e:
pending_copies += 1
logger.error(e)
logger.error('Remote copy pending: %s: %s' % (
source_identifier, source_snapshots[source_identifier]['Arn']))
else:
pending_copies += 1
logger.error('Remote copy pending: %s: %s' % (
source_identifier, source_snapshots[source_identifier]['Arn']))
else:
logger.info('Not copying %s locally. Older than %s days' % (source_identifier, RETENTION_DAYS))
else:
logger.info('Not copying %s locally. No valid timestamp' % source_identifier)
if pending_copies > 0:
log_message = 'Copies pending: %s. Needs retrying' % pending_copies
logger.error(log_message)
raise SnapshotToolException(log_message)