Skip to content

Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.

fix(lambda): set standard runtime env before handler module load (#190)

Luke Bondeabca13d0437
diff --git a/README.md b/README.md
index e3eb6d13..861cb8cd 100644
--- a/README.md
+++ b/README.md
@@ -570,6 +570,8 @@ print(json.loads(resp["Payload"].read())) # {'statusCode': 200, 'body': '{"hell
Layers that ship npm packages work too — MiniStack resolves the `nodejs/node_modules` subdirectory inside each layer zip and prepends it to the module search path.
+MiniStack also sets the standard Lambda runtime environment before the handler module is loaded, including `LAMBDA_TASK_ROOT`, `AWS_LAMBDA_FUNCTION_NAME`, `AWS_LAMBDA_FUNCTION_MEMORY_SIZE`, and `_LAMBDA_FUNCTION_ARN`. That keeps import-time Lambda detection and conditional handler setup aligned with AWS warm runtime behaviour.
+
---
## Architecture
diff --git a/ministack/core/lambda_runtime.py b/ministack/core/lambda_runtime.py
index 09a95886..37e4f69f 100644
--- a/ministack/core/lambda_runtime.py
+++ b/ministack/core/lambda_runtime.py
@@ -182,6 +182,10 @@ rl.on("line", async (line) => {
if (lineNum === 1) {
const { code_dir, module: modPath, handler: handlerName, env } = msg;
Object.assign(process.env, env || {});
+ process.env.LAMBDA_TASK_ROOT = code_dir;
+ process.env.AWS_LAMBDA_FUNCTION_NAME = msg.function_name || process.env.AWS_LAMBDA_FUNCTION_NAME || "";
+ process.env.AWS_LAMBDA_FUNCTION_MEMORY_SIZE = String(msg.memory || process.env.AWS_LAMBDA_FUNCTION_MEMORY_SIZE || "128");
+ process.env._LAMBDA_FUNCTION_ARN = msg.arn || process.env._LAMBDA_FUNCTION_ARN || "";
patchAwsSdk();
try {
const fullPath = path.resolve(code_dir, modPath);
@@ -314,6 +318,10 @@ class Worker:
module_name, handler_name = handler.rsplit(".", 1)
env_vars = self.config.get("Environment", {}).get("Variables", {})
spawn_env = {**os.environ, **env_vars}
+ spawn_env.setdefault("LAMBDA_TASK_ROOT", code_dir)
+ spawn_env.setdefault("AWS_LAMBDA_FUNCTION_NAME", self.config.get("FunctionName", ""))
+ spawn_env.setdefault("AWS_LAMBDA_FUNCTION_MEMORY_SIZE", str(self.config.get("MemorySize", 128)))
+ spawn_env.setdefault("_LAMBDA_FUNCTION_ARN", self.config.get("FunctionArn", ""))
self._proc = subprocess.Popen(
[binary, worker_path],