"""Lambda smart_downloader function module.""" import logging import boto3 import config import sentry_sdk from src import dynamodb_utils from src import logic from sentry_sdk.integrations.aws_lambda import AwsLambdaIntegration # set logger level to INFO in order to see logs in CloudWatch logging.getLogger().setLevel(logging.INFO) logger = logging.getLogger(__name__) if config.SENTRY_DSN: sentry_sdk.init( dsn=config.SENTRY_DSN, integrations=[ AwsLambdaIntegration(), ], ) else: logger.warning('Initializing without sentry') def handler(event, context): """Lambda entry point.""" dynamodb_client = boto3.client('dynamodb', region_name=config.AWS_REGION) try: if 'Records' in event: # DynamoDB event https://docs.aws.amazon.com/lambda/latest/dg/with-ddb-example.html # noqa:E501 logger.info('DynamoDB event detected') for record in event['Records']: assert record['eventSource'] == 'aws:dynamodb' event_name = record['eventName'] table_name = dynamodb_utils.get_table_name_from_arn( record['eventSourceARN']) if not event_name == 'INSERT': logger.info(f'Skipping eventName {event_name}') continue dynamodb_record = record['dynamodb'] download_request, key = dynamodb_utils.extract_download_request_from_dynamodb_item( # noqa:E501 dynamodb_record) to_update = {'status': 'PROCESSING'} update_kwargs = dynamodb_utils.build_update_item_expression_kwargs( # noqa:E501 to_update) dynamodb_client.update_item( TableName=table_name, Key=key, **update_kwargs ) to_update = {} try: download_response = logic.parallel_download_curl_to_s3( tasks=download_request.tasks, n_threads=download_request.n_threads ) return_code = download_response['return_code'] if return_code != 0: to_update['error_message'] = ( f'Return code {return_code} is not 0' ) to_update['status'] = 'ERROR' else: to_update['status'] = 'DONE' to_update.update(download_response) except Exception as e: to_update['status'] = 'ERROR' to_update['error_message'] = str(e) update_kwargs = dynamodb_utils.build_update_item_expression_kwargs( # noqa:E501 to_update) dynamodb_client.update_item( TableName=table_name, Key=key, **update_kwargs ) else: logger.info('Single invoke event detected') if 'tasks' not in event: logger.warning('tasks key not found in event') logger.warning(event) # single event come from trigger request download_response = logic.parallel_download_curl_to_s3( tasks=event['tasks']) return download_response except Exception as e: logger.exception(str(e)) raise e