"""Lambda function module.""" import common_config import config import dynamodb import sentry import util from constants import common def ignore_event(event): """Check if the event should be ignored. Args: event (dict): S3 event Returns: bool: True if event should be ignored, False otherwise """ principal_id = event['Records'][0]['userIdentity']['principalId'] ignore_result = [principal_id.endswith(ignore_id) for ignore_id in config.PRINCIPAL_IDS_TO_IGNORE] return any(ignore_result) @sentry.sentry_capture_exception def handler(event, context): """Lambda entry point.""" if common_config.IGNORE_ALL_EVENTS is not None: common_config.logger.warning(common.IGNORE_MESSAGE) return if ignore_event(event): common_config.logger.info( 'Ignoring event triggered by lambda actions.') return s3_object = event['Records'][0]['s3']['object'] attachment_attrs = util.parse_file_key(s3_object['key']) period_id = attachment_attrs['period_id'] label_id = attachment_attrs['label_id'] account_type = attachment_attrs['account_type'] quarter_periods = util.get_quarter_periods_by_period(period_id) label_type_id_period_id_single = '{}{}_{}'.format( account_type, label_id, period_id) label_type_id_period_id_quarter = util.get_attachment_primary_key_value( account_type, label_id, quarter_periods) file_name = attachment_attrs['filename'] pkey_single = { 'label_type_id_period_id': label_type_id_period_id_single, 'file_name': file_name} pkey_quarter = { 'label_type_id_period_id': label_type_id_period_id_quarter, 'file_name': util.format_quarter_file_name(file_name, period_id)} dynamodb.batch_delete_data( table_name=common_config.DYNAMODB_TABLE_SUCCESS, keys=(pkey_single, pkey_quarter))