Skip to content

Commit fb3fbfd

Browse files
fix(aws-lambda): Keep accepting ./ and absolute task root handler paths
Co-Authored-By: Ivana Kellyer <131587164+sentrivana@users.noreply.github.com>
1 parent 3194674 commit fb3fbfd

4 files changed

Lines changed: 22 additions & 3 deletions

File tree

‎scripts/init_serverless_sdk.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ def extract_and_load_lambda_function_module(self, module_path):
3838
"""
3939
Method that extracts and loads lambda function module from module_path
4040
"""
41+
# the handler used to be loaded from a file path, so keep accepting
42+
# `./dir/file` and absolute `<task root>/dir/file` by making them relative
43+
# to the task root, which is on `sys.path`.
44+
task_root = os.environ.get("LAMBDA_TASK_ROOT", os.getcwd())
45+
module_path = os.path.relpath(os.path.join(task_root, module_path), task_root)
46+
4147
# like the Lambda runtime: `dir/file` is the module `dir.file`, which `sys.modules`
4248
# keeps, so its code only runs on the first invocation.
4349
# https://github.com/aws/aws-lambda-python-runtime-interface-client/blob/fa2462d93d11da632aad8f8bfb07a3e060438150/awslambdaric/bootstrap.py#L53
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# counts the invocations since this module was loaded; gets reset on cold start or
2+
# when the module is reloaded.
3+
num_invocations = 0
4+
5+
6+
def handler(event, context):
7+
global num_invocations
8+
num_invocations += 1
9+
return {"num_invocations": num_invocations}

‎tests/integrations/aws_lambda/test_aws_lambda.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,11 @@ def test_init_error(lambda_client, test_environment):
201201
assert transaction_event["transaction"] == "InitError"
202202

203203

204-
def test_path_handler_loaded_once(lambda_client, test_environment):
204+
@pytest.mark.parametrize("function_name", ["PathHandler", "AbsolutePathHandler"])
205+
def test_path_handler_loaded_once(lambda_client, test_environment, function_name):
205206
num_invocations = [
206207
json.loads(
207-
lambda_client.invoke(FunctionName="PathHandler", Payload=json.dumps({}))[
208+
lambda_client.invoke(FunctionName=function_name, Payload=json.dumps({}))[
208209
"Payload"
209210
].read()
210211
)["num_invocations"]

‎tests/integrations/aws_lambda/utils.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@
2828
SAM_PORT = 3001
2929

3030
# functions whose handler is a path instead of `index.handler`.
31-
INITIAL_HANDLERS = {"PathHandler": "app/index.handler"}
31+
INITIAL_HANDLERS = {
32+
"PathHandler": "app/index.handler",
33+
"AbsolutePathHandler": "/var/task/app/index.handler",
34+
}
3235

3336
PYTHON_VERSION = f"python{sys.version_info.major}.{sys.version_info.minor}"
3437

0 commit comments

Comments
 (0)