def lambda_handler()

in lambda/copy_snapshots_dest_rds/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_snapshots', 'DBSnapshots', IncludeShared=True)

    shared_snapshots = get_shared_snapshots(PATTERN, response)
    own_snapshots = 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_snapshots', 'DBSnapshots')
    own_dest_snapshots = get_own_snapshots_dest(PATTERN, response_dest)

    for shared_identifier, shared_attributes in shared_snapshots.items():

        if shared_identifier not in own_snapshots.keys() and shared_identifier not in own_dest_snapshots.keys():
        # Check date
            creation_date = get_timestamp(shared_identifier, shared_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 own account
                    try:
                        copy_local(shared_identifier, shared_attributes)

                    except Exception as e:
                        pending_copies += 1
                        logger.error('Local copy pending: %s (%s)' % (shared_identifier, e))

                    else:
                        if REGION != DESTINATION_REGION:
                            pending_copies += 1
                            logger.error('Remote copy pending: %s' % shared_identifier)

                else:
                    logger.info('Not copying %s locally. Older than %s days' % (shared_identifier, RETENTION_DAYS))

            else:
                logger.info('Not copying %s locally. No valid timestamp' % shared_identifier)


        # Copy to DESTINATION_REGION
        elif shared_identifier not in own_dest_snapshots.keys() and shared_identifier in own_snapshots.keys() and REGION != DESTINATION_REGION:
            if own_snapshots[shared_identifier]['Status'] == 'available':
                try:
                    copy_remote(shared_identifier, own_snapshots[shared_identifier])

                except Exception as e:
                    pending_copies += 1
                    logger.error('Remote copy pending: %s: %s (%s)' % (
                        shared_identifier, own_snapshots[shared_identifier]['Arn'], e))
            else:
                pending_copies += 1
                logger.error('Remote copy pending: %s: %s' % (
                    shared_identifier, own_snapshots[shared_identifier]['Arn']))

        # Delete local snapshots
        elif shared_identifier in own_dest_snapshots.keys() and shared_identifier in own_snapshots.keys() and own_dest_snapshots[shared_identifier]['Status'] == 'available' and REGION != DESTINATION_REGION:

            response = client.delete_db_snapshot(
                DBSnapshotIdentifier=shared_identifier
            )

            logger.info('Deleting local snapshot: %s' % shared_identifier)

    if pending_copies > 0:
        log_message = 'Copies pending: %s. Needs retrying' % pending_copies
        logger.error(log_message)
        raise SnapshotToolException(log_message)