fix(lambda): omit Environment when no variables are set (#2838)

* 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.
This commit is contained in:
Brian Taylor authored and GitHub committed 2026-09-01 10:56:48 -04:00
1 parent db182b3048
commit 2bf9b57bd9
4 files changed
+108 -16

No files matched your search

@@ -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)