"""Lambda function module for remove_ingestion_lock.""" import boto3 from common.schemas.state_machine_schema import StateMachineSchema import config logger = config.app_logger s3_client = boto3.client('s3') def handler(event, context): """Remove lock file.""" state_machine_data = StateMachineSchema().load(event) upc = state_machine_data.product.upc lockfile_key = f'{config.INGESTION_LOCK_S3_PATH}{upc}.lock' # Delete lockfile logger.info(f'Deleting lockfile: {lockfile_key}') s3_client.delete_object( Bucket=config.INGESTION_LOCK_S3_BUCKET, Key=lockfile_key, ExpectedBucketOwner=config.INGESTION_S3_EXPECTED_OWNER ) display_upc = state_machine_data.product.display_upc if upc != display_upc: lockfile_key = f'{config.INGESTION_LOCK_S3_PATH}{display_upc}.lock' # Delete lockfile logger.info(f'Deleting lockfile: {lockfile_key}') s3_client.delete_object( Bucket=config.INGESTION_LOCK_S3_BUCKET, Key=lockfile_key, ExpectedBucketOwner=config.INGESTION_S3_EXPECTED_OWNER ) return StateMachineSchema().dump(state_machine_data)