"""Lambda function module.""" from typing import Dict import uuid import boto3 from botocore.errorfactory import ClientError import config from ddex_ingester_common.logging import utils as logging_utils from ddex_ingester_common.schemas.state_machine_schema import ( StateMachineSchema ) logger = logging_utils.get_logger(config.app_logger) s3_client = boto3.client('s3') def handler(event: Dict, context: Dict) -> Dict: """Lambda entrypoint.""" state_machine_data = StateMachineSchema().load(event) correlation_id = state_machine_data.correlation_id or str(uuid.uuid4()) state_machine_data.correlation_id = correlation_id logging_utils.update_logger_correlation_id(logger, correlation_id) logging_utils.update_logger_with_message_ids( logger, state_machine_data.message_id, state_machine_data.message_thread_id, state_machine_data.execution_name ) upc = state_machine_data.product.upc lockfile_key = f'{config.INGESTION_LOCK_S3_PATH}{upc}.lock' # Check if lockfile exists try: logger.info('Checking if lock exists') s3_client.head_object( Bucket=config.INGESTION_LOCK_S3_BUCKET, Key=lockfile_key) msg = f'Lockfile exists: {lockfile_key}' logger.warn(msg) raise LockfileExistsException(msg) except ClientError: # Lockfile does not exist logger.info(f'Creating lockfile: {lockfile_key}') s3_client.put_object( Bucket=config.INGESTION_LOCK_S3_BUCKET, Key=lockfile_key ) return StateMachineSchema().dump(state_machine_data) class LockfileExistsException(Exception): """Lockfile exists exception."""