Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.
Python awsaws-emulatoraws-localaws-sdkdevtoolsdockerdynamodbec2emulatorlambdalocalstacklocalstack-alternativeministackmock-awsopen-sourcepythons3sqsterraform
fix(stepfunctions): make CreateStateMachine idempotent (#1510)
Maximilian Bandlea300306255b7
diff --git a/ministack/services/stepfunctions.py b/ministack/services/stepfunctions.pyindex b6a0eb63..79febcd9 100644--- a/ministack/services/stepfunctions.py+++ b/ministack/services/stepfunctions.py@@ -316,6 +316,20 @@ def _create_state_machine(data):arn = f"arn:aws:states:{get_region()}:{get_account_id()}:stateMachine:{name}"if arn in _state_machines:+ # Creating the same machine twice is idempotent on AWS and makes SDK retry safe+ # It checks the sfn itself: definition, type, logging and whether the call publishes+ existing = _state_machines[arn]+ publishes = bool(data.get("publish"))+ if (data.get("definition", "{}") == existing["definition"]+ and data.get("type", "STANDARD") == existing["type"]+ and data.get("loggingConfiguration",+ {"level": "OFF", "includeExecutionData": False})+ == existing["loggingConfiguration"]+ and publishes == bool(existing.get("createVersionArn"))):+ response = {"stateMachineArn": arn, "creationDate": existing["creationDate"]}+ if publishes:+ response["stateMachineVersionArn"] = existing["createVersionArn"]+ return json_response(response)return error_response_json("StateMachineAlreadyExists",f"State machine {name} already exists", 400)@@ -362,6 +376,7 @@ def _create_state_machine(data):_state_machines[arn]["lastVersionNumber"] += 1next_number = _state_machines[arn]["lastVersionNumber"]version_arn = f"{arn}:{next_number}"+ _state_machines[arn]["createVersionArn"] = version_arn_state_machine_versions[version_arn] = {"stateMachineVersionArn": version_arn,"stateMachineArn": arn,diff --git a/tests/test_stepfunctions.py b/tests/test_stepfunctions.pyindex d402fcc6..729923d6 100644--- a/tests/test_stepfunctions.py+++ b/tests/test_stepfunctions.py@@ -4914,23 +4914,43 @@ def test_sfn_update_state_machine(sfn):assert desc["definition"] == defn_v2def test_sfn_create_duplicate_name(sfn):- """CreateStateMachine with duplicate name should fail."""+ """An identical CreateStateMachine is idempotent; a name reused for another machine is not."""defn = json.dumps({"StartAt": "X","States": {"X": {"Type": "Pass", "End": True}},})- sfn.create_state_machine(+ first = sfn.create_state_machine(+ name="sfn-dup-err-test",+ definition=defn,+ roleArn="arn:aws:iam::000000000000:role/R",+ )+ # What an SDK retry looks like: the same call again answers with the machine it already made.+ again = sfn.create_state_machine(name="sfn-dup-err-test",definition=defn,roleArn="arn:aws:iam::000000000000:role/R",)+ assert again["stateMachineArn"] == first["stateMachineArn"]+ assert again["creationDate"] == first["creationDate"]++ # A differing roleArn is ignored rather than rejected, and is not written over either.+ other_role = sfn.create_state_machine(+ name="sfn-dup-err-test",+ definition=defn,+ roleArn="arn:aws:iam::000000000000:role/Other",+ )+ assert other_role["stateMachineArn"] == first["stateMachineArn"]+ assert sfn.describe_state_machine(+ stateMachineArn=first["stateMachineArn"])["roleArn"].endswith(":role/R")++ # The reserved case: the same name carrying a different machine.with pytest.raises(ClientError) as exc:sfn.create_state_machine(name="sfn-dup-err-test",- definition=defn,+ definition=json.dumps({"StartAt": "Y", "States": {"Y": {"Type": "Pass", "End": True}}}),roleArn="arn:aws:iam::000000000000:role/R",)- assert "StateMachineAlreadyExists" in str(exc.value) or "Conflict" in str(exc.value) or exc.value.response["Error"]["Code"]+ assert exc.value.response["Error"]["Code"] == "StateMachineAlreadyExists"def test_sfn_describe_not_found(sfn):"""DescribeStateMachine on non-existent ARN should fail."""