caltrans-pt / lambda-deployment /lambda_function.py
mousom's picture
Upload folder using huggingface_hub
e6c137b verified
Raw History Blame Contribute Delete
1.44 kB
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)
})
}