diff --git a/docs/services/lambda.md b/docs/services/lambda.md index 893c55fe7d..c212e161d2 100644 --- a/docs/services/lambda.md +++ b/docs/services/lambda.md @@ -376,6 +376,12 @@ returns the qualified one (`...:function:name:2`), as `PublishVersion` does. Wit both answer for `$LATEST` and create nothing. `UpdateFunctionConfiguration` has no `Publish` parameter in the AWS API and none here. +On a cold launch, `AWS_LAMBDA_FUNCTION_VERSION` contains the executing version: +`$LATEST` for an unqualified invocation and the published version number for a +version or alias invocation. This follows the +[Lambda environment variable contract](https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html#configuration-envvars-runtime) +and was checked against live AWS in `us-east-1` on October 12, 2026 at 19:49 UTC. + `DeleteFunction` honours `Qualifier` — it removes that published version only, leaving `$LATEST`, the other versions and the function's aliases in place; without a qualifier the whole function goes. Matching the live service, deleting `$LATEST` by qualifier and naming an alias are both 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 4db129b97c..38097e4bf1 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 @@ -224,7 +224,6 @@ public class ContainerLauncher implements LambdaRuntimeLauncher { 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_FUNCTION_VERSION=$LATEST"); env.add("AWS_LAMBDA_LOG_GROUP_NAME=" + cwLogGroup); env.add("AWS_LAMBDA_LOG_STREAM_NAME=" + cwLogStream); if (fn.getHandler() != null && !fn.getHandler().isBlank()) { @@ -261,6 +260,9 @@ public class ContainerLauncher implements LambdaRuntimeLauncher { }); } + env.removeIf(value -> value.startsWith("AWS_LAMBDA_FUNCTION_VERSION=")); + env.add("AWS_LAMBDA_FUNCTION_VERSION=" + fn.getVersion()); + ContainerBuilder.Builder specBuilder = containerBuilder.newContainer(image) .withName(containerName) .withEnv(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 5a6aa6338d..08df2bc798 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 @@ -158,6 +158,8 @@ public class KubernetesPodLauncher implements LambdaRuntimeLauncher { LOG.infov("Launching pod for function: {0}", fn.getFunctionName()); RuntimeApiServer runtimeApiServer = runtimeApiServerFactory.create(); + runtimeApiServer.setFunctionMetadata(fn.getFunctionName(), fn.getVersion(), fn.getHandler(), + fn.getAccountId()); // Mirrors ContainerLauncher: any failure after the runtime-api server is allocated // must release its port and delete a half-created pod, or cold-start bursts leak @@ -186,7 +188,6 @@ public class KubernetesPodLauncher implements LambdaRuntimeLauncher { 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_FUNCTION_VERSION=$LATEST"); env.add("AWS_LAMBDA_LOG_GROUP_NAME=" + cwLogGroup); env.add("AWS_LAMBDA_LOG_STREAM_NAME=" + cwLogStream); if (fn.getHandler() != null && !fn.getHandler().isBlank()) { @@ -208,6 +209,9 @@ public class KubernetesPodLauncher implements LambdaRuntimeLauncher { }); } + env.removeIf(value -> value.startsWith("AWS_LAMBDA_FUNCTION_VERSION=")); + env.add("AWS_LAMBDA_FUNCTION_VERSION=" + fn.getVersion()); + LambdaPodSpecFactory.ImageConfig imageConfig = imagePackage ? new LambdaPodSpecFactory.ImageConfig(fn.getImageConfigEntryPoint(), fn.getImageConfigCommand(), fn.getImageConfigWorkingDirectory()) diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaVersionIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaVersionIntegrationTest.java index 7d212eb8f3..791de0012f 100644 --- a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaVersionIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaVersionIntegrationTest.java @@ -151,7 +151,9 @@ class LambdaVersionIntegrationTest { .then() .statusCode(200) .header("X-Amz-Function-Error", nullValue()) - .body("ok", equalTo(true)); + .body("ok", equalTo(true)) + .body("envVersion", equalTo("$LATEST")) + .body("contextVersion", equalTo("$LATEST")); } finally { given().delete(BASE_PATH + "/functions/" + fnName); } @@ -192,6 +194,8 @@ class LambdaVersionIntegrationTest { // Body first: the header assertion below fails fast and would otherwise mask what // the version actually returned. .body("ok", equalTo(true)) + .body("envVersion", equalTo("1")) + .body("contextVersion", equalTo("1")) .header("X-Amz-Function-Error", nullValue()); } finally { given().delete(BASE_PATH + "/functions/" + fnName); @@ -300,7 +304,13 @@ class LambdaVersionIntegrationTest { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (ZipOutputStream zos = new ZipOutputStream(baos)) { zos.putNextEntry(new ZipEntry("index.js")); - zos.write("exports.handler = async () => ({ ok: true });".getBytes()); + zos.write(""" + exports.handler = async (event, context) => ({ + ok: true, + envVersion: process.env.AWS_LAMBDA_FUNCTION_VERSION, + contextVersion: context.functionVersion + }); + """.getBytes()); zos.closeEntry(); } String base64Zip = Base64.getEncoder().encodeToString(baos.toByteArray()); 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 32862dfdfd..4ad459d005 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 @@ -37,6 +37,7 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.io.TempDir; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.params.provider.ValueSource; import org.mockito.ArgumentCaptor; import org.mockito.InOrder; import org.mockito.Mock; @@ -225,6 +226,40 @@ class ContainerLauncherTest { .orElse(null); } + @ParameterizedTest + @ValueSource(strings = {"$LATEST", "1", "7"}) + void launchExposesTheExecutingVersion(String version) { + LambdaFunction fn = new LambdaFunction(); + fn.setFunctionName("version-fn"); + fn.setHotReloadHostPath("/home/ci/code"); + fn.setRuntime("nodejs20.x"); + fn.setHandler("index.handler"); + fn.setVersion(version); + + launcher.launch(fn); + + assertEquals(List.of("AWS_LAMBDA_FUNCTION_VERSION=" + version), + captureRealContainerSpec().env().stream() + .filter(value -> value.startsWith("AWS_LAMBDA_FUNCTION_VERSION=")).toList()); + } + + @Test + void launchPreventsAnEnvironmentOverrideOfTheExecutingVersion() { + LambdaFunction fn = new LambdaFunction(); + fn.setFunctionName("version-fn"); + fn.setHotReloadHostPath("/home/ci/code"); + fn.setRuntime("nodejs20.x"); + fn.setHandler("index.handler"); + fn.setVersion("7"); + fn.setEnvironment(Map.of("AWS_LAMBDA_FUNCTION_VERSION", "user-override")); + + launcher.launch(fn); + + assertEquals(List.of("AWS_LAMBDA_FUNCTION_VERSION=7"), + captureRealContainerSpec().env().stream() + .filter(value -> value.startsWith("AWS_LAMBDA_FUNCTION_VERSION=")).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 836d0bad89..2950d005ac 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 @@ -24,6 +24,8 @@ import org.awaitility.Awaitility; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; @@ -184,6 +186,25 @@ class KubernetesPodLauncherTest { }); } + @ParameterizedTest + @CsvSource({"$LATEST, $$LATEST", "1, 1", "7, 7"}) + void launchExposesTheExecutingVersionInThePodAndRuntimeApi(String version, String podValue) { + markPodPhaseInBackground("floci-lambda-my-fn-", "Running"); + LambdaFunction fn = function(); + fn.setVersion(version); + fn.setEnvironment(Map.of("AWS_LAMBDA_FUNCTION_VERSION", "user-override")); + + ContainerHandle handle = launcher.launch(fn); + + Pod pod = client.pods().inNamespace("default").withName(handle.getContainerId()).get(); + // The pod spec escapes dollars for kubelet expansion; the runtime receives literal $LATEST. + assertThat(pod.getSpec().getContainers().getFirst().getEnv()) + .filteredOn(variable -> "AWS_LAMBDA_FUNCTION_VERSION".equals(variable.getName())) + .extracting(EnvVar::getValue) + .containsExactly(podValue); + verify(runtimeApiServer).setFunctionMetadata("my-fn", version, "index.handler", "000000000000"); + } + @Test void launchPassesTheFunctionsOwningAccountIntoTheBaselineAwsEnv() { // A pod-launched Lambda is subject to the same placeholder-credential bug as a