-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlambda_handler.py
More file actions
109 lines (90 loc) · 3.21 KB
/
Copy pathlambda_handler.py
File metadata and controls
109 lines (90 loc) · 3.21 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
"""
AWS Lambda handler for the secure agent flow application.
This handler processes requests and runs the CrewAI workflow with AWS Bedrock.
"""
import json
import logging
import os
from typing import Dict, Any
# Configure logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)
try:
from crew import SecureAgentFlowCrew
from config import Config
except ImportError as e:
logger.error(f"Import error: {e}")
# For Lambda deployment, these modules should be available
# Define fallback classes to prevent NameError
class SecureAgentFlowCrew:
def run_workflow(self, **kwargs):
raise RuntimeError("SecureAgentFlowCrew not available")
class Config:
@staticmethod
def validate_config():
return {"valid": False, "message": "Config module not available"}
def agent_handler(event: Dict[str, Any], context) -> Dict[str, Any]:
"""
AWS Lambda handler function for secure agent flow.
Args:
event: Lambda event containing the request data
context: Lambda context object
Returns:
Dict containing the response with statusCode and body
"""
try:
logger.info(f"Received event: {json.dumps(event)}")
# Extract input from the event
body = event.get('body', '{}')
if isinstance(body, str):
body = json.loads(body)
os.environ["WEBSOCKET_CONNECTION_ID"] = event.get('requestContext', {}).get('connectionId', "123456")
context_input = body.get("context_input")
# Validate configuration
config_status = Config.validate_config()
if not config_status["valid"]:
logger.error(f"Configuration error: {config_status['message']}")
return {
'statusCode': 400,
'headers': {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
'body': json.dumps({
'error': 'Configuration error',
'message': config_status['message']
})
}
# Initialize and run the crew
logger.info("Initializing SecureAgentFlowCrew")
crew = SecureAgentFlowCrew()
logger.info("Starting workflow execution")
result = crew.run_workflow(
context_input=context_input
)
logger.info("Workflow completed successfully")
return {
'statusCode': 200,
'headers': {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
'body': json.dumps({
'success': True,
'result': result,
'message': 'Secure agent flow executed successfully'
})
}
except Exception as e:
logger.error(f"Error in lambda_handler: {str(e)}", exc_info=True)
return {
'statusCode': 500,
'headers': {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
'body': json.dumps({
'error': 'Internal server error',
'message': str(e)
})
}