"""Lambda file_upload_initialize function module.""" from datetime import datetime import sentry_sdk from lambdacommon.common_config import logger from pydantic import ValidationError from sentry_sdk.integrations.aws_lambda import AwsLambdaIntegration import config from file_upload_initialize.processor import FileUploadInitializeProcessor from file_upload_initialize.schemas import FileUploadEvent if config.SENTRY_DSN: sentry_sdk.init( dsn=config.SENTRY_DSN, environment=config.ENVIRONMENT, integrations=[AwsLambdaIntegration(timeout_warning=True)], ) def handler(event: dict, context: object) -> dict: """Lambda entry point. Args: event (dict): Event data passed to the Lambda function context (object): Lambda runtime information Returns: dict: Response with status """ try: # Validate event structure logger.info(f'Event: {event}') validated_event = FileUploadEvent(**event) # Process the event processor = FileUploadInitializeProcessor(validated_event) start_time = datetime.now() result = processor.process() end_time = datetime.now() # Log processing time diff = end_time - start_time logger.info(f'Finished processing in {diff}') # Return result return result.model_dump() except ValidationError as e: logger.error(f'Invalid event structure: {e.json()}') raise except Exception: logger.exception('Failed to process file upload') raise