Skip to content

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

fix(lambda): enforce RevisionId on function and alias updates (#2228)

Kinon0da8b2c950a6
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 7f325333..3f8ad0a9 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -9,6 +9,7 @@ Versioning follows [Semantic Versioning](https://semver.org/).
### Fixed
+- **Lambda — conditional function and alias updates** — `UpdateFunctionCode`, `UpdateFunctionConfiguration`, and `UpdateAlias` reject a stale `RevisionId` with `PreconditionFailedException` (412) before changing state or invalidating workers. Competing updates to the same revision are serialized per function or alias, while unrelated resources remain independent.
- **SQS — empty policy updates** — `SetQueueAttributes` removes `Policy`, `RedrivePolicy`, and `RedriveAllowPolicy` when explicitly set to an empty string, so Terraform policy deletion succeeds and subsequent attribute reads omit the removed policy. Invalid nonempty redrive policies and numeric attributes are still rejected before any attributes change.
- **AppSync Events event payloads**: deliver each WebSocket data frame with a string `event` field, matching observed AWS behavior. HTTP and WebSocket publish batches still produce one frame per event.
- **CloudFront — function status follows publish and association** — `DescribeFunction` and `ListFunctions` reported the `DEVELOPMENT` stage as `UNPUBLISHED` even after `PublishFunction`, so Terraform's `aws_cloudfront_function.status`, which reads that stage, never left `UNPUBLISHED`; once the current development version is published it now reports `UNASSOCIATED`, or `ASSOCIATED` while a distribution's cache behavior references the function, and an `UpdateFunction` makes it `UNPUBLISHED` again until the next publish. Contributed by @fabio-andre-rodrigues.
diff --git a/ministack/services/lambda_svc.py b/ministack/services/lambda_svc.py
index 11534f63..5bb71f26 100644
--- a/ministack/services/lambda_svc.py
+++ b/ministack/services/lambda_svc.py
@@ -44,8 +44,10 @@ import threading
import time
import urllib.error
import urllib.request
+import weakref
import zipfile
from datetime import datetime, timezone
+from functools import wraps
from typing import Any
from urllib.parse import quote, unquote
@@ -2767,6 +2769,46 @@ def _delete_function(name: str, query_params: dict, path_qualifier: str | None =
return 204, {}, b""
+# Code and configuration share a revision; each alias has its own. Only
+# registry lookup is global. Weak entries disappear once all active callers
+# release their strong references, without persisting locks in function state.
+_revision_locks = weakref.WeakValueDictionary()
+_revision_locks_guard = threading.Lock()
+
+
+def _serialized_revision_update(*, alias=False):
+ def decorate(operation):
+ @wraps(operation)
+ def guarded(*args, **kwargs):
+ name = args[0] if args else kwargs["func_name" if alias else "name"]
+ alias_name = (args[1] if len(args) > 1 else kwargs["alias_name"]) if alias else None
+ key = (get_account_id(), get_region(), name, alias_name)
+ with _revision_locks_guard:
+ lock = _revision_locks.get(key)
+ if lock is None:
+ lock = threading.RLock()
+ _revision_locks[key] = lock
+ # Keep this reference while waiting as well as while executing:
+ # a waiter must never acquire a replacement lock for the same key.
+ # Reentrant acquisition cannot deadlock a same-thread nested call.
+ with lock:
+ return operation(*args, **kwargs)
+ return guarded
+ return decorate
+
+
+def _check_revision(current: dict, data: dict):
+ if "RevisionId" in data and data["RevisionId"] != current.get("RevisionId"):
+ logger.debug("Rejected Lambda update with a stale RevisionId")
+ return error_response_json(
+ "PreconditionFailedException",
+ "The RevisionId provided does not match the latest RevisionId.",
+ 412,
+ )
+ return None
+
+
+@_serialized_revision_update()
def _update_code(name: str, data: dict):
if name not in _functions:
return error_response_json(
@@ -2775,6 +2817,9 @@ def _update_code(name: str, data: dict):
404,
)
func = _functions[name]
+ err = _check_revision(func["config"], data)
+ if err is not None:
+ return err
code_zip = None
# UpdateFunctionCode is where AWS lets the architecture change (the
# UpdateFunctionConfiguration model has no Architectures member).
@@ -2859,6 +2904,7 @@ def _update_code(name: str, data: dict):
return json_response(func["config"])
+@_serialized_revision_update()
def _update_config(name: str, data: dict):
if name not in _functions:
return error_response_json(
@@ -2866,6 +2912,9 @@ def _update_config(name: str, data: dict):
f"Function not found: {_func_arn(name)}",
404,
)
+ err = _check_revision(_functions[name]["config"], data)
+ if err is not None:
+ return err
err = _validate_dead_letter_config(data.get("DeadLetterConfig"))
if err:
return err
@@ -5973,6 +6022,7 @@ def _get_alias(func_name: str, alias_name: str):
return json_response(alias)
+@_serialized_revision_update(alias=True)
def _update_alias(func_name: str, alias_name: str, data: dict):
if func_name not in _functions:
return error_response_json(
@@ -5987,6 +6037,9 @@ def _update_alias(func_name: str, alias_name: str, data: dict):
f"Alias not found: {_func_arn(func_name)}:{alias_name}",
404,
)
+ err = _check_revision(alias, data)
+ if err is not None:
+ return err
for key in ("FunctionVersion", "Description"):
if key in data:
alias[key] = data[key]
diff --git a/tests/test_lambda_revision.py b/tests/test_lambda_revision.py
new file mode 100644
index 00000000..15090ced
--- /dev/null
+++ b/tests/test_lambda_revision.py
@@ -0,0 +1,186 @@
+# Copyright (c) 2026 MiniStack Contributors. SPDX-License-Identifier: MIT
+"""Conditional Lambda updates reject stale revisions before any side effects."""
+
+import base64
+import copy
+import json
+import threading
+from concurrent.futures import ThreadPoolExecutor
+from unittest.mock import Mock
+
+import pytest
+
+from ministack.core.responses import AccountRegionScopedDict, request_scope
+from ministack.services import lambda_svc as svc
+
+
+def functions(monkeypatch):
+ store = AccountRegionScopedDict()
+ for name in ("revision-test", "unrelated"):
+ store[name] = {
+ "config": {"RevisionId": "function-before", "Architectures": ["x86_64"],
+ "Description": "original", "Version": "$LATEST"},
+ "code_zip": b"original-code", "versions": {}, "next_version": 1,
+ "aliases": {alias: {"RevisionId": "alias-before", "FunctionVersion": "1",
+ "Description": "original"} for alias in ("live", "other")},
+ }
+ monkeypatch.setattr(svc, "_functions", store)
+ for helper in ("invalidate_worker", "_pool_kill_function", "_sweep_extract_cache",
+ "_schedule_state_transition", "_snapstart_provision_version_async"):
+ monkeypatch.setattr(svc, helper, Mock())
+ return store
+
+
+def _update(operation, revision="function-before", value="changed", name="revision-test"):
+ data = {"RevisionId": revision} if revision is not None else {}
+ if operation == "code":
+ return svc._update_code(name, {**data, "ZipFile": base64.b64encode(value.encode()).decode()})
+ if operation == "config":
+ return svc._update_config(name, {**data, "Description": value})
+ return svc._update_alias(name, "live", {**data, "Description": value, "FunctionVersion": "2"})
+
+
[email protected]("operation", ["code", "config", "alias"])
+def test_stale_revision_rejects_before_mutation_or_side_effects(functions, monkeypatch, operation):
+ fetch = Mock(side_effect=AssertionError("stale request fetched code"))
+ monkeypatch.setattr(svc, "_fetch_code_from_s3", fetch)
+ before = copy.deepcopy(functions._data)
+ if operation == "code":
+ result = svc._update_code("revision-test", {
+ "RevisionId": "stale", "S3Bucket": "unused", "S3Key": "unused",
+ "Architectures": ["arm64"], "Publish": True,
+ "S3ObjectStorageMode": "CustomerManaged", "SourceKMSKeyArn": "changed",
+ })
+ elif operation == "config":
+ result = svc._update_config("revision-test", {
+ "RevisionId": "stale", "Description": "changed", "MemorySize": 512,
+ "Environment": {"Variables": {"CHANGED": "true"}},
+ })
+ else:
+ result = svc._update_alias("revision-test", "live", {
+ "RevisionId": "stale", "FunctionVersion": "2", "Description": "changed",
+ "RoutingConfig": {"AdditionalVersionWeights": {"3": 0.1}},
+ })
+ status, _, body = result
+ assert status == 412
+ assert json.loads(body)["__type"] == "PreconditionFailedException"
+ assert functions._data == before
+ fetch.assert_not_called()
+ for helper in (svc.invalidate_worker, svc._pool_kill_function, svc._sweep_extract_cache,
+ svc._schedule_state_transition, svc._snapstart_provision_version_async):
+ helper.assert_not_called()
+
+
[email protected]("operation", ["code", "config", "alias"])
+def test_matching_and_omitted_revisions_allow_updates(functions, operation):
+ revision = "alias-before" if operation == "alias" else "function-before"
+ status, _, body = _update(operation, revision)
+ assert status == 200
+ updated = json.loads(body)
+ assert updated["RevisionId"] != revision
+ assert _update(operation, revision)[0] == 412
+ assert _update(operation, updated["RevisionId"])[0] == 200
+ assert _update(operation, None)[0] == 200
+
+
[email protected]("style", ["keyword", "mixed", "data-keyword"])
+def test_revision_updates_preserve_keyword_call_support(functions, style):
+ assert svc._update_code(name="revision-test", data={"RevisionId": "function-before"})[0] == 200
+ revision = functions["revision-test"]["config"]["RevisionId"]
+ assert svc._update_config(name="revision-test", data={"RevisionId": revision})[0] == 200
+ data = {"RevisionId": "alias-before"}
+ if style == "keyword":
+ result = svc._update_alias(func_name="revision-test", alias_name="live", data=data)
+ elif style == "mixed":
+ result = svc._update_alias("revision-test", alias_name="live", data=data)
+ else:
+ result = svc._update_alias("revision-test", "live", data=data)
+ assert result[0] == 200
+
+
[email protected]("first,second", [("code", "code"), ("code", "config"),
+ ("config", "code"), ("config", "config"),
+ ("alias", "alias")])
+def test_competing_updates_with_same_revision_have_one_winner(functions, monkeypatch, first, second):
+ entered, release, second_started, second_done = (threading.Event() for _ in range(4))
+ helper = "new_uuid" if first == "alias" else "_now_iso"
+ original = getattr(svc, helper)
+ first_thread = []
+
+ def pause_first():
+ if threading.get_ident() == first_thread[0] and not entered.is_set():
+ entered.set()
+ assert release.wait(5), "first update was not released"
+ return original()
+
+ monkeypatch.setattr(svc, helper, pause_first)
+ revision = "alias-before" if first == "alias" else "function-before"
+
+ def run_first():
+ first_thread.append(threading.get_ident())
+ return _update(first, revision, "winner")
+
+ def run_second():
+ second_started.set()
+ try:
+ return _update(second, revision, "loser")
+ finally:
+ second_done.set()
+
+ with ThreadPoolExecutor(max_workers=2) as pool:
+ winner = pool.submit(run_first)
+ try:
+ assert entered.wait(5), "first update never reached mutation"
+ loser = pool.submit(run_second)
+ assert second_started.wait(5)
+ # Let an unguarded contender finish while the first revision is
+ # still current; a correctly serialized contender must wait.
+ second_done.wait(0.3)
+ finally:
+ release.set()
+ assert winner.result(timeout=5)[0] == 200
+ assert loser.result(timeout=5)[0] == 412
+ record = functions["revision-test"]
+ if first == "alias":
+ assert record["aliases"]["live"]["Description"] == "winner"
+ elif first == "code":
+ assert record["code_zip"] == b"winner"
+ assert record["config"]["Description"] == "original"
+ else:
+ assert record["config"]["Description"] == "winner"
+ assert record["code_zip"] == b"original-code"
+
+
+def test_slow_code_update_does_not_block_independent_resources(functions, monkeypatch):
+ entered, release = threading.Event(), threading.Event()
+ prototype = copy.deepcopy(functions["revision-test"])
+
+ def fetch(*args, **kwargs):
+ entered.set()
+ assert release.wait(5)
+ return b"new-code"
+
+ monkeypatch.setattr(svc, "_fetch_code_from_s3", fetch)
+ with ThreadPoolExecutor(max_workers=2) as pool:
+ slow = pool.submit(svc._update_code, "revision-test", {
+ "RevisionId": "function-before", "S3Bucket": "unused", "S3Key": "unused"})
+ try:
+ assert entered.wait(5)
+ assert pool.submit(_update, "config", name="unrelated").result(timeout=2)[0] == 200
+ assert pool.submit(_update, "alias", "alias-before").result(timeout=2)[0] == 200
+ for account, region in (("222222222222", "us-east-1"), ("000000000000", "eu-west-1")):
+ def other_tenant():
+ with request_scope(account, region):
+ functions["revision-test"] = copy.deepcopy(prototype)
+ return _update("config")
+ assert pool.submit(other_tenant).result(timeout=2)[0] == 200
+ finally:
+ release.set()
+ assert slow.result(timeout=5)[0] == 200
+
+
[email protected]("operation", ["code", "config", "alias"])
+def test_missing_resource_keeps_not_found_response(functions, operation):
+ assert _update(operation, "stale", name="missing")[0] == 404
+ assert svc._update_alias("revision-test", "missing", {"RevisionId": "stale"})[0] == 404