Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.
Python awsaws-emulatoraws-localaws-sdkdevtoolsdockerdynamodbec2emulatorlambdalocalstacklocalstack-alternativeministackmock-awsopen-sourcepythons3sqsterraform
fix(lambda): accept slash-form Python handler paths (pkg/sub/mod.fn) (#451)
W. H. Wangf173a44ecb4a
diff --git a/ministack/core/lambda_runtime.py b/ministack/core/lambda_runtime.pyindex 94d08475..7b0b1fd9 100644--- a/ministack/core/lambda_runtime.py+++ b/ministack/core/lambda_runtime.py@@ -414,6 +414,12 @@ class Worker:handler = self.config.get("Handler", "index.handler")module_name, handler_name = handler.rsplit(".", 1)+ # AWS Python Lambda accepts both dot (``pkg.mod.fn``) and slash+ # (``pkg/mod.fn``) in nested handler paths; ``__import__`` only+ # takes dot. Other runtimes (Node.js, etc.) keep the raw string+ # because they don't use Python module resolution.+ if runtime.startswith("python"):+ module_name = module_name.replace("/", ".")env_vars = self.config.get("Environment", {}).get("Variables", {})spawn_env = {**os.environ, **env_vars}# Restore the internal endpoint URL so Lambda SDK calls reachdiff --git a/ministack/services/lambda_svc.py b/ministack/services/lambda_svc.pyindex e75935b7..cbdd731e 100644--- a/ministack/services/lambda_svc.py+++ b/ministack/services/lambda_svc.py@@ -2711,6 +2711,12 @@ def _execute_function_local(func: dict, event: dict) -> dict:if "." not in handler:return {"body": {"errorMessage": f"Invalid handler format: {handler}", "errorType": "Runtime.InvalidEntrypoint"}, "error": True}module_name, func_name = handler.rsplit(".", 1)+ # AWS Python Lambda accepts both dot (``pkg.mod.fn``) and slash+ # (``pkg/mod.fn``) in nested handler paths; ``__import__`` only+ # takes dot. Other runtimes (Node.js, etc.) keep the raw string+ # because they don't use Python module resolution.+ if runtime.startswith("python"):+ module_name = module_name.replace("/", ".")if is_node:wrapper_path = os.path.join(tmpdir, "_wrapper.js")diff --git a/tests/test_lambda.py b/tests/test_lambda.pyindex 2c170bb3..ec532303 100644--- a/tests/test_lambda.py+++ b/tests/test_lambda.py@@ -59,6 +59,34 @@ def test_lambda_create_invoke(lam):payload = json.loads(resp["Payload"].read())assert payload["statusCode"] == 200++def test_lambda_python_nested_handler_slash_form(lam):+ """AWS Python Lambda accepts both dot and slash separators in nested+ handler paths (``pkg/sub/mod.fn`` equivalent to ``pkg.sub.mod.fn``);+ real AWS resolves either form via the underlying file path. MiniStack+ previously imported the pre-rsplit string literally, so slash form+ failed with ``ModuleNotFoundError: No module named 'pkg/sub/mod'``.+ """+ code = b"def hello(event, context):\n return {\"ok\": True, \"event\": event}\n"+ buf = io.BytesIO()+ with zipfile.ZipFile(buf, "w") as zf:+ zf.writestr("pkg/sub/mod.py", code)+ lam.create_function(+ FunctionName="slash-handler-fn",+ Runtime="python3.12",+ Role="arn:aws:iam::000000000000:role/test-role",+ Handler="pkg/sub/mod.hello",+ Code={"ZipFile": buf.getvalue()},+ )+ resp = lam.invoke(+ FunctionName="slash-handler-fn",+ Payload=json.dumps({"k": "v"}),+ )+ assert "FunctionError" not in resp, resp+ payload = json.loads(resp["Payload"].read())+ assert payload.get("ok") is True++def test_create_function_missing_runtime_raises(lam):"""Zip deployment without a Runtime should return InvalidParameterValueException."""buf = io.BytesIO()