diff --git a/docs/services/lambda.md b/docs/services/lambda.md index 24353ad408..5515d22c3b 100644 --- a/docs/services/lambda.md +++ b/docs/services/lambda.md @@ -978,6 +978,11 @@ exist, and `AWS_LAMBDA_LOG_GROUP_NAME` names that group. Once a group is named, functions can share, each stream's name carries the function's name, as on AWS: `2026/10/16/my-fn[$LATEST]` rather than the default group's `2026/10/16/[$LATEST]`. +The execution environment gets `AWS_LAMBDA_LOG_FORMAT` from `LogFormat` and, under `LogFormat=JSON`, +`AWS_LAMBDA_LOG_LEVEL` from `ApplicationLogLevel` (`INFO` when unset, the level the API reports). +The runtime interface clients read both: a Python function writes each record as a JSON object, +`extra` keys included, and drops records under the level. + `DurableConfig` requires `ExecutionTimeout` on create. A durable function created without a `Timeout` gets `min(ExecutionTimeout, 900)`. It always logs in JSON format, and `LogFormat: Text` is rejected. On update the members are merged, and a function created diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaController.java b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaController.java index 054cd37bd4..cf36831ec1 100644 --- a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaController.java +++ b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaController.java @@ -888,8 +888,7 @@ public class LambdaController { ObjectNode logging = node.putObject("LoggingConfig"); logging.put("LogFormat", logFormat); if ("JSON".equals(logFormat)) { - logging.put("ApplicationLogLevel", - fn.getApplicationLogLevel() != null ? fn.getApplicationLogLevel() : "INFO"); + logging.put("ApplicationLogLevel", fn.applicationLogLevelOrDefault()); logging.put("SystemLogLevel", fn.getSystemLogLevel() != null ? fn.getSystemLogLevel() : "INFO"); } diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncher.java b/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncher.java index 592480650a..fffd566e4b 100644 --- a/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncher.java +++ b/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncher.java @@ -246,17 +246,7 @@ public class ContainerLauncher implements LambdaRuntimeLauncher { String lambdaRegion = extractRegionFromArn(fn.getFunctionArn(), config.defaultRegion()); String lambdaAccountId = AwsArnUtils.accountOrDefault(fn.getFunctionArn(), config.defaultAccountId()); - // Build env vars - List env = new ArrayList<>(); - env.add("AWS_LAMBDA_RUNTIME_API=" + runtimeApiEndpoint); - env.add("AWS_LAMBDA_FUNCTION_NAME=" + fn.getFunctionName()); - env.add("AWS_LAMBDA_FUNCTION_MEMORY_SIZE=" + fn.getMemorySize()); - env.add("AWS_LAMBDA_FUNCTION_TIMEOUT=" + fn.getTimeout()); - env.add("AWS_LAMBDA_LOG_GROUP_NAME=" + cwLogGroup); - env.add("AWS_LAMBDA_LOG_STREAM_NAME=" + cwLogStream); - if (fn.getHandler() != null && !fn.getHandler().isBlank()) { - env.add("_HANDLER=" + fn.getHandler()); - } + List env = LambdaRuntimeEnv.of(fn, runtimeApiEndpoint, cwLogGroup, cwLogStream); // Region, credentials and the Floci endpoint the SDK should target: the same baseline // Floci gives every launched container. When the host ~/.aws is mounted (awsConfigPath), // the SDK discovers credentials from /opt/aws-config instead of injected credentials. diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/LambdaRuntimeEnv.java b/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/LambdaRuntimeEnv.java new file mode 100644 index 0000000000..36001339e0 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/LambdaRuntimeEnv.java @@ -0,0 +1,36 @@ +package io.github.hectorvent.floci.services.lambda.launcher; + +import io.github.hectorvent.floci.services.lambda.model.LambdaFunction; + +import java.util.ArrayList; +import java.util.List; + +/** The Lambda runtime environment every launcher gives an execution environment, before the SDK baseline. */ +public final class LambdaRuntimeEnv { + + private LambdaRuntimeEnv() { + } + + /** Returns a mutable list, so a launcher can add or override variables after it. */ + public static List of(LambdaFunction fn, String runtimeApi, String logGroup, String logStream) { + List env = new ArrayList<>(); + env.add("AWS_LAMBDA_RUNTIME_API=" + runtimeApi); + env.add("AWS_LAMBDA_FUNCTION_NAME=" + fn.getFunctionName()); + env.add("AWS_LAMBDA_FUNCTION_MEMORY_SIZE=" + fn.getMemorySize()); + env.add("AWS_LAMBDA_FUNCTION_TIMEOUT=" + fn.getTimeout()); + env.add("AWS_LAMBDA_LOG_GROUP_NAME=" + logGroup); + env.add("AWS_LAMBDA_LOG_STREAM_NAME=" + logStream); + // LoggingConfig: the runtime interface clients read these to pick the log format and the + // application log level, so without them a JSON function still logs text. + if (fn.getLogFormat() != null) { + env.add("AWS_LAMBDA_LOG_FORMAT=" + fn.getLogFormat()); + } + if (fn.applicationLogLevelOrDefault() != null) { + env.add("AWS_LAMBDA_LOG_LEVEL=" + fn.applicationLogLevelOrDefault()); + } + if (fn.getHandler() != null && !fn.getHandler().isBlank()) { + env.add("_HANDLER=" + fn.getHandler()); + } + return env; + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/kubernetes/KubernetesPodLauncher.java b/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/kubernetes/KubernetesPodLauncher.java index d2cc595052..b8321229e0 100644 --- a/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/kubernetes/KubernetesPodLauncher.java +++ b/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/kubernetes/KubernetesPodLauncher.java @@ -14,6 +14,7 @@ import io.github.hectorvent.floci.services.lambda.LambdaService; import io.github.hectorvent.floci.services.lambda.launcher.ContainerHandle; import io.github.hectorvent.floci.services.lambda.launcher.ContainerLauncher; import io.github.hectorvent.floci.services.lambda.launcher.ImageResolver; +import io.github.hectorvent.floci.services.lambda.launcher.LambdaRuntimeEnv; import io.github.hectorvent.floci.services.lambda.launcher.LambdaRuntimeLauncher; import io.github.hectorvent.floci.services.lambda.model.ContainerState; import io.github.hectorvent.floci.services.lambda.model.LambdaFunction; @@ -184,16 +185,8 @@ public class KubernetesPodLauncher implements LambdaRuntimeLauncher { Optional caConfigMap = ContainerCaBundle.hostPath(config).map(bundle -> ensureCaConfigMap(namespace, bundle)); - ArrayList env = new ArrayList<>(); - env.add("AWS_LAMBDA_RUNTIME_API=" + addressResolver.resolve() + ":" + runtimeApiServer.getPort()); - env.add("AWS_LAMBDA_FUNCTION_NAME=" + fn.getFunctionName()); - env.add("AWS_LAMBDA_FUNCTION_MEMORY_SIZE=" + fn.getMemorySize()); - env.add("AWS_LAMBDA_FUNCTION_TIMEOUT=" + fn.getTimeout()); - env.add("AWS_LAMBDA_LOG_GROUP_NAME=" + cwLogGroup); - env.add("AWS_LAMBDA_LOG_STREAM_NAME=" + cwLogStream); - if (fn.getHandler() != null && !fn.getHandler().isBlank()) { - env.add("_HANDLER=" + fn.getHandler()); - } + List env = LambdaRuntimeEnv.of(fn, + addressResolver.resolve() + ":" + runtimeApiServer.getPort(), cwLogGroup, cwLogStream); env.addAll(awsEnv.sdkBaselineEnv(region, Optional.empty(), addressResolver.flociBaseUrl(), Optional.empty(), AwsArnUtils.accountOrDefault(fn.getFunctionArn(), config.defaultAccountId()))); if (fn.getEnvironment() != null) { diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/model/LambdaFunction.java b/src/main/java/io/github/hectorvent/floci/services/lambda/model/LambdaFunction.java index 2c3df4150f..b7966b5da4 100644 --- a/src/main/java/io/github/hectorvent/floci/services/lambda/model/LambdaFunction.java +++ b/src/main/java/io/github/hectorvent/floci/services/lambda/model/LambdaFunction.java @@ -205,6 +205,17 @@ public class LambdaFunction { public String getApplicationLogLevel() { return applicationLogLevel; } public void setApplicationLogLevel(String applicationLogLevel) { this.applicationLogLevel = applicationLogLevel; } + /** + * The level a JSON-format function logs at: its own, else INFO, as Lambda reports it. None under + * Text, where log levels are refused. + */ + public String applicationLogLevelOrDefault() { + if (applicationLogLevel != null) { + return applicationLogLevel; + } + return "JSON".equals(logFormat) ? "INFO" : null; + } + public String getSystemLogLevel() { return systemLogLevel; } public void setSystemLogLevel(String systemLogLevel) { this.systemLogLevel = systemLogLevel; } diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncherTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncherTest.java index 9dd45cb860..f8e63c9624 100644 --- a/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncherTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncherTest.java @@ -333,6 +333,30 @@ class ContainerLauncherTest { .filter(value -> value.startsWith("AWS_LAMBDA_FUNCTION_VERSION=")).toList()); } + @ParameterizedTest + @CsvSource({"JSON, DEBUG, DEBUG", "JSON, , INFO", "Text, , "}) + void launchPassesTheLoggingConfigToTheRuntime(String logFormat, String level, String runtimeLevel) { + LambdaFunction fn = new LambdaFunction(); + fn.setFunctionName("logging-config-fn"); + fn.setHotReloadHostPath("/home/ci/code"); + fn.setRuntime("python3.13"); + fn.setHandler("fn.handler"); + fn.setLogFormat(logFormat); + fn.setApplicationLogLevel(level); + + launcher.launch(fn); + + List expected = new ArrayList<>(List.of("AWS_LAMBDA_LOG_FORMAT=" + logFormat)); + if (runtimeLevel != null) { + expected.add("AWS_LAMBDA_LOG_LEVEL=" + runtimeLevel); + } + assertEquals(expected, + captureRealContainerSpec().env().stream() + .filter(value -> value.startsWith("AWS_LAMBDA_LOG_FORMAT=") + || value.startsWith("AWS_LAMBDA_LOG_LEVEL=")) + .toList()); + } + @Test void launchFunction_hotReloadMountsTheHostDirectoryReadOnlyAtVarTask() { LambdaFunction fn = new LambdaFunction(); diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/kubernetes/KubernetesPodLauncherTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/kubernetes/KubernetesPodLauncherTest.java index 7ba31a4d57..3e8d1baca8 100644 --- a/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/kubernetes/KubernetesPodLauncherTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/kubernetes/KubernetesPodLauncherTest.java @@ -247,6 +247,23 @@ class KubernetesPodLauncherTest { verify(runtimeApiServer).setFunctionMetadata("my-fn", version, "index.handler", "000000000000"); } + @Test + void launchPassesTheLoggingConfigToTheRuntime() { + markPodPhaseInBackground("floci-lambda-my-fn-", "Running"); + LambdaFunction fn = function(); + fn.setLogFormat("JSON"); + fn.setApplicationLogLevel("DEBUG"); + + ContainerHandle handle = launcher.launch(fn); + + Pod pod = client.pods().inNamespace("default").withName(handle.getContainerId()).get(); + assertThat(pod.getSpec().getContainers().getFirst().getEnv()) + .filteredOn(variable -> "AWS_LAMBDA_LOG_FORMAT".equals(variable.getName()) + || "AWS_LAMBDA_LOG_LEVEL".equals(variable.getName())) + .extracting(EnvVar::getValue) + .containsExactly("JSON", "DEBUG"); + } + @Test void launchPassesTheFunctionsOwningAccountIntoTheBaselineAwsEnv() { // A pod-launched Lambda is subject to the same placeholder-credential bug as a