From 2bf9b57bd9210198c9e0396bf1c3f96f33943733 Mon Sep 17 00:00:00 2001 From: Brian Taylor <82356519+bsaurusrex@users.noreply.github.com> Date: Tue, 1 Sep 2026 22:56:48 +0800 Subject: [PATCH] fix(lambda): omit Environment when no variables are set (#2838) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(lambda): omit Environment when no variables are set Real AWS omits the Environment member entirely from CreateFunction, GetFunction, GetFunctionConfiguration and ListFunctions responses when a function has no environment variables. Floci always emitted it, as an empty object. The Terraform provider reads a present-but-empty Environment back as an environment {} block in state, so any function declared without variables plans a removal on every run and never converges. On Lambda@Edge, where environment variables are not permitted at all, the block can never legitimately exist and each no-op apply also republishes the function. Layers, KMSKeyArn, FileSystemConfigs and VpcConfig in the same response builder already omit-when-empty; Environment was the outlier. * test(compat): correct Environment assertions to match live AWS Three assertions in the Python and Java SDK compatibility suites required an Environment block that real AWS does not send: - test_get_function_configuration_environment_always_present - test_update_environment_variables_round_trip (the cleared case) - LambdaFunctionConfigTest.getFunctionConfigurationHasDefaults Measured against the live service in both directions: a function created without variables comes back with no Environment member, and clearing to an empty Variables map removes it again. The tests encoded Floci's previous behaviour rather than the API's, which is why they went green. Also corrects the wording of the Java clear case. It sends an Environment with no Variables member, which is 'leave unchanged', not 'clear' — live AWS keeps the existing variables, as Floci already did. The assertion now pins that instead of describing a clear that never happened. --- .../floci/test/LambdaFunctionConfigTest.java | 24 +++++-- .../tests/test_lambda_function_config.py | 24 +++++-- .../services/lambda/LambdaController.java | 8 ++- .../lambda/LambdaIntegrationTest.java | 68 +++++++++++++++++++ 4 files changed, 108 insertions(+), 16 deletions(-) diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/LambdaFunctionConfigTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/LambdaFunctionConfigTest.java index c13dbdd953..10e6ea77d9 100644 --- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/LambdaFunctionConfigTest.java +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/LambdaFunctionConfigTest.java @@ -102,10 +102,13 @@ class LambdaFunctionConfigTest { .as("default TracingConfig.Mode must be PassThrough") .isEqualTo("PassThrough"); - // Environment block must always be present (even when empty) + // AWS omits Environment entirely for a function with no variables — verified + // against the live service — so the SDK deserialises it as null here, the same + // as layers() and vpcConfig(). Returning an empty block instead gives Terraform + // a permanent `- environment {}` diff. assertThat(resp.environment()) - .as("Environment must always be present in the response") - .isNotNull(); + .as("Environment must be omitted when no variables are set") + .isNull(); } // ─── UpdateFunctionConfiguration ───────────────────────────────────────── @@ -207,16 +210,23 @@ class LambdaFunctionConfigTest { .containsEntry("KEY_A", "value-a") .containsEntry("KEY_B", "value-b"); - // Clear environment — response must still include the Environment block - UpdateFunctionConfigurationResponse cleared = lambda.updateFunctionConfiguration( + // An Environment with no Variables member is "leave unchanged", not "clear" — the + // existing variables survive. Verified against the live service: sending + // Environment={} keeps {"KEY_A": "value-a"} on both the update response and the + // following read. (Clearing takes an empty Variables map, which AWS answers by + // omitting the member entirely.) + UpdateFunctionConfigurationResponse unchanged = lambda.updateFunctionConfiguration( UpdateFunctionConfigurationRequest.builder() .functionName(FN) .environment(Environment.builder().build()) .build()); - assertThat(cleared.environment()) - .as("Environment block must be present even after clearing variables") + assertThat(unchanged.environment()) + .as("an Environment with no Variables member must leave the variables alone") .isNotNull(); + assertThat(unchanged.environment().variables()) + .containsEntry("KEY_A", "value-a") + .containsEntry("KEY_B", "value-b"); } @Test diff --git a/compatibility-tests/sdk-test-python/tests/test_lambda_function_config.py b/compatibility-tests/sdk-test-python/tests/test_lambda_function_config.py index 8cf5f30505..6fa60befeb 100644 --- a/compatibility-tests/sdk-test-python/tests/test_lambda_function_config.py +++ b/compatibility-tests/sdk-test-python/tests/test_lambda_function_config.py @@ -114,9 +114,16 @@ class TestFunctionConfigurationDefaults: finally: lambda_client.delete_function(FunctionName=fn) - def test_get_function_configuration_environment_always_present( + def test_get_function_configuration_omits_environment_when_unset( self, lambda_client, minimal_lambda_zip, unique_name ): + """AWS omits Environment entirely for a function with no variables. + + Verified against lambda.us-east-1.amazonaws.com and + lambda.ap-southeast-1.amazonaws.com: a function created without + --environment comes back with no Environment key at all, the same way + Layers and VpcConfig are omitted rather than returned empty. + """ fn = f"pytest-cfg-{unique_name}" try: lambda_client.create_function( @@ -127,8 +134,9 @@ class TestFunctionConfigurationDefaults: Code={"ZipFile": minimal_lambda_zip}, ) resp = lambda_client.get_function_configuration(FunctionName=fn) - assert "Environment" in resp, ( - "Environment block must always be present in GetFunctionConfiguration response" + assert "Environment" not in resp, ( + "Environment must be omitted when no variables are set; an empty " + "block gives Terraform a permanent `- environment {}` diff" ) finally: lambda_client.delete_function(FunctionName=fn) @@ -246,13 +254,17 @@ class TestUpdateFunctionConfiguration: assert variables.get("KEY_A") == "value-a" assert variables.get("KEY_B") == "value-b" - # Clear environment — block must still be present + # Clear environment — AWS drops the block again. Measured live: + # update_function_configuration(Environment={"Variables": {}}) returns + # no Environment key, and the following get_function_configuration + # agrees, so a cleared function is indistinguishable from one that + # never had variables. cleared = lambda_client.update_function_configuration( FunctionName=fn, Environment={"Variables": {}}, ) - assert "Environment" in cleared, ( - "Environment block must be present even after clearing variables" + assert "Environment" not in cleared, ( + "Environment must be omitted once variables are cleared" ) finally: lambda_client.delete_function(FunctionName=fn) 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 134dbfdc19..d45c6f7359 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 @@ -672,10 +672,12 @@ public class LambdaController { .put("LocalMountPath", fileSystem.getLocalMountPath())); } - // Environment — always present (SDK expects it even when empty) - ObjectNode envNode = node.putObject("Environment"); + // Environment — omitted entirely when no variables are set, as AWS does. An empty + // object is not the same answer as absence: the Terraform provider reads one back as + // an `environment {}` block in state, so a function declared without variables plans a + // removal on every run. Same rule as Layers, KMSKeyArn and VpcConfig above. if (fn.getEnvironment() != null && !fn.getEnvironment().isEmpty()) { - ObjectNode vars = envNode.putObject("Variables"); + ObjectNode vars = node.putObject("Environment").putObject("Variables"); fn.getEnvironment().forEach(vars::put); } diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaIntegrationTest.java index f2921ea08b..96e0c97e12 100644 --- a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaIntegrationTest.java @@ -551,4 +551,72 @@ class LambdaIntegrationTest { .then() .statusCode(404); } + + // ── Environment is omitted, not empty, when no variables are set ── + + @Test + @Order(41) + void environmentMemberIsAbsentUntilVariablesAreSet() { + String fn = "env-absent-fn"; + given() + .contentType("application/json") + .body(""" + { + "FunctionName": "env-absent-fn", + "Runtime": "nodejs20.x", + "Role": "arn:aws:iam::000000000000:role/lambda-role", + "Handler": "index.handler" + } + """) + .when() + .post(BASE_PATH + "/functions") + .then() + .statusCode(201) + .body("$", not(hasKey("Environment"))); + + given() + .when() + .get(BASE_PATH + "/functions/" + fn + "/configuration") + .then() + .statusCode(200) + .body("$", not(hasKey("Environment"))); + + given() + .when() + .get(BASE_PATH + "/functions/" + fn) + .then() + .statusCode(200) + .body("Configuration", not(hasKey("Environment"))); + + // Setting a variable brings the member back... + given() + .contentType("application/json") + .body(""" + { "Environment": { "Variables": { "FOO": "bar" } } } + """) + .when() + .put(BASE_PATH + "/functions/" + fn + "/configuration") + .then() + .statusCode(200) + .body("Environment.Variables.FOO", equalTo("bar")); + + // ...and clearing it to an empty map removes it again, which is what AWS + // answers for a cleared function, not "Environment": {} or an empty Variables map. + given() + .contentType("application/json") + .body(""" + { "Environment": { "Variables": {} } } + """) + .when() + .put(BASE_PATH + "/functions/" + fn + "/configuration") + .then() + .statusCode(200) + .body("$", not(hasKey("Environment"))); + + given() + .when() + .delete(BASE_PATH + "/functions/" + fn) + .then() + .statusCode(anyOf(is(200), is(204))); + } }