Skip to content

Commit d6dd716

Browse files
committed
fix: defaulting to process.cwd() when LAMBDA_TASK_ROOT is not found
1 parent 9ab0fac commit d6dd716

2 files changed

Lines changed: 14 additions & 10 deletions

File tree

src/utils/runtime-setup.test.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,15 @@ describe("runtime-setup", () => {
6868
);
6969
});
7070

71-
it("should throw if LAMBDA_TASK_ROOT is not set", async () => {
71+
it("should fall back to process.cmd() when LAMBDA_TASK_ROOT is not set", async () => {
7272
// GIVEN
7373
delete process.env.LAMBDA_TASK_ROOT;
7474

7575
// WHEN & THEN
76-
await expect(createRuntime()).rejects.toThrow(
77-
"LAMBDA_TASK_ROOT environment variable is not set",
76+
await createRuntime();
77+
expect(UserFunctionLoader.load).toHaveBeenCalledWith(
78+
process.cwd(),
79+
expect.any(String),
7880
);
7981
});
8082
});
@@ -150,7 +152,7 @@ describe("runtime-setup", () => {
150152
// GIVEN
151153
process.env.AWS_LAMBDA_RUNTIME_API = "test-api:8080";
152154
process.env._HANDLER = "index.handler";
153-
process.env.LAMBDA_TASK_ROOT = "/test/path";
155+
process.env.LAMBDA_TASK_ROOT = "/var/task";
154156

155157
// WHEN
156158
await createRuntime();
@@ -167,7 +169,7 @@ describe("runtime-setup", () => {
167169
// GIVEN
168170
process.env.AWS_LAMBDA_RUNTIME_API = "test-api:8080";
169171
process.env._HANDLER = "index.handler";
170-
process.env.LAMBDA_TASK_ROOT = "/test/path";
172+
process.env.LAMBDA_TASK_ROOT = "/var/task";
171173
process.env.AWS_LAMBDA_LOG_FORMAT = "JSON";
172174

173175
// WHEN
@@ -184,7 +186,7 @@ describe("runtime-setup", () => {
184186
// GIVEN
185187
process.env.AWS_LAMBDA_RUNTIME_API = "test-api:8080";
186188
process.env._HANDLER = "index.handler";
187-
process.env.LAMBDA_TASK_ROOT = "/test/path";
189+
process.env.LAMBDA_TASK_ROOT = "/var/task";
188190
process.env.AWS_LAMBDA_LOG_LEVEL = "ERROR";
189191

190192
// WHEN

src/utils/runtime-setup.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ export async function createRuntime(
1818
const isMultiConcurrent = isMultiConcurrentMode();
1919
const runtimeApi = process.env.AWS_LAMBDA_RUNTIME_API;
2020
const handlerString = process.env._HANDLER;
21-
const taskRoot = process.env.LAMBDA_TASK_ROOT;
21+
// LAMBDA_TASK_ROOT is a restricted environment variable set to /var/task.
22+
// If we set it as required we are forcing customers to set it to a value that
23+
// can't change.
24+
// We fall back to process.cwd() to allow OCI customers to place the handler wherever
25+
// they want.
26+
const taskRoot = process.env.LAMBDA_TASK_ROOT || process.cwd();
2227

2328
if (!runtimeApi) {
2429
throw new PlatformError(
@@ -28,9 +33,6 @@ export async function createRuntime(
2833
if (!handlerString) {
2934
throw new PlatformError("_HANDLER environment variable is not set");
3035
}
31-
if (!taskRoot) {
32-
throw new PlatformError("LAMBDA_TASK_ROOT environment variable is not set");
33-
}
3436

3537
const rapidClient = await RAPIDClient.create(
3638
runtimeApi,

0 commit comments

Comments
 (0)