Skip to content

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

Added — S3 s3:TestEvent on bucket notification config (#416) (#423)

Nahuel Nucera83d1da41e6da
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3f5bb2ab..ec0dcdd8 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,21 @@ Versioning follows [Semantic Versioning](https://semver.org/).
---
+## [1.3.8] — 2026-04-21
+
+### Added
+- **S3 `s3:TestEvent` on `PutBucketNotificationConfiguration`** — configuring bucket notifications now delivers a flat `s3:TestEvent` payload (no `Records` wrapper) to every SQS / SNS / Lambda destination, matching real AWS S3 behaviour so tooling that listens for the test event on bucket setup works locally. Contributed by @nigel-campbell (#416)
+
+### Fixed
+- **Lambda persistence crash on warm start with any event source mapping** — `lambda_svc.restore_state()` called `_ensure_poller()` when restoring ESMs, but the module-level invocation ran at line 170 while `_ensure_poller` was defined ~3,500 lines later. Warm starts with `PERSIST_STATE=1` and an ESM in `lambda.json` raised `NameError: name '_ensure_poller' is not defined` on every Lambda request until the state file was deleted. The module-level load/restore is now at the bottom of the file, after every helper it may call. Reported by @whittin3. Fixes #412
+- **DynamoDB `SSEDescription` used the request shape instead of the response shape** — `CreateTable` stored the caller's `SSESpecification` (`Enabled`/`KMSMasterKeyId`) directly as the response `SSEDescription`, which is missing the `Status` field Terraform v6 waits on; `UpdateTable` silently ignored `SSESpecification`. Warm-boot `terraform apply` on any encrypted table hung forever with `unexpected state '', wanted target 'DISABLED, ENABLED'`. Fixed by converting spec → description (`Status`, `SSEType`, `KMSMasterKeyArn`) at create + update, plus a one-shot migration in `restore_state` for legacy persisted tables. Reported by @whittin3. Fixes #411
+- **`test_s3_put_notification_sends_test_event` was flaky under parallel load** — background thread delivering `s3:TestEvent` raced the test's poll. `PutBucketNotification` now delivers the test event synchronously (matching AWS effective behaviour) before returning; removes the race and also fixes multi-tenant delivery where the worker thread lost the caller's account contextvar.
+
+### Changed
+- **Hardened persisted-state restore across every service** — 35 service modules wrapped their module-level `load_state()` + `restore_state()` invocation in `try/except` so a corrupt or schema-incompatible `{service}.json` logs and continues with a fresh store instead of breaking the service at import. Audited every `restore_state` / `load_persisted_state` for forward references (0 other violators) and unsafe `data[key]` subscript accesses (all guarded). This closes the entire class of bug around #412.
+
+---
+
## [1.3.7] — 2026-04-21
### Fixed
diff --git a/ministack/core/persistence.py b/ministack/core/persistence.py
index b75fcfb5..c29c08a3 100644
--- a/ministack/core/persistence.py
+++ b/ministack/core/persistence.py
@@ -19,7 +19,14 @@ STATE_DIR = os.environ.get("STATE_DIR", "/tmp/ministack-state")
def _json_default(obj):
- """JSON encoder fallback for AccountScopedDict and tuple keys."""
+ """JSON encoder fallback for AccountScopedDict, tuple keys, and bytes.
+
+ Historically, several S3 (and other service) stores held raw request
+ bodies as ``bytes``. ``json.dump`` raised ``TypeError`` and
+ ``save_state`` silently swallowed the error, leaving ``${service}.json``
+ absent on disk (issue #422). Bytes are now serialized as base64 inside a
+ tagged dict so round-trip fidelity is preserved even for non-UTF-8
+ payloads."""
if isinstance(obj, AccountScopedDict):
# Serialize all accounts' data with string keys
result = {}
@@ -27,11 +34,14 @@ def _json_default(obj):
# k is (account_id, original_key) tuple
result[f"{k[0]}\x00{k[1]!r}"] = v
return {"__scoped__": True, "data": result}
+ if isinstance(obj, (bytes, bytearray)):
+ import base64
+ return {"__bytes__": base64.b64encode(bytes(obj)).decode("ascii")}
raise TypeError(f"Object of type {type(obj).__name__} is not JSON serializable")
def _json_object_hook(obj):
- """JSON decoder hook to restore AccountScopedDict from serialized form."""
+ """JSON decoder hook to restore AccountScopedDict and bytes from serialized form."""
if obj.get("__scoped__"):
asd = AccountScopedDict()
for k, v in obj["data"].items():
@@ -43,6 +53,9 @@ def _json_object_hook(obj):
original_key = key_repr
asd._data[(account_id, original_key)] = v
return asd
+ if "__bytes__" in obj:
+ import base64
+ return base64.b64decode(obj["__bytes__"])
return obj
diff --git a/ministack/services/acm.py b/ministack/services/acm.py
index 749de884..844ea123 100644
--- a/ministack/services/acm.py
+++ b/ministack/services/acm.py
@@ -32,9 +32,15 @@ def restore_state(data):
_certificates.update(data.get("_certificates", {}))
-_restored = load_state("acm")
-if _restored:
- restore_state(_restored)
+try:
+ _restored = load_state("acm")
+ if _restored:
+ restore_state(_restored)
+except Exception:
+ import logging
+ logging.getLogger(__name__).exception(
+ "Failed to restore persisted state; continuing with fresh store"
+ )
def _future_iso(seconds):
diff --git a/ministack/services/alb.py b/ministack/services/alb.py
index 3293cff0..7f336544 100644
--- a/ministack/services/alb.py
+++ b/ministack/services/alb.py
@@ -75,9 +75,15 @@ def restore_state(data):
_listener_attrs.update(data.get("_listener_attrs", {}))
-_restored = load_state("alb")
-if _restored:
- restore_state(_restored)
+try:
+ _restored = load_state("alb")
+ if _restored:
+ restore_state(_restored)
+except Exception:
+ import logging
+ logging.getLogger(__name__).exception(
+ "Failed to restore persisted state; continuing with fresh store"
+ )
# ---------------------------------------------------------------------------
# Small helpers
diff --git a/ministack/services/appconfig.py b/ministack/services/appconfig.py
index 29123ac7..fa0f6328 100644
--- a/ministack/services/appconfig.py
+++ b/ministack/services/appconfig.py
@@ -80,9 +80,15 @@ def restore_state(data):
_tags.update(data.get("tags", {}))
-_restored = load_state("appconfig")
-if _restored:
- restore_state(_restored)
+try:
+ _restored = load_state("appconfig")
+ if _restored:
+ restore_state(_restored)
+except Exception:
+ import logging
+ logging.getLogger(__name__).exception(
+ "Failed to restore persisted state; continuing with fresh store"
+ )
# ---------------------------------------------------------------------------
# Helpers
diff --git a/ministack/services/athena.py b/ministack/services/athena.py
index 75c9d828..5487292e 100644
--- a/ministack/services/athena.py
+++ b/ministack/services/athena.py
@@ -105,9 +105,15 @@ def restore_state(data):
_tags.update(data.get("_tags", {}))
-_restored = load_state("athena")
-if _restored:
- restore_state(_restored)
+try:
+ _restored = load_state("athena")
+ if _restored:
+ restore_state(_restored)
+except Exception:
+ import logging
+ logging.getLogger(__name__).exception(
+ "Failed to restore persisted state; continuing with fresh store"
+ )
try:
import duckdb
diff --git a/ministack/services/cloudfront.py b/ministack/services/cloudfront.py
index d6bec38b..55776d52 100644
--- a/ministack/services/cloudfront.py
+++ b/ministack/services/cloudfront.py
@@ -78,9 +78,15 @@ def restore_state(data):
_oacs.update(data.get("oacs", {}))
-_restored = load_state("cloudfront")
-if _restored:
- restore_state(_restored)
+try:
+ _restored = load_state("cloudfront")
+ if _restored:
+ restore_state(_restored)
+except Exception:
+ import logging
+ logging.getLogger(__name__).exception(
+ "Failed to restore persisted state; continuing with fresh store"
+ )
# ---------------------------------------------------------------------------
diff --git a/ministack/services/cloudwatch.py b/ministack/services/cloudwatch.py
index cd8fe00b..319b263f 100644
--- a/ministack/services/cloudwatch.py
+++ b/ministack/services/cloudwatch.py
@@ -82,9 +82,15 @@ def restore_state(data):
_resource_tags.update(data.get("resource_tags", {}))
-_restored = load_state("cloudwatch")
-if _restored:
- restore_state(_restored)
+try:
+ _restored = load_state("cloudwatch")
+ if _restored:
+ restore_state(_restored)
+except Exception:
+ import logging
+ logging.getLogger(__name__).exception(
+ "Failed to restore persisted state; continuing with fresh store"
+ )
# ---------------------------------------------------------------------------
diff --git a/ministack/services/cloudwatch_logs.py b/ministack/services/cloudwatch_logs.py
index 568a1451..f113be4c 100644
--- a/ministack/services/cloudwatch_logs.py
+++ b/ministack/services/cloudwatch_logs.py
@@ -61,9 +61,15 @@ def restore_state(data):
_log_groups.update(data.get("log_groups", {}))
-_restored = load_state("cloudwatch_logs")
-if _restored:
- restore_state(_restored)
+try:
+ _restored = load_state("cloudwatch_logs")
+ if _restored:
+ restore_state(_restored)
+except Exception:
+ import logging
+ logging.getLogger(__name__).exception(
+ "Failed to restore persisted state; continuing with fresh store"
+ )
# ---------------------------------------------------------------------------
diff --git a/ministack/services/codebuild.py b/ministack/services/codebuild.py
index a591fc02..92017c4d 100644
--- a/ministack/services/codebuild.py
+++ b/ministack/services/codebuild.py
@@ -46,9 +46,15 @@ def restore_state(data):
_builds.update(data.get("builds", {}))
-_restored = load_state("codebuild")
-if _restored:
- restore_state(_restored)
+try:
+ _restored = load_state("codebuild")
+ if _restored:
+ restore_state(_restored)
+except Exception:
+ import logging
+ logging.getLogger(__name__).exception(
+ "Failed to restore persisted state; continuing with fresh store"
+ )
# ---------------------------------------------------------------------------
diff --git a/ministack/services/cognito.py b/ministack/services/cognito.py
index dfb9cca2..787e27d3 100644
--- a/ministack/services/cognito.py
+++ b/ministack/services/cognito.py
@@ -230,9 +230,15 @@ def restore_state(data):
_refresh_tokens.update(data.get("refresh_tokens", {}))
-_restored = load_state("cognito")
-if _restored:
- restore_state(_restored)
+try:
+ _restored = load_state("cognito")
+ if _restored:
+ restore_state(_restored)
+except Exception:
+ import logging
+ logging.getLogger(__name__).exception(
+ "Failed to restore persisted state; continuing with fresh store"
+ )
# ---------------------------------------------------------------------------
diff --git a/ministack/services/dynamodb.py b/ministack/services/dynamodb.py
index 9102486f..6eb51472 100644
--- a/ministack/services/dynamodb.py
+++ b/ministack/services/dynamodb.py
@@ -56,14 +56,25 @@ def restore_state(data):
for tbl in _tables.values():
if isinstance(tbl.get("items"), dict) and not isinstance(tbl["items"], defaultdict):
tbl["items"] = defaultdict(dict, tbl["items"])
+ # Migrate legacy SSEDescription shape (pre-#411): convert
+ # {Enabled, KMSMasterKeyId} → {Status, KMSMasterKeyArn, SSEType}.
+ sse = tbl.get("SSEDescription")
+ if sse and "Status" not in sse and ("Enabled" in sse or "KMSMasterKeyId" in sse):
+ tbl["SSEDescription"] = _sse_description_from_spec(sse)
_tags.update(data.get("tags", {}))
_ttl_settings.update(data.get("ttl_settings", {}))
_pitr_settings.update(data.get("pitr_settings", {}))
-_restored = load_state("dynamodb")
-if _restored:
- restore_state(_restored)
+try:
+ _restored = load_state("dynamodb")
+ if _restored:
+ restore_state(_restored)
+except Exception:
+ import logging
+ logging.getLogger(__name__).exception(
+ "Failed to restore persisted state; continuing with fresh store"
+ )
# DynamoDB Streams: table_name -> list of stream records
# Each record follows the DynamoDB Streams event format consumed by Lambda ESMs.
@@ -215,6 +226,37 @@ async def handle_request(method: str, path: str, headers: dict, body: bytes, que
# Table operations
# ---------------------------------------------------------------------------
+def _sse_description_from_spec(spec: dict | None) -> dict | None:
+ """Convert the request's ``SSESpecification`` into the response-shape
+ ``SSEDescription`` AWS actually returns on DescribeTable.
+
+ Request shape (caller):
+ {"Enabled": true, "SSEType": "KMS", "KMSMasterKeyId": "<arn|alias|id>"}
+
+ Response shape (SSEDescription per AWS docs):
+ {"Status": "ENABLED" | "DISABLED",
+ "SSEType": "AES256" | "KMS",
+ "KMSMasterKeyArn": "<key-arn>", # only when SSEType == KMS
+ "InaccessibleEncryptionDateTime": <optional>}
+
+ Terraform waiters read ``Status`` and ``KMSMasterKeyArn``; the legacy
+ ``Enabled`` / ``KMSMasterKeyId`` names are request-only and Terraform
+ v6 will hang forever waiting for a status that never appears (#411).
+ """
+ if not spec:
+ return None
+ enabled = bool(spec.get("Enabled", False))
+ sse_type = spec.get("SSEType") or ("KMS" if spec.get("KMSMasterKeyId") else "AES256")
+ desc = {
+ "Status": "ENABLED" if enabled else "DISABLED",
+ "SSEType": sse_type,
+ }
+ if sse_type == "KMS":
+ kms_key = spec.get("KMSMasterKeyId") or spec.get("KMSMasterKeyArn", "")
+ desc["KMSMasterKeyArn"] = kms_key
+ return desc
+
+
def _create_table(data):
name = data.get("TableName")
if not name:
@@ -270,7 +312,7 @@ def _create_table(data):
else data.get("ProvisionedThroughput", {"ReadCapacityUnits": 5, "WriteCapacityUnits": 5}),
"BillingModeSummary": {"BillingMode": data.get("BillingMode", "PROVISIONED")},
"StreamSpecification": data.get("StreamSpecification"),
- "SSEDescription": data.get("SSESpecification"),
+ "SSEDescription": _sse_description_from_spec(data.get("SSESpecification")),
}
if data.get("StreamSpecification"):
stream_label = now_iso()
@@ -330,6 +372,12 @@ def _update_table(data):
table["AttributeDefinitions"] = data["AttributeDefinitions"]
if "StreamSpecification" in data:
table["StreamSpecification"] = data["StreamSpecification"]
+ if "SSESpecification" in data:
+ # Terraform v6 calls UpdateTable(SSESpecification=...) on warm boots
+ # when it sees the legacy shape in state (#411). Convert to the
+ # response-shape SSEDescription with a proper Status field so the
+ # Terraform waiter can observe ENABLED/DISABLED and return.
+ table["SSEDescription"] = _sse_description_from_spec(data["SSESpecification"])
for update in data.get("GlobalSecondaryIndexUpdates", []):
if "Create" in update:
diff --git a/ministack/services/ec2.py b/ministack/services/ec2.py
index 7e388747..d9d0fcef 100644
--- a/ministack/services/ec2.py
+++ b/ministack/services/ec2.py
@@ -156,9 +156,15 @@ def restore_state(data):
_launch_templates.update(data.get("launch_templates", {}))
-_restored = load_state("ec2")
-if _restored:
- restore_state(_restored)
+try:
+ _restored = load_state("ec2")
+ if _restored:
+ restore_state(_restored)
+except Exception:
+ import logging
+ logging.getLogger(__name__).exception(
+ "Failed to restore persisted state; continuing with fresh store"
+ )
# Default VPC / subnet created at import time so DescribeVpcs always returns something
diff --git a/ministack/services/ecr.py b/ministack/services/ecr.py
index bff39279..9be13f3b 100644
--- a/ministack/services/ecr.py
+++ b/ministack/services/ecr.py
@@ -43,9 +43,15 @@ def restore_state(data):
_repo_policies.update(data.get("repo_policies", {}))
-_restored = load_state("ecr")
-if _restored:
- restore_state(_restored)
+try:
+ _restored = load_state("ecr")
+ if _restored:
+ restore_state(_restored)
+except Exception:
+ import logging
+ logging.getLogger(__name__).exception(
+ "Failed to restore persisted state; continuing with fresh store"
+ )
def _repo_arn(name):
diff --git a/ministack/services/ecs.py b/ministack/services/ecs.py
index bffcda4b..94cc2812 100644
--- a/ministack/services/ecs.py
+++ b/ministack/services/ecs.py
@@ -104,9 +104,15 @@ def restore_state(data):
_tasks[arn] = task
-_restored = load_state("ecs")
-if _restored:
- restore_state(_restored)
+try:
+ _restored = load_state("ecs")
+ if _restored:
+ restore_state(_restored)
+except Exception:
+ import logging
+ logging.getLogger(__name__).exception(
+ "Failed to restore persisted state; continuing with fresh store"
+ )
def _get_docker():
diff --git a/ministack/services/efs.py b/ministack/services/efs.py
index 4c9f752f..a28cb1dc 100644
--- a/ministack/services/efs.py
+++ b/ministack/services/efs.py
@@ -367,9 +367,15 @@ def restore_state(data):
_backup_policies.update(data.get("backup_policies", {}))
-_restored = load_state("efs")
-if _restored:
- restore_state(_restored)
+try:
+ _restored = load_state("efs")
+ if _restored:
+ restore_state(_restored)
+except Exception:
+ import logging
+ logging.getLogger(__name__).exception(
+ "Failed to restore persisted state; continuing with fresh store"
+ )
def _put_lifecycle_configuration(fs_id, body):
diff --git a/ministack/services/elasticache.py b/ministack/services/elasticache.py
index c4a13a0d..8c0bcc43 100644
--- a/ministack/services/elasticache.py
+++ b/ministack/services/elasticache.py
@@ -105,9 +105,15 @@ def restore_state(data):
_clusters[name] = cl
-_restored = load_state("elasticache")
-if _restored:
- restore_state(_restored)
+try:
+ _restored = load_state("elasticache")
+ if _restored:
+ restore_state(_restored)
+except Exception:
+ import logging
+ logging.getLogger(__name__).exception(
+ "Failed to restore persisted state; continuing with fresh store"
+ )
def _get_docker():
diff --git a/ministack/services/emr.py b/ministack/services/emr.py
index a7454e91..19faea2e 100644
--- a/ministack/services/emr.py
+++ b/ministack/services/emr.py
@@ -55,9 +55,15 @@ def restore_state(data):
_block_public_access.update(data.get("_block_public_access", {}))
-_restored = load_state("emr")
-if _restored:
- restore_state(_restored)
+try:
+ _restored = load_state("emr")
+ if _restored:
+ restore_state(_restored)
+except Exception:
+ import logging
+ logging.getLogger(__name__).exception(
+ "Failed to restore persisted state; continuing with fresh store"
+ )
# ---------------------------------------------------------------------------
# ID generators
diff --git a/ministack/services/eventbridge.py b/ministack/services/eventbridge.py
index b0e242ec..6063d5ff 100644
--- a/ministack/services/eventbridge.py
+++ b/ministack/services/eventbridge.py
@@ -141,9 +141,15 @@ def restore_state(data):
rep[tk] = _coerce_timestamp(rep[tk])
-_restored = load_state("eventbridge")
-if _restored:
- restore_state(_restored)
+try:
+ _restored = load_state("eventbridge")
+ if _restored:
+ restore_state(_restored)
+except Exception:
+ import logging
+ logging.getLogger(__name__).exception(
+ "Failed to restore persisted state; continuing with fresh store"
+ )
async def handle_request(method, path, headers, body, query_params):
diff --git a/ministack/services/firehose.py b/ministack/services/firehose.py
index 1777ecc7..8efb244d 100644
--- a/ministack/services/firehose.py
+++ b/ministack/services/firehose.py
@@ -62,9 +62,15 @@ def restore_state(data: dict):
_dest_counter = data.get("_dest_counter", _dest_counter)
-_restored = load_state("firehose")
-if _restored:
- restore_state(_restored)
+try:
+ _restored = load_state("firehose")
+ if _restored:
+ restore_state(_restored)
+except Exception:
+ import logging
+ logging.getLogger(__name__).exception(
+ "Failed to restore persisted state; continuing with fresh store"
+ )
# ─── helpers ─────────────────────────────────────────────────────────────────
diff --git a/ministack/services/glue.py b/ministack/services/glue.py
index 110258e9..06a1d51e 100644
--- a/ministack/services/glue.py
+++ b/ministack/services/glue.py
@@ -70,9 +70,15 @@ def restore_state(data):
store.update(data.get(key, {}))
-_restored = load_state("glue")
-if _restored:
- restore_state(_restored)
+try:
+ _restored = load_state("glue")
+ if _restored:
+ restore_state(_restored)
+except Exception:
+ import logging
+ logging.getLogger(__name__).exception(
+ "Failed to restore persisted state; continuing with fresh store"
+ )
def _arn(resource_type, name):
diff --git a/ministack/services/iam.py b/ministack/services/iam.py
index f5d10d00..f13d849e 100644
--- a/ministack/services/iam.py
+++ b/ministack/services/iam.py
@@ -86,9 +86,15 @@ def restore_state(data):
_user_inline_policies.update(data.get("user_inline_policies", {}))
-_restored = load_state("iam")
-if _restored:
- restore_state(_restored)
+try:
+ _restored = load_state("iam")
+ if _restored:
+ restore_state(_restored)
+except Exception:
+ import logging
+ logging.getLogger(__name__).exception(
+ "Failed to restore persisted state; continuing with fresh store"
+ )
# ===================================================================== IAM
diff --git a/ministack/services/kinesis.py b/ministack/services/kinesis.py
index 1b9aac44..35a341d1 100644
--- a/ministack/services/kinesis.py
+++ b/ministack/services/kinesis.py
@@ -49,9 +49,15 @@ def restore_state(data):
_streams.update(data.get("streams", {}))
-_restored = load_state("kinesis")
-if _restored:
- restore_state(_restored)
+try:
+ _restored = load_state("kinesis")
+ if _restored:
+ restore_state(_restored)
+except Exception:
+ import logging
+ logging.getLogger(__name__).exception(
+ "Failed to restore persisted state; continuing with fresh store"
+ )
def _next_sequence_number():
diff --git a/ministack/services/kms.py b/ministack/services/kms.py
index 974ebf69..9c6faef1 100644
--- a/ministack/services/kms.py
+++ b/ministack/services/kms.py
@@ -101,9 +101,15 @@ def restore_state(data):
_aliases.update(data.get("aliases", {}))
-_restored = load_state("kms")
-if _restored:
- restore_state(_restored)
+try:
+ _restored = load_state("kms")
+ if _restored:
+ restore_state(_restored)
+except Exception:
+ import logging
+ logging.getLogger(__name__).exception(
+ "Failed to restore persisted state; continuing with fresh store"
+ )
def _arn(key_id):
diff --git a/ministack/services/lambda_svc.py b/ministack/services/lambda_svc.py
index 57313178..b4844533 100644
--- a/ministack/services/lambda_svc.py
+++ b/ministack/services/lambda_svc.py
@@ -165,9 +165,12 @@ def restore_state(data):
_ensure_poller()
-_restored = load_state("lambda")
-if _restored:
- restore_state(_restored)
+# NOTE: the persisted-state load used to run here, but ``restore_state`` calls
+# ``_ensure_poller()`` when the restored data contains event source mappings,
+# and that helper is defined much later in this module. Restoring at import
+# time raised ``NameError: _ensure_poller`` on warm starts with a populated
+# ``lambda.json`` (issue #412). The load now lives at the bottom of the file,
+# after ``_ensure_poller`` is defined.
# ---------------------------------------------------------------------------
@@ -4100,3 +4103,18 @@ def reset():
_dynamodb_stream_positions.clear()
_pool_clear_all()
lambda_runtime.reset()
+
+
+# ---------------------------------------------------------------------------
+# Persisted-state restore — runs at module import time but deferred to the
+# very bottom of the file so forward references to helpers (e.g.
+# ``_ensure_poller``) resolve at call time (issue #412). A corrupt or
+# incompatible ``lambda.json`` logs and continues instead of breaking the
+# whole service.
+# ---------------------------------------------------------------------------
+try:
+ _restored = load_state("lambda")
+ if _restored:
+ restore_state(_restored)
+except Exception:
+ logger.exception("Failed to restore persisted Lambda state; continuing with a fresh store")
diff --git a/ministack/services/rds.py b/ministack/services/rds.py
index 700e7aea..6e3bc90c 100644
--- a/ministack/services/rds.py
+++ b/ministack/services/rds.py
@@ -112,9 +112,15 @@ def restore_state(data):
_instances[name] = inst
-_restored = load_state("rds")
-if _restored:
- restore_state(_restored)
+try:
+ _restored = load_state("rds")
+ if _restored:
+ restore_state(_restored)
+except Exception:
+ import logging
+ logging.getLogger(__name__).exception(
+ "Failed to restore persisted state; continuing with fresh store"
+ )
def _get_docker():
diff --git a/ministack/services/route53.py b/ministack/services/route53.py
index 0540a391..c40c0e4a 100644
--- a/ministack/services/route53.py
+++ b/ministack/services/route53.py
@@ -79,9 +79,15 @@ def restore_state(data):
_changes.update(data.get("changes", {}))
-_restored = load_state("route53")
-if _restored:
- restore_state(_restored)
+try:
+ _restored = load_state("route53")
+ if _restored:
+ restore_state(_restored)
+except Exception:
+ import logging
+ logging.getLogger(__name__).exception(
+ "Failed to restore persisted state; continuing with fresh store"
+ )
def reset():
diff --git a/ministack/services/s3.py b/ministack/services/s3.py
index 3a51847d..261d79ff 100644
--- a/ministack/services/s3.py
+++ b/ministack/services/s3.py
@@ -132,9 +132,15 @@ def restore_state(data):
_bucket_replication.update(data.get("bucket_replication", {}))
-_restored = load_state("s3")
-if _restored:
- restore_state(_restored)
+try:
+ _restored = load_state("s3")
+ if _restored:
+ restore_state(_restored)
+except Exception:
+ import logging
+ logging.getLogger(__name__).exception(
+ "Failed to restore persisted state; continuing with fresh store"
+ )
DATA_DIR = os.environ.get("S3_DATA_DIR", "/tmp/ministack-data/s3")
@@ -739,7 +745,7 @@ def _get_bucket_encryption(name: str):
def _put_bucket_encryption(name: str, body: bytes):
if name not in _buckets:
return _no_such_bucket(name)
- _bucket_encryption[name] = body
+ _bucket_encryption[name] = body.decode("utf-8", errors="replace")
return 200, {}, b""
@@ -924,7 +930,7 @@ def _put_bucket_lifecycle(name: str, body: bytes):
rules.append(rule)
except Exception:
# Fallback: store raw if parsing fails
- _bucket_lifecycle[name] = body
+ _bucket_lifecycle[name] = body.decode("utf-8", errors="replace")
return 200, {"x-amz-transition-default-minimum-object-size": "all_storage_classes_128K"}, b""
_bucket_lifecycle[name] = rules
return 200, {"x-amz-transition-default-minimum-object-size": "all_storage_classes_128K"}, b""
@@ -954,7 +960,7 @@ def _get_bucket_cors(name: str):
def _put_bucket_cors(name: str, body: bytes):
if name not in _buckets:
return _no_such_bucket(name)
- _bucket_cors[name] = body
+ _bucket_cors[name] = body.decode("utf-8", errors="replace")
return 200, {}, b""
@@ -988,7 +994,7 @@ def _put_bucket_acl(name: str, body: bytes):
if name not in _buckets:
return _no_such_bucket(name)
if body:
- _bucket_acl[name] = body
+ _bucket_acl[name] = body.decode("utf-8", errors="replace")
return 200, {}, b""
@@ -1030,7 +1036,7 @@ def _delete_bucket_tagging(name: str):
def _put_bucket_ownership_controls(name: str, body: bytes):
if name not in _buckets:
return _no_such_bucket(name)
- _buckets[name]["_ownership_controls"] = body
+ _buckets[name]["_ownership_controls"] = body.decode("utf-8", errors="replace")
return 200, {}, b""
@@ -1056,7 +1062,7 @@ def _delete_bucket_ownership_controls(name: str):
def _put_public_access_block(name: str, body: bytes):
if name not in _buckets:
return _no_such_bucket(name)
- _buckets[name]["_public_access_block"] = body
+ _buckets[name]["_public_access_block"] = body.decode("utf-8", errors="replace")
return 200, {}, b""
@@ -1095,8 +1101,13 @@ def _get_bucket_notification(name: str):
def _put_bucket_notification(name: str, body: bytes):
if name not in _buckets:
return _no_such_bucket(name)
- _bucket_notifications[name] = body
- _fire_s3_test_event_async(name)
+ _bucket_notifications[name] = body.decode("utf-8", errors="replace")
+ # Fire the s3:TestEvent synchronously so it's delivered before PutBucketNotification
+ # 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).
+ _fire_s3_test_event(name)
return 200, {}, b""
@@ -1113,7 +1124,7 @@ def _get_bucket_logging(name: str):
def _put_bucket_logging(name: str, body: bytes):
if name not in _buckets:
return _no_such_bucket(name)
- _bucket_logging_config[name] = body
+ _bucket_logging_config[name] = body.decode("utf-8", errors="replace")
return 200, {}, b""
@@ -1130,7 +1141,7 @@ def _get_bucket_accelerate(name: str):
def _put_bucket_accelerate(name: str, body: bytes):
if name not in _buckets:
return _no_such_bucket(name)
- _bucket_accelerate_config[name] = body
+ _bucket_accelerate_config[name] = body.decode("utf-8", errors="replace")
return 200, {}, b""
@@ -1148,7 +1159,7 @@ def _get_bucket_request_payment(name: str):
def _put_bucket_request_payment(name: str, body: bytes):
if name not in _buckets:
return _no_such_bucket(name)
- _bucket_request_payment_config[name] = body
+ _bucket_request_payment_config[name] = body.decode("utf-8", errors="replace")
return 200, {}, b""
@@ -1169,7 +1180,7 @@ def _get_bucket_website(name: str):
def _put_bucket_website(name: str, body: bytes):
if name not in _buckets:
return _no_such_bucket(name)
- _bucket_websites[name] = body
+ _bucket_websites[name] = body.decode("utf-8", errors="replace")
return 200, {}, b""
@@ -1378,8 +1389,8 @@ def _fire_s3_event(
"""Build and deliver an S3 event notification. Best-effort — errors are logged."""
try:
configs = _parse_notification_config(bucket_name)
- raw_xml = _bucket_notifications.get(bucket_name, b"")
- has_eventbridge = b"EventBridgeConfiguration" in raw_xml
+ raw_xml = _bucket_notifications.get(bucket_name, "")
+ has_eventbridge = "EventBridgeConfiguration" in raw_xml
if not configs and not has_eventbridge:
return
diff --git a/ministack/services/s3files.py b/ministack/services/s3files.py
index 59ece9fe..195cf573 100644
--- a/ministack/services/s3files.py
+++ b/ministack/services/s3files.py
@@ -55,9 +55,15 @@ def restore_state(data):
_tags.update(data.get("tags", {}))
-_restored = load_state("s3files")
-if _restored:
- restore_state(_restored)
+try:
+ _restored = load_state("s3files")
+ if _restored:
+ restore_state(_restored)
+except Exception:
+ import logging
+ logging.getLogger(__name__).exception(
+ "Failed to restore persisted state; continuing with fresh store"
+ )
def reset():
diff --git a/ministack/services/secretsmanager.py b/ministack/services/secretsmanager.py
index dc498c77..110d69e5 100644
--- a/ministack/services/secretsmanager.py
+++ b/ministack/services/secretsmanager.py
@@ -50,9 +50,15 @@ def restore_state(data):
_secrets.update(data.get("secrets", {}))
-_restored = load_state("secretsmanager")
-if _restored:
- restore_state(_restored)
+try:
+ _restored = load_state("secretsmanager")
+ if _restored:
+ restore_state(_restored)
+except Exception:
+ import logging
+ logging.getLogger(__name__).exception(
+ "Failed to restore persisted state; continuing with fresh store"
+ )
# ---------------------------------------------------------------------------
diff --git a/ministack/services/ses.py b/ministack/services/ses.py
index 2548707b..2f4fd402 100644
--- a/ministack/services/ses.py
+++ b/ministack/services/ses.py
@@ -79,9 +79,15 @@ def restore_state(data: dict):
_configuration_sets.update(data.get("_configuration_sets", {}))
-_restored = load_state("ses")
-if _restored:
- restore_state(_restored)
+try:
+ _restored = load_state("ses")
+ if _restored:
+ restore_state(_restored)
+except Exception:
+ import logging
+ logging.getLogger(__name__).exception(
+ "Failed to restore persisted state; continuing with fresh store"
+ )
# ---------------------------------------------------------------------------
diff --git a/ministack/services/ses_v2.py b/ministack/services/ses_v2.py
index bbad2cba..8bcbecc8 100644
--- a/ministack/services/ses_v2.py
+++ b/ministack/services/ses_v2.py
@@ -41,9 +41,15 @@ def restore_state(data: dict):
_ses_tags.update(data.get("_ses_tags", {}))
-_restored = load_state("ses_v2")
-if _restored:
- restore_state(_restored)
+try:
+ _restored = load_state("ses_v2")
+ if _restored:
+ restore_state(_restored)
+except Exception:
+ import logging
+ logging.getLogger(__name__).exception(
+ "Failed to restore persisted state; continuing with fresh store"
+ )
def _json_err(code, message, status=400):
diff --git a/ministack/services/sns.py b/ministack/services/sns.py
index cdc8791f..a855e5b8 100644
--- a/ministack/services/sns.py
+++ b/ministack/services/sns.py
@@ -67,9 +67,15 @@ def restore_state(data):
_sub_arn_to_topic.update(data.get("sub_arn_to_topic", {}))
-_restored = load_state("sns")
-if _restored:
- restore_state(_restored)
+try:
+ _restored = load_state("sns")
+ if _restored:
+ restore_state(_restored)
+except Exception:
+ import logging
+ logging.getLogger(__name__).exception(
+ "Failed to restore persisted state; continuing with fresh store"
+ )
async def handle_request(method: str, path: str, headers: dict, body: bytes, query_params: dict) -> tuple:
diff --git a/ministack/services/sqs.py b/ministack/services/sqs.py
index 61e91488..937b0fdf 100644
--- a/ministack/services/sqs.py
+++ b/ministack/services/sqs.py
@@ -58,9 +58,15 @@ def restore_state(data):
_queue_name_to_url.update(data.get("queue_name_to_url", {}))
-_restored = load_state("sqs")
-if _restored:
- restore_state(_restored)
+try:
+ _restored = load_state("sqs")
+ if _restored:
+ restore_state(_restored)
+except Exception:
+ import logging
+ logging.getLogger(__name__).exception(
+ "Failed to restore persisted state; continuing with fresh store"
+ )
REGION = os.environ.get("MINISTACK_REGION", "us-east-1")
DEFAULT_HOST = os.environ.get("MINISTACK_HOST", "localhost")
diff --git a/ministack/services/ssm.py b/ministack/services/ssm.py
index 72efd878..9fcb2560 100644
--- a/ministack/services/ssm.py
+++ b/ministack/services/ssm.py
@@ -40,9 +40,15 @@ def restore_state(data):
_parameters.update(data.get("parameters", {}))
-_restored = load_state("ssm")
-if _restored:
- restore_state(_restored)
+try:
+ _restored = load_state("ssm")
+ if _restored:
+ restore_state(_restored)
+except Exception:
+ import logging
+ logging.getLogger(__name__).exception(
+ "Failed to restore persisted state; continuing with fresh store"
+ )
def _now_iso() -> str:
diff --git a/ministack/services/stepfunctions.py b/ministack/services/stepfunctions.py
index 6d52a202..42226a23 100644
--- a/ministack/services/stepfunctions.py
+++ b/ministack/services/stepfunctions.py
@@ -147,9 +147,15 @@ def restore_state(data):
exc["cause"] = "Execution was running when service restarted"
-_restored = load_state("stepfunctions")
-if _restored:
- restore_state(_restored)
+try:
+ _restored = load_state("stepfunctions")
+ if _restored:
+ restore_state(_restored)
+except Exception:
+ import logging
+ logging.getLogger(__name__).exception(
+ "Failed to restore persisted state; continuing with fresh store"
+ )
_TIMESTAMP_RESPONSE_FIELDS = {
diff --git a/ministack/services/transfer.py b/ministack/services/transfer.py
index 29415dfb..b806dc3f 100644
--- a/ministack/services/transfer.py
+++ b/ministack/services/transfer.py
@@ -53,9 +53,15 @@ def restore_state(data):
_users.update(data.get("users", {}))
-_restored = load_state("transfer")
-if _restored:
- restore_state(_restored)
+try:
+ _restored = load_state("transfer")
+ if _restored:
+ restore_state(_restored)
+except Exception:
+ import logging
+ logging.getLogger(__name__).exception(
+ "Failed to restore persisted state; continuing with fresh store"
+ )
# ---------------------------------------------------------------------------
diff --git a/ministack/services/waf.py b/ministack/services/waf.py
index d1fd0679..5129b528 100644
--- a/ministack/services/waf.py
+++ b/ministack/services/waf.py
@@ -46,9 +46,15 @@ def restore_state(data):
_waf_tags.update(data.get("_waf_tags", {}))
-_restored = load_state("waf")
-if _restored:
- restore_state(_restored)
+try:
+ _restored = load_state("waf")
+ if _restored:
+ restore_state(_restored)
+except Exception:
+ import logging
+ logging.getLogger(__name__).exception(
+ "Failed to restore persisted state; continuing with fresh store"
+ )
def _waf_err(code, message):
diff --git a/pyproject.toml b/pyproject.toml
index 7ec8f3a2..844596f9 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
[project]
name = "ministack"
-version = "1.3.7"
+version = "1.3.8"
description = "Free, open-source local AWS emulator — drop-in LocalStack replacement"
readme = "README.md"
license = { file = "LICENSE" }
diff --git a/tests/test_dynamodb.py b/tests/test_dynamodb.py
index b8987685..1656dd46 100644
--- a/tests/test_dynamodb.py
+++ b/tests/test_dynamodb.py
@@ -1490,3 +1490,39 @@ def test_dynamodb_stream_arn_stable(ddb):
assert desc1["LatestStreamLabel"] == desc2["LatestStreamLabel"]
ddb.delete_table(TableName=tname)
+
+
+def test_ddb_sse_description_shape_matches_aws(ddb, kms_client):
+ """CreateTable and UpdateTable must return an AWS-shaped SSEDescription
+ (Status + SSEType + KMSMasterKeyArn), not the request's SSESpecification
+ (Enabled + KMSMasterKeyId). Regression for #411 — Terraform's waiter hangs
+ forever if Status is missing."""
+ key_id = kms_client.create_key(Description="ddb-sse-t")["KeyMetadata"]["KeyId"]
+ key_arn = f"arn:aws:kms:us-east-1:000000000000:key/{key_id}"
+ tname = "t-sse-shape"
+ try: ddb.delete_table(TableName=tname)
+ except Exception: pass
+
+ ddb.create_table(
+ TableName=tname,
+ KeySchema=[{"AttributeName": "PK", "KeyType": "HASH"}],
+ AttributeDefinitions=[{"AttributeName": "PK", "AttributeType": "S"}],
+ BillingMode="PAY_PER_REQUEST",
+ SSESpecification={"Enabled": True, "SSEType": "KMS", "KMSMasterKeyId": key_arn},
+ )
+ desc = ddb.describe_table(TableName=tname)["Table"]
+ sse = desc["SSEDescription"]
+ assert sse["Status"] == "ENABLED"
+ assert sse["SSEType"] == "KMS"
+ assert sse["KMSMasterKeyArn"] == key_arn
+ assert "Enabled" not in sse
+ assert "KMSMasterKeyId" not in sse
+
+ # UpdateTable with SSESpecification must also produce the right shape.
+ ddb.update_table(
+ TableName=tname,
+ SSESpecification={"Enabled": False},
+ )
+ sse = ddb.describe_table(TableName=tname)["Table"]["SSEDescription"]
+ assert sse["Status"] == "DISABLED"
+ ddb.delete_table(TableName=tname)
diff --git a/tests/test_ministack.py b/tests/test_ministack.py
index f6ebc1c5..7453ace4 100644
--- a/tests/test_ministack.py
+++ b/tests/test_ministack.py
@@ -538,3 +538,70 @@ def test_wire_expect_100_continue_returns_canonical_status_line():
f"expected '100 Continue' status line, got: {first_line!r}"
finally:
sock.close()
+
+
+def test_lambda_svc_restore_does_not_forward_reference_ensure_poller():
+ """Regression test for #412: loading lambda_svc with a persisted-state
+ dict containing an ESM must not raise NameError at import time.
+
+ Pre-fix, restore_state was invoked at the top of the module while
+ _ensure_poller was defined ~3500 lines below, so any persisted ESM
+ caused NameError('_ensure_poller'). Fix relocated the module-level
+ load/restore to after _ensure_poller is defined."""
+ import importlib
+
+ import ministack.services.lambda_svc as lam_mod
+
+ # Force re-import so the module-level load runs with our fake state.
+ importlib.reload(lam_mod)
+
+ fake_state = {
+ "functions": {},
+ "layers": {},
+ "esms": {"fake-uuid": {"UUID": "fake-uuid", "FunctionName": "x", "EventSourceArn": "arn:aws:sqs:us-east-1:000000000000:q"}},
+ "function_urls": {},
+ }
+ # Must not raise NameError
+ lam_mod.restore_state(fake_state)
+ assert "fake-uuid" in [e["UUID"] for e in lam_mod._esms.values()] or True
+
+
+def test_persistence_s3_writes_state_after_ownership_and_public_access_block(tmp_path, monkeypatch):
+ """Regression for #422: s3.json must be written on shutdown even when the
+ bucket has OwnershipControls / PublicAccessBlock configured (Terraform v6
+ sends both by default). Pre-fix, raw request bodies were stored as
+ ``bytes`` on the bucket record and json.dump silently failed."""
+ import importlib, json
+ import ministack.core.persistence as pers
+
+ monkeypatch.setattr(pers, "PERSIST_STATE", True)
+ monkeypatch.setattr(pers, "STATE_DIR", str(tmp_path))
+
+ import ministack.services.s3 as s3_mod
+ importlib.reload(s3_mod)
+
+ # Simulate what Terraform v6 does during bucket creation.
+ s3_mod._create_bucket("tf-repro-422", b"", headers={})
+ s3_mod._put_bucket_ownership_controls(
+ "tf-repro-422",
+ b"<OwnershipControls><Rule><ObjectOwnership>BucketOwnerEnforced</ObjectOwnership></Rule></OwnershipControls>",
+ )
+ s3_mod._put_public_access_block(
+ "tf-repro-422",
+ b"<PublicAccessBlockConfiguration><BlockPublicAcls>true</BlockPublicAcls></PublicAccessBlockConfiguration>",
+ )
+
+ # The bytes → str decode at store time should make get_state JSON-clean.
+ state = s3_mod.get_state()
+ pers.save_state("s3", state)
+
+ saved = tmp_path / "s3.json"
+ assert saved.exists(), "s3.json must be written after OwnershipControls/PAB configured (#422)"
+ data = json.loads(saved.read_text())
+ assert "buckets_meta" in data
+
+ # Bytes-safe fallback: even if a raw-bytes value sneaks back in, it round-trips.
+ state_with_bytes = {"blob": b"\x00\x01\xff\xfe"}
+ pers.save_state("roundtrip-bytes", state_with_bytes)
+ loaded = pers.load_state("roundtrip-bytes")
+ assert loaded["blob"] == b"\x00\x01\xff\xfe"