File size: 1,440 Bytes
e6c137b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import json
import sys
import os

# Add the current directory to the Python path so we can import main
sys.path.append(os.path.dirname(__file__))

from mangum import Mangum
from main import app

# Create the Lambda handler using Mangum
handler = Mangum(app, lifespan="off")

def lambda_handler(event, context):
    """
    AWS Lambda handler function for the Caltrans Payment Tracking API
    
    This function serves as the entry point for AWS Lambda and routes
    HTTP requests to the FastAPI application using Mangum adapter.
    
    Args:
        event: AWS Lambda event object containing the HTTP request
        context: AWS Lambda context object containing runtime information
        
    Returns:
        HTTP response formatted for AWS API Gateway
    """
    try:
        # Use Mangum to handle the FastAPI app
        response = handler(event, context)
        return response
    except Exception as e:
        # Return error response if something goes wrong
        return {
            'statusCode': 500,
            'headers': {
                'Content-Type': 'application/json',
                'Access-Control-Allow-Origin': '*',
                'Access-Control-Allow-Headers': 'Content-Type',
                'Access-Control-Allow-Methods': 'OPTIONS,POST,GET'
            },
            'body': json.dumps({
                'error': 'Internal server error',
                'message': str(e)
            })
        }