-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
54 lines (45 loc) · 1.85 KB
/
Copy pathmain.py
File metadata and controls
54 lines (45 loc) · 1.85 KB
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
48
49
50
51
52
53
# main.py
# This file is used to launch the Lambda Runtime Interface Client (RIC)
# for local testing. It simulates the AWS Lambda environment.
import sys
from mangum import Mangum
from fastapi import FastAPI, Request, Response
import json
# The handler function is in agent.py
from agent import lambda_handler
app = FastAPI()
@app.post("/")
async def handle_request(request: Request):
"""
This endpoint mimics the AWS Lambda invocation endpoint.
It translates the incoming HTTP request into the event dictionary
expected by the Lambda handler.
"""
# AWS Lambda Test Event in the console sends a JSON body like this.
# For a real invocation, the structure might be different, but for
# local testing this is sufficient.
event = await request.json()
# An empty context object is usually fine for local testing.
context = {}
# Call the actual Lambda handler
response_data = lambda_handler(event, context)
# The handler returns a dict, we need to wrap it in a JSON response.
return Response(
content=json.dumps(response_data),
media_type="application/json",
status_code=200
)
# The Mangum adapter wraps the FastAPI app to make it compatible with AWS Lambda.
handler = Mangum(app)
if __name__ == "__main__":
# For local execution, you might want to add a simple CLI interface
# or a direct call for testing.
# Example: python main.py '{"actionGroup": "...", ...}'
if len(sys.argv) > 1:
event_payload = json.loads(sys.argv[1])
result = lambda_handler(event_payload, {})
print(json.dumps(result, indent=2))
else:
print("Usage for local test: python main.py '<json_event_payload>'")
print("Starting local server simulation is not implemented in this file.")
print("Use docker-compose to run the full simulated environment.")