Skip to content

Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.

fix(lambda): publishing on create or code update leaves $LATEST unpublished (#2157)

Jeff Heller40912a5dbc83
diff --git a/CHANGELOG.md b/CHANGELOG.md
index ce9fdcb6..41226691 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,8 @@ Versioning follows [Semantic Versioning](https://semver.org/).
## [Unreleased]
+### Fixed
+- **Lambda — publishing on create or code update leaves `$LATEST` unpublished** — `CreateFunction` and `UpdateFunctionCode` with `Publish: true` overwrote `$LATEST`'s `Version` with the new version number. `ListVersionsByFunction` then listed that number twice and no `$LATEST`, `GetFunctionConfiguration` without a qualifier reported it, and invoking `$LATEST` set `AWS_LAMBDA_FUNCTION_VERSION` to it. `$LATEST` now stays `$LATEST`, as on AWS; the create and update responses are unchanged.
### Added
- **SSM — service settings** — `GetServiceSetting`, `UpdateServiceSetting` and `ResetServiceSetting`, per account and region, persisted. `SettingValue` is validated for the ids whose accepted values AWS documents, and `/ssm/parameter-store/default-parameter-tier` sets the `Tier` of a `PutParameter` that names none. Unblocks Terraform's `aws_ssm_service_setting`.
diff --git a/ministack/services/lambda_svc.py b/ministack/services/lambda_svc.py
index 3a1d4533..fd729211 100644
--- a/ministack/services/lambda_svc.py
+++ b/ministack/services/lambda_svc.py
@@ -2416,6 +2416,8 @@ def _create_function(data: dict):
"provisioned_concurrency": {},
}
+ # Publishing leaves $LATEST unpublished; the response carries the published number.
+ response = config
if data.get("Publish"):
ver_num = _functions[name]["next_version"]
_functions[name]["next_version"] = ver_num + 1
@@ -2428,12 +2430,12 @@ def _create_function(data: dict):
_functions[name]["versions"][str(ver_num)] = ver_record
if _stamp_snapstart_published_version(ver_config):
_snapstart_provision_version_async(name, ver_record)
- config["Version"] = str(ver_num)
+ response = {**config, "Version": str(ver_num)}
_schedule_state_transition(name, _LAMBDA_STATE_TRANSITION_DELAY)
runtime_or_image = config.get("Runtime") or ("image=" + image_uri if image_uri else "?")
logger.info("Lambda function created: %s (%s)", name, runtime_or_image)
- return json_response(config, 201)
+ return json_response(response, 201)
def _get_function(name: str, qualifier: str | None = None):
@@ -2892,7 +2894,7 @@ def _update_code(name: str, data: dict):
func["versions"][str(ver_num)] = ver_record
if _stamp_snapstart_published_version(ver_config):
_snapstart_provision_version_async(name, ver_record)
- func["config"]["Version"] = str(ver_num)
+ return json_response({**func["config"], "Version": str(ver_num)})
return json_response(func["config"])
diff --git a/tests/test_lambda.py b/tests/test_lambda.py
index 3a73a00c..c6e742e0 100644
--- a/tests/test_lambda.py
+++ b/tests/test_lambda.py
@@ -2143,6 +2143,22 @@ def test_lambda_update_code_publish_version(lam):
versions = [v["Version"] for v in resp["Versions"] if v["Version"] != "$LATEST"]
assert len(versions) >= 1
+def test_lambda_publish_on_create_and_update_leaves_latest_unpublished(lam):
+ name = "lam-publish-leaves-latest"
+ created = lam.create_function(
+ FunctionName=name,
+ Runtime="python3.11",
+ Role=_LAMBDA_ROLE,
+ Handler="index.handler",
+ Code={"ZipFile": _make_zip("def handler(event, context): return 1")},
+ Publish=True,
+ )
+ assert created["Version"] == "1"
+ lam.update_function_code(FunctionName=name, ZipFile=_make_zip("def handler(event, context): return 2"), Publish=True)
+ versions = [v["Version"] for v in lam.list_versions_by_function(FunctionName=name)["Versions"]]
+ assert versions == ["$LATEST", "1", "2"]
+ assert lam.get_function_configuration(FunctionName=name)["Version"] == "$LATEST"
+
def test_lambda_nodejs_promise_handler(lam):
code = (
"exports.handler = (event) => Promise.resolve({ promise: true, val: event.x });"