-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_lambda.py
More file actions
106 lines (92 loc) · 2.99 KB
/
debug_lambda.py
File metadata and controls
106 lines (92 loc) · 2.99 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
import json
import os
from dotenv import load_dotenv
from lambda_function import lambda_handler
from datetime import datetime, timezone
### Context object for local testing ###
class Context:
def __init__(self):
self.function_name = "FunctionName"
self.function_version = "1"
self.memory_limit_in_mb = 128
self.log_stream_name = "log_stream_name"
self.aws_request_id = "aws_request_id"
self.get_remaining_time_in_millis = lambda: 1000
# Setup the local env vars
load_dotenv()
x_api_key = os.environ.get("X_API_KEY")
nasa_api_key = os.environ.get("NASA_API_KEY", "DEMO_KEY")
date_param_str = datetime.now(timezone.utc).strftime("%Y-%m-%d")
# Construct events
event = {
"version": "2.0",
"routeKey": "$default",
"rawPath": "/path/to/resource",
"rawQueryString": "parameter1=value1¶meter1=value2¶meter2=value",
"cookies": ["cookie1", "cookie2"],
"headers": {
"Header1": "value1",
"Header2": "value1,value2",
"X-Source": "APIGateway",
"x-api-key": x_api_key,
},
"queryStringParameters": {
"someDate": date_param_str,
},
"requestContext": {
"accountId": "123456789012",
"apiId": "api-id",
"authentication": {
"clientCert": {
"clientCertPem": "CERT_CONTENT",
"subjectDN": "www.example.com",
"issuerDN": "Example issuer",
"serialNumber": "a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1",
"validity": {
"notBefore": "May 28 12:30:02 2019 GMT",
"notAfter": "Aug 5 09:36:04 2021 GMT",
},
}
},
"domainName": "id.execute-api.us-east-1.amazonaws.com",
"domainPrefix": "id",
"http": {
"method": "POST",
"path": "/path/to/resource",
"protocol": "HTTP/1.1",
"sourceIp": "192.168.0.1/32",
"userAgent": "agent",
},
"requestId": "id",
"routeKey": "$default",
"stage": "$default",
"time": "12/Mar/2020:19:03:58 +0000",
"timeEpoch": 1583348638390,
},
"body": {
"key1": "value1",
"key2": "value2",
"key3": "value3",
},
"pathParameters": {"parameter1": "value1"},
"isBase64Encoded": True,
"stageVariables": {"stageVariable1": "value1", "stageVariable2": "value2"},
}
# For debugging
context = Context()
print(f"event: {event}")
try:
resp = lambda_handler(event, context)
print("##################### DEBUG LAMBDA #####################")
for k in resp:
if k == "body":
body = json.loads(resp[k])
print(f"{k}: {json.dumps(body, indent=2)}")
else:
print(f"{k}: {resp[k]}")
except Exception as e:
print("##################### ERROR #####################")
print(f"Error type: {type(e).__name__}")
print(f"Error message: {e!s}")
import traceback
print(traceback.format_exc())