-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_wrapper.py
More file actions
79 lines (68 loc) · 2.32 KB
/
lambda_wrapper.py
File metadata and controls
79 lines (68 loc) · 2.32 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
import subprocess
import json
import os
import logging
import traceback
# Configure logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def lambda_handler(event, context):
try:
# Determine binary path
task_root = os.environ.get("LAMBDA_TASK_ROOT", "/var/task")
binary_path = os.path.join(task_root, "lambda_handler")
# Validate binary existence and executability
if not os.path.exists(binary_path):
logger.error(f"Binary not found at {binary_path}")
return {
"statusCode": 404,
"body": json.dumps(
{"error": "Binary not found", "path": binary_path}
),
}
if not os.access(binary_path, os.X_OK):
logger.error(f"Binary not executable at {binary_path}")
return {
"statusCode": 403,
"body": json.dumps(
{"error": "Binary not executable", "path": binary_path}
),
}
# Prepare input for subprocess
input_data = json.dumps({"event": event, "context": str(context)})
# Run subprocess with enhanced error handling
try:
result = subprocess.run(
[binary_path],
input=input_data,
text=True,
capture_output=True,
env=os.environ.copy(),
timeout=30, # Add timeout to prevent hanging
)
except subprocess.TimeoutExpired:
logger.error("Binary execution timed out")
return {
"statusCode": 504,
"body": json.dumps({"error": "Execution timed out"}),
}
# Log stderr if not empty
if result.stderr:
print(result.stderr)
# Log stdout if not empty
if result.stdout:
print(result.stdout)
except Exception as e:
# Catch-all error handling
logger.error(f"Unexpected error: {str(e)}")
logger.error(traceback.format_exc())
return {
"statusCode": 500,
"body": json.dumps(
{
"error": "Unexpected error",
"details": str(e),
"trace": traceback.format_exc(),
}
),
}