Skip to content

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

fix(s3): stop sending s3:TestEvent to Lambda notification targets (#1688)

ppettitaucd908d6c5b93
diff --git a/CHANGELOG.md b/CHANGELOG.md
index c4f691b5..fe130095 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,8 @@ Versioning follows [Semantic Versioning](https://semver.org/).
## [Unreleased]
+### Fixed
+- **S3 — the `s3:TestEvent` no longer reaches Lambda targets** — `PutBucketNotificationConfiguration` fanned the test event out to every destination, Lambda included, but AWS sends it to SQS queues and SNS topics only; a Lambda destination is verified through its function permissions, not by invoking it. The payload carries no `Records` array, so each S3-triggered function raised on it, and because delivery goes through the async path the event was retried to `MaximumRetryAttempts` (default 2) and then routed to the function's DLQ or `OnFailure` destination, where a synthetic event AWS never sends looked like a genuinely dropped message. Any CDK stack with an S3-triggered Lambda hit this through `BucketNotificationsHandler`. Queue and topic destinations still receive the test event, and real object events still invoke the function. Contributed by @ppettitau.
### Changed
- **Lambda — warm local custom runtimes** — `provided.*` bootstraps now reuse the existing subprocess worker pool instead of restarting on every invocation. Each invocation receives its request metadata through the Lambda Runtime API, concurrent calls use separate workers, and failed environments are cleaned up before reuse. Durable invocations retain their one-shot executor; Docker, image, and proxy execution are unchanged.
diff --git a/ministack/services/s3.py b/ministack/services/s3.py
index 1be395dd..205d5721 100644
--- a/ministack/services/s3.py
+++ b/ministack/services/s3.py
@@ -2632,7 +2632,8 @@ def _put_bucket_notification(name: str, body: bytes):
# returns — matches AWS's effective behaviour and avoids a race where the
# client polls the destination queue/topic before the background thread has
# delivered the message (also loses the caller's account contextvar across
- # threads, which broke multi-tenant tests).
+ # threads, which broke multi-tenant tests). Queue and topic destinations only;
+ # AWS does not send the test event to Lambda targets.
_fire_s3_test_event(name)
return 200, {}, b""
@@ -3339,7 +3340,8 @@ def _fire_s3_event_async(
def _fire_s3_test_event(bucket_name: str) -> None:
- """Deliver an s3:TestEvent to every destination in the bucket notification config."""
+ """Deliver an s3:TestEvent to the SQS and SNS destinations in the bucket
+ notification config. AWS does not send it to Lambda targets."""
try:
configs = _parse_notification_config(bucket_name)
if not configs:
@@ -3359,8 +3361,8 @@ def _fire_s3_test_event(bucket_name: str) -> None:
_deliver_event_to_sqs(cfg["arn"], payload, bucket_region)
elif cfg["type"] == "sns":
_deliver_event_to_sns(cfg["arn"], payload, bucket_region)
- elif cfg["type"] == "lambda":
- _deliver_event_to_lambda(cfg["arn"], payload, bucket_region)
+ # No lambda branch: AWS verifies Lambda destinations by checking the
+ # function's permissions, not by invoking them.
except Exception:
logger.exception("S3 test-event delivery failed for config %s", cfg.get("id"))
except Exception:
diff --git a/tests/test_s3.py b/tests/test_s3.py
index a0c93b31..e3a4adf7 100644
--- a/tests/test_s3.py
+++ b/tests/test_s3.py
@@ -2011,6 +2011,31 @@ def test_s3_event_notification_to_lambda_boto3_default(s3, lam, logs):
"Lambda was not invoked for boto3-shaped notification config"
+def test_s3_put_notification_no_test_event_to_lambda(s3, lam, logs):
+ """AWS delivers the s3:TestEvent only to SQS and SNS destinations. A Lambda
+ target must not be invoked at configuration time, otherwise every deploy
+ logs failures for handlers that assume the Records array is present.
+ """
+ fname = "s3-no-test-evt-lam"
+ bkt = "s3-no-test-evt-bkt"
+ arn = _create_event_lambda(lam, fname)
+ s3.create_bucket(Bucket=bkt)
+ s3.put_bucket_notification_configuration(
+ Bucket=bkt,
+ NotificationConfiguration={
+ "LambdaFunctionConfigurations": [
+ {"LambdaFunctionArn": arn, "Events": ["s3:ObjectCreated:*"]},
+ ],
+ },
+ )
+ assert not _wait_lambda_invoked(logs, fname, "s3:TestEvent", timeout=3.0), \
+ "s3:TestEvent was delivered to the Lambda target; AWS sends it to SQS and SNS only"
+
+ s3.put_object(Bucket=bkt, Key="real.txt", Body=b"hi")
+ assert _wait_lambda_invoked(logs, fname, "real.txt"), \
+ "Lambda was not invoked for a real object event"
+
+
def test_s3_event_notification_to_lambda_validates_bucket_region(s3, lam):
fname = "s3-evt-lam-region"
_create_event_lambda(lam, fname, marker="east")