Skip to content

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

fix(iot): make CreateThingType idempotent for identical re-creates (#1349)

iot-rocket02b3244a531a
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 35a3fcec..7c561ec7 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,7 @@ Versioning follows [Semantic Versioning](https://semver.org/).
## [Unreleased]
### Fixed
+- **IoT — `CreateThingType` is idempotent for identical re-creates** — re-creating an existing thing type always returned `ResourceAlreadyExistsException`, so a retried request or a re-run provisioning script failed where AWS succeeds. The same `thingTypeProperties` now return the existing ids — absent, `null` and empty compare equal, `searchableAttributes` is unordered — and only a real mismatch keeps the `409`, as `CreateThing` already did. Contributed by @iot-rocket.
- **EC2 — `RevokeSecurityGroupIngress` / `RevokeSecurityGroupEgress` honour `SecurityGroupRuleIds`** — both read only `IpPermissions`, so a revoke by rule id (as Terraform does) returned `Return=true` while removing nothing, leaving the rule in place forever. Ids now resolve against the group's `sgr-*` rules, which are removed with their tags and echoed in `revokedSecurityGroupRuleSet`; one unknown id rejects the whole call with `InvalidSecurityGroupRuleId.NotFound`. Contributed by @iot-rocket.
## [1.4.16] — 2026-08-12
diff --git a/ministack/services/iot.py b/ministack/services/iot.py
index fab5e401..27a46859 100644
--- a/ministack/services/iot.py
+++ b/ministack/services/iot.py
@@ -644,22 +644,49 @@ def _handle_thing_type(method: str, path: str, body: bytes, qp: dict) -> tuple:
)
+def _thing_type_props_identity(props: dict) -> tuple:
+ """Comparable identity of a thingTypeProperties block.
+
+ Covers every member the API models, so a re-create differing only in
+ mqtt5Configuration is a genuine conflict rather than a silent no-op.
+ Absent, None and empty compare equal, and searchableAttributes is
+ compared as a set because AWS treats it as unordered.
+ """
+ return (
+ props.get("thingTypeDescription") or None,
+ frozenset(props.get("searchableAttributes") or ()),
+ json.dumps(props.get("mqtt5Configuration") or None, sort_keys=True),
+ )
+
+
def _create_thing_type(name: str, payload: dict) -> tuple:
- if name in _thing_types:
+ props = payload.get("thingTypeProperties") or {}
+ existing = _thing_types.get(name)
+ if existing is not None:
+ # Idempotent: same properties return success; different properties 409.
+ stored = existing.get("thingTypeProperties") or {}
+ if _thing_type_props_identity(stored) == _thing_type_props_identity(props):
+ return json_response({
+ "thingTypeName": existing["thingTypeName"],
+ "thingTypeArn": existing["thingTypeArn"],
+ "thingTypeId": existing["thingTypeId"],
+ })
return error_response_json(
"ResourceAlreadyExistsException",
- f"ThingType {name!r} already exists",
+ f"ThingType {name!r} already exists with different properties",
409,
)
- props = payload.get("thingTypeProperties") or {}
+ stored_props = {
+ "thingTypeDescription": props.get("thingTypeDescription"),
+ "searchableAttributes": list(props.get("searchableAttributes", []) or []),
+ }
+ if props.get("mqtt5Configuration"):
+ stored_props["mqtt5Configuration"] = props["mqtt5Configuration"]
record = {
"thingTypeName": name,
"thingTypeId": new_uuid(),
"thingTypeArn": _thing_type_arn(name),
- "thingTypeProperties": {
- "thingTypeDescription": props.get("thingTypeDescription"),
- "searchableAttributes": list(props.get("searchableAttributes", []) or []),
- },
+ "thingTypeProperties": stored_props,
"thingTypeMetadata": {
"deprecated": False,
"deprecationDate": None,
@@ -667,6 +694,7 @@ def _create_thing_type(name: str, payload: dict) -> tuple:
},
}
_thing_types[name] = record
+ logger.info("IoT Thing Type created: %s", name)
return json_response({
"thingTypeName": name,
"thingTypeArn": record["thingTypeArn"],
diff --git a/tests/test_iot.py b/tests/test_iot.py
index 5dd4772e..f34b52b9 100644
--- a/tests/test_iot.py
+++ b/tests/test_iot.py
@@ -179,6 +179,85 @@ def test_iot_thing_type_lifecycle(iot_client):
assert ei.value.response["Error"]["Code"] == "ResourceNotFoundException"
+def test_iot_create_thing_type_idempotent_recreate(iot_client):
+ name = _unique("type")
+ first = iot_client.create_thing_type(thingTypeName=name)
+
+ # Identical re-create (no properties) returns 200 with the existing ids.
+ again = iot_client.create_thing_type(thingTypeName=name)
+ assert again["thingTypeId"] == first["thingTypeId"]
+ assert again["thingTypeArn"] == first["thingTypeArn"]
+
+ # Explicit empty properties count as identical to absent ones.
+ empty = iot_client.create_thing_type(
+ thingTypeName=name,
+ thingTypeProperties={"searchableAttributes": []},
+ )
+ assert empty["thingTypeId"] == first["thingTypeId"]
+
+ iot_client.deprecate_thing_type(thingTypeName=name)
+ iot_client.delete_thing_type(thingTypeName=name)
+
+
+def test_iot_create_thing_type_conflicting_recreate_rejected(iot_client):
+ name = _unique("type")
+ iot_client.create_thing_type(thingTypeName=name)
+ with pytest.raises(ClientError) as ei:
+ iot_client.create_thing_type(
+ thingTypeName=name,
+ thingTypeProperties={"thingTypeDescription": "different"},
+ )
+ assert ei.value.response["Error"]["Code"] == "ResourceAlreadyExistsException"
+ iot_client.deprecate_thing_type(thingTypeName=name)
+ iot_client.delete_thing_type(thingTypeName=name)
+
+
+def test_iot_create_thing_type_idempotency_covers_all_properties(iot_client):
+ """The comparison spans every modelled property and treats
+ searchableAttributes as unordered: a re-create differing only in
+ mqtt5Configuration conflicts, while the same attributes in another order
+ are idempotent."""
+ name = _unique("type")
+ mqtt5 = {"propagatingAttributes": [
+ {"userPropertyKey": "region", "thingAttribute": "site"},
+ ]}
+ first = iot_client.create_thing_type(
+ thingTypeName=name,
+ thingTypeProperties={
+ "searchableAttributes": ["site", "region"],
+ "mqtt5Configuration": mqtt5,
+ },
+ )
+
+ same = iot_client.create_thing_type(
+ thingTypeName=name,
+ thingTypeProperties={
+ "searchableAttributes": ["region", "site"],
+ "mqtt5Configuration": mqtt5,
+ },
+ )
+ assert same["thingTypeId"] == first["thingTypeId"]
+
+ with pytest.raises(ClientError) as ei:
+ iot_client.create_thing_type(
+ thingTypeName=name,
+ thingTypeProperties={
+ "searchableAttributes": ["site", "region"],
+ "mqtt5Configuration": {"propagatingAttributes": [
+ {"userPropertyKey": "region", "thingAttribute": "other"},
+ ]},
+ },
+ )
+ assert ei.value.response["Error"]["Code"] == "ResourceAlreadyExistsException"
+
+ # The first create's configuration is the one that stuck.
+ described = iot_client.describe_thing_type(thingTypeName=name)
+ assert described["thingTypeProperties"]["mqtt5Configuration"] == mqtt5
+
+ iot_client.deprecate_thing_type(thingTypeName=name)
+ iot_client.delete_thing_type(thingTypeName=name)
+
+
def test_iot_delete_thing_type_active_rejected(iot_client):
name = _unique("type")
iot_client.create_thing_type(thingTypeName=name)