forked from aws/aws-sam-cli
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlambda_debug_settings.py
More file actions
224 lines (208 loc) · 10.3 KB
/
Copy pathlambda_debug_settings.py
File metadata and controls
224 lines (208 loc) · 10.3 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
"""
Represents Lambda debug entrypoints.
"""
import logging
from argparse import ArgumentParser
from collections import namedtuple
from typing import List, cast
from samcli.local.docker.lambda_image import Runtime
class DebuggingNotSupported(Exception):
pass
DebugSettings = namedtuple("DebugSettings", ["entrypoint", "container_env_vars"])
LOG = logging.getLogger(__name__)
class LambdaDebugSettings:
@staticmethod
def get_debug_settings(debug_port, debug_args_list, _container_env_vars, runtime, options):
"""
Get Debug settings based on the Runtime
Parameters
----------
debug_port int
Port to open for debugging in the container
debug_args_list list(str)
Additional debug args
container_env_vars dict
Additional debug environmental variables
runtime str
Lambda Function runtime
options dict
Additonal options needed (i.e delve Path)
Returns
-------
tuple:DebugSettings (list, dict)
Tuple of debug entrypoint and debug env vars
"""
entry = ["/var/rapid/aws-lambda-rie", "--log-level", "error"]
if not _container_env_vars:
_container_env_vars = dict()
# The value of entrypoint_mapping is a callable instead of DebugSettings
# so that DebugSetting objects are not always created.
entrypoint_mapping = {
Runtime.java8al2.value: lambda: DebugSettings(
entry,
container_env_vars={
"_JAVA_OPTIONS": "-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,quiet=y,"
f"address={debug_port} -XX:MaxHeapSize=2834432k -XX:MaxMetaspaceSize=163840k "
"-XX:ReservedCodeCacheSize=81920k -XX:+UseSerialGC -XX:-TieredCompilation "
"-Djava.net.preferIPv4Stack=true -Xshare:off" + " ".join(debug_args_list),
**_container_env_vars,
},
),
Runtime.java11.value: lambda: DebugSettings(
entry,
container_env_vars={
"_JAVA_OPTIONS": "-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,quiet=y,"
f"address=*:{debug_port} -XX:MaxHeapSize=2834432k -XX:MaxMetaspaceSize=163840k "
"-XX:ReservedCodeCacheSize=81920k -XX:+UseSerialGC -XX:-TieredCompilation "
"-Djava.net.preferIPv4Stack=true" + " ".join(debug_args_list),
**_container_env_vars,
},
),
Runtime.java17.value: lambda: DebugSettings(
entry,
container_env_vars={
"_JAVA_OPTIONS": "-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,quiet=y,"
f"address=*:{debug_port} -XX:MaxHeapSize=2834432k -XX:+UseSerialGC "
"-XX:+TieredCompilation -XX:TieredStopAtLevel=1 "
"-Djava.net.preferIPv4Stack=true" + " ".join(debug_args_list),
**_container_env_vars,
},
),
Runtime.java21.value: lambda: DebugSettings(
entry,
container_env_vars={
"_JAVA_OPTIONS": "-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,quiet=y,"
f"address=*:{debug_port} -XX:MaxHeapSize=2834432k -XX:+UseSerialGC "
"-XX:+TieredCompilation -XX:TieredStopAtLevel=1 "
"-Djava.net.preferIPv4Stack=true" + " ".join(debug_args_list),
**_container_env_vars,
},
),
Runtime.java25.value: lambda: DebugSettings(
entry,
container_env_vars={
"_JAVA_OPTIONS": "-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,quiet=y,"
f"address=*:{debug_port} -XX:MaxHeapSize=2834432k -XX:+UseSerialGC "
"-XX:+TieredCompilation -XX:TieredStopAtLevel=1 "
"-Djava.net.preferIPv4Stack=true" + " ".join(debug_args_list),
**_container_env_vars,
},
),
Runtime.dotnet6.value: lambda: DebugSettings(
entry + ["/var/runtime/bootstrap"] + debug_args_list,
container_env_vars={"_AWS_LAMBDA_DOTNET_DEBUGGING": "1", **_container_env_vars},
),
Runtime.dotnet8.value: lambda: DebugSettings(
entry + ["/var/runtime/bootstrap"] + debug_args_list,
container_env_vars={"_AWS_LAMBDA_DOTNET_DEBUGGING": "1", **_container_env_vars},
),
Runtime.go1x.value: lambda: DebugSettings(
entry,
container_env_vars={
"_AWS_LAMBDA_GO_DEBUGGING": "1",
"_AWS_LAMBDA_GO_DELVE_API_VERSION": LambdaDebugSettings.parse_go_delve_api_version(debug_args_list),
"_AWS_LAMBDA_GO_DELVE_LISTEN_PORT": debug_port,
"_AWS_LAMBDA_GO_DELVE_PATH": options.get("delvePath"),
**_container_env_vars,
},
),
Runtime.nodejs16x.value: lambda: DebugSettings(
entry
+ ["/var/lang/bin/node"]
+ debug_args_list
+ ["--no-lazy", "--expose-gc"]
+ ["/var/runtime/index.mjs"],
container_env_vars={
"NODE_PATH": "/opt/nodejs/node_modules:/opt/nodejs/node16/node_modules:/var/runtime/node_modules:"
"/var/runtime:/var/task",
"NODE_OPTIONS": f"--inspect-brk=0.0.0.0:{str(debug_port)} --max-http-header-size 81920",
"AWS_EXECUTION_ENV": "AWS_Lambda_nodejs16.x",
**_container_env_vars,
},
),
Runtime.nodejs18x.value: lambda: DebugSettings(
entry
+ ["/var/lang/bin/node"]
+ debug_args_list
+ ["--no-lazy", "--expose-gc"]
+ ["/var/runtime/index.mjs"],
container_env_vars={
"NODE_PATH": "/opt/nodejs/node_modules:/opt/nodejs/node18/node_modules:/var/runtime/node_modules:"
"/var/runtime:/var/task",
"NODE_OPTIONS": f"--inspect-brk=0.0.0.0:{str(debug_port)} --max-http-header-size 81920",
"AWS_EXECUTION_ENV": "AWS_Lambda_nodejs18.x",
**_container_env_vars,
},
),
Runtime.nodejs20x.value: lambda: DebugSettings(
entry
+ ["/var/lang/bin/node"]
+ debug_args_list
+ ["--no-lazy", "--expose-gc"]
+ ["/var/runtime/index.mjs"],
container_env_vars={
"NODE_PATH": "/opt/nodejs/node_modules:/opt/nodejs/node20/node_modules:/var/runtime/node_modules:"
"/var/runtime:/var/task",
"NODE_OPTIONS": f"--inspect-brk=0.0.0.0:{str(debug_port)} --max-http-header-size 81920",
"AWS_EXECUTION_ENV": "AWS_Lambda_nodejs20.x",
**_container_env_vars,
},
),
Runtime.nodejs22x.value: lambda: DebugSettings(
entry
+ ["/var/lang/bin/node"]
+ debug_args_list
+ ["--no-lazy", "--expose-gc"]
+ ["/var/runtime/index.mjs"],
container_env_vars={
"NODE_PATH": "/opt/nodejs/node_modules:/opt/nodejs/node22/node_modules:/var/runtime/node_modules:"
"/var/runtime:/var/task",
"NODE_OPTIONS": f"--inspect-brk=0.0.0.0:{str(debug_port)} --max-http-header-size 81920",
"AWS_EXECUTION_ENV": "AWS_Lambda_nodejs22.x",
**_container_env_vars,
},
),
Runtime.python38.value: lambda: DebugSettings(
entry + ["/var/lang/bin/python3.8"] + debug_args_list + ["/var/runtime/bootstrap.py"],
container_env_vars=_container_env_vars,
),
Runtime.python39.value: lambda: DebugSettings(
entry + ["/var/lang/bin/python3.9"] + debug_args_list + ["/var/runtime/bootstrap.py"],
container_env_vars=_container_env_vars,
),
Runtime.python310.value: lambda: DebugSettings(
entry + ["/var/lang/bin/python3.10"] + debug_args_list + ["/var/runtime/bootstrap.py"],
container_env_vars=_container_env_vars,
),
Runtime.python311.value: lambda: DebugSettings(
entry + ["/var/lang/bin/python3.11"] + debug_args_list + ["/var/runtime/bootstrap.py"],
container_env_vars=_container_env_vars,
),
Runtime.python312.value: lambda: DebugSettings(
entry + ["/var/lang/bin/python3.12"] + debug_args_list + ["/var/runtime/bootstrap.py"],
container_env_vars=_container_env_vars,
),
Runtime.python313.value: lambda: DebugSettings(
entry + ["/var/lang/bin/python3.13"] + debug_args_list + ["/var/runtime/bootstrap.py"],
container_env_vars=_container_env_vars,
),
Runtime.python314.value: lambda: DebugSettings(
entry + ["/var/lang/bin/python3.14"] + debug_args_list + ["/var/runtime/bootstrap.py"],
container_env_vars=_container_env_vars,
),
}
try:
return entrypoint_mapping[runtime]()
except KeyError as ex:
if not runtime:
LOG.debug("Passing entrypoint as specified in template")
return DebugSettings(entry + debug_args_list, _container_env_vars)
raise DebuggingNotSupported("Debugging is not currently supported for {}".format(runtime)) from ex
@staticmethod
def parse_go_delve_api_version(debug_args_list: List[str]) -> int:
parser = ArgumentParser("Parser for delve args")
parser.add_argument("-delveAPI", type=int, default=1)
args, unknown_args = parser.parse_known_args(debug_args_list)
if unknown_args:
LOG.warning('Ignoring unrecognized arguments: %s. Only "-delveAPI" is supported.', unknown_args)
return cast(int, args.delveAPI)