Skip to content

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

fix/extend-cfn-status (#1456)

Nahuel Nucera7d8d305aead1
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8ef26db3..7d2e0483 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -10,7 +10,7 @@ Versioning follows [Semantic Versioning](https://semver.org/).
### Fixed
- **Step Functions — an unimplemented optimized service integration now fails the execution instead of silently succeeding** — a Task using an `arn:aws:states:::<service>:<action>` integration MiniStack does not implement fell through to echoing its input back as a `SUCCEEDED` result, so a state machine whose side effect never ran still reported success. Such a task now fails with `States.Runtime` naming the unimplemented resource; implemented integrations and the generic `aws-sdk:` form are unaffected. Reported by @iwasakar.
- **CloudFormation — an update that would replace a custom-named resource is refused instead of silently executing and destroying data** — a stack update requiring replacement of a resource with an explicit physical name (for example changing a DynamoDB key attribute's type on a table that sets `TableName`) executed the replacement, reported `UPDATE_COMPLETE`, and dropped the table's rows. Such an update now fails and rolls back to `UPDATE_ROLLBACK_COMPLETE` with the message real CloudFormation uses (`CloudFormation cannot update a stack when a custom-named resource requires replacing. Rename <name> and update the stack again.`), leaving the resource and its data untouched; renaming the resource is still the way to force the replacement. Reported by @iot-rocket.
-- **CloudFormation — change sets report a valid status, fail when their execution fails, and do not outlive their stack** — `ExecuteChangeSet` wrote `EXECUTE_COMPLETE` into the change set's `Status` (a ChangeSetStatus, whose values are only `CREATE_*`/`DELETE_*`/`FAILED`) and did so at schedule time, so the CDK rejected the change set as not ready (it gates on `Status == CREATE_COMPLETE`) and a failed execution never surfaced. `Status` now stays `CREATE_COMPLETE`, and `ExecutionStatus` moves to `EXECUTE_COMPLETE` or `EXECUTE_FAILED` based on the deployment outcome. Deleting a stack now removes its change sets, and a duplicate change-set name is refused with `AlreadyExistsException`, so a leftover executed change set no longer shadows a same-named one on a re-created stack. Reported by @iot-rocket.
+- **CloudFormation — change sets report a valid status, fail when their execution fails, and do not outlive their stack** — `ExecuteChangeSet` wrote `EXECUTE_COMPLETE` into the change set's `Status` (a ChangeSetStatus, whose values are only `CREATE_*`/`DELETE_*`/`FAILED`) and did so at schedule time, so the CDK rejected the change set as not ready (it gates on `Status == CREATE_COMPLETE`) and a failed execution never surfaced. `Status` now stays `CREATE_COMPLETE`, and `ExecutionStatus` moves to `EXECUTE_COMPLETE` or `EXECUTE_FAILED` based on the deployment outcome. Deleting a stack now removes its change sets, and a duplicate change-set name is refused with `AlreadyExistsException`, so a leftover executed change set no longer shadows a same-named one on a re-created stack. A change set with no changes now ends `FAILED` with the reason real CloudFormation gives (`The submitted information didn't contain changes. Submit different information to create a change set.`) instead of reporting `CREATE_COMPLETE`, and executing a change set deletes the stack's other now-invalid change sets. A missing change set now returns the AWS `ChangeSetNotFound` error (404) instead of a non-AWS `ChangeSetNotFoundException` (400), and a direct `UpdateStack` marks the stack's pending change sets `OBSOLETE`. Reported by @iot-rocket.
- **RDS — Aurora engine versions are validated on non-create writes and global inheritance** — `ModifyDBInstance`, `ModifyDBCluster`, `CreateGlobalCluster`, and `ModifyGlobalCluster` stored arbitrary Aurora engine versions that the shared engine catalog did not advertise, and `CreateDBCluster` could inherit such a version from legacy global-cluster state even though supplying it explicitly was rejected. All paths now use the shared catalog validator and reject unknown versions before mutating any state: create paths keep the create-time `Cannot find version {version} for {engine}`, while the modify paths report the upgrade-target shape real AWS uses (`InvalidParameterCombination` / `Cannot find upgrade target from {current} with requested version {requested}.`, as seen in real `ModifyDBCluster` transcripts). `ModifyGlobalCluster` now also propagates an accepted version to every member cluster the way real AWS applies global-database version changes, and `ModifyDBCluster` refuses to move a member cluster to a different major than its global cluster — the combination `CreateDBCluster` already refuses to produce. Contributed by @kiran01bm.
### Added
- **RDS — replicated Aurora PostgreSQL readers survive StopDBCluster / StartDBCluster and warm boot** — stopping a cluster with replicating readers was refused with `InvalidDBClusterStateFault` (the follow-up slice of #1325 the reader feature shipped without), and a MiniStack restart demoted a persisted reader to an alias of the writer's shared container. `StopDBCluster` now stops the writer's container and every reader-owned standby container — a stop that fails on any of them rolls back what it stopped and reports the failure instead of publishing `stopped` — and `StartDBCluster` restarts the writer, waits for it to accept authenticated connections, then revives each reader by re-cloning from the writer at its current address, keeping the member's identity, endpoint, and volume name (reader compute owns no durable state, as on Aurora, so revival recreates the container rather than restarting it). The same revival runs on warm boot. A revival that genuinely fails lands the cluster back on `stopped` for retry; only when compute deliberately cannot come back (control-plane-only restore, or the replication flag withdrawn between runs) does the member fall back to aliasing the writer, releasing its former container and volume, instead of advertising an endpoint nothing serves. Contributed by @kiran01bm.
diff --git a/ministack/services/cloudformation/changesets.py b/ministack/services/cloudformation/changesets.py
index d2a16a0a..113475ad 100644
--- a/ministack/services/cloudformation/changesets.py
+++ b/ministack/services/cloudformation/changesets.py
@@ -181,13 +181,24 @@ def _create_change_set(params):
f"changeSet/{cs_name}/{new_uuid()}"
)
+ if changes:
+ _cs_status, _cs_exec, _cs_reason = "CREATE_COMPLETE", "AVAILABLE", ""
+ else:
+ # Real AWS: a change set with no changes ends FAILED and cannot be
+ # executed (ExecutionStatus UNAVAILABLE), with this exact reason.
+ _cs_status, _cs_exec = "FAILED", "UNAVAILABLE"
+ _cs_reason = (
+ "The submitted information didn't contain changes. "
+ "Submit different information to create a change set."
+ )
change_set = {
"ChangeSetId": cs_id,
"ChangeSetName": cs_name,
"StackId": stack_id,
"StackName": stack_name,
- "Status": "CREATE_COMPLETE",
- "ExecutionStatus": "AVAILABLE",
+ "Status": _cs_status,
+ "ExecutionStatus": _cs_exec,
+ "StatusReason": _cs_reason,
"CreationTime": now_iso(),
"Description": _p(params, "Description", ""),
"ChangeSetType": cs_type,
@@ -217,8 +228,8 @@ def _describe_change_set(params):
stack_name = _p(params, "StackName")
_, cs = _find_change_set(cs_name, stack_name)
if not cs:
- return _error("ChangeSetNotFoundException",
- f"ChangeSet [{cs_name}] does not exist")
+ return _error("ChangeSetNotFound",
+ f"ChangeSet [{cs_name}] does not exist", 404)
params_xml = ""
for p in cs.get("Parameters", []):
@@ -256,6 +267,7 @@ def _describe_change_set(params):
f"<StackId>{_esc(cs['StackId'])}</StackId>"
f"<StackName>{_esc(cs['StackName'])}</StackName>"
f"<Status>{cs['Status']}</Status>"
+ f"<StatusReason>{_esc(cs.get('StatusReason', ''))}</StatusReason>"
f"<ExecutionStatus>{cs['ExecutionStatus']}</ExecutionStatus>"
f"<CreationTime>{cs['CreationTime']}</CreationTime>"
f"<Description>{_esc(cs.get('Description', ''))}</Description>"
@@ -293,10 +305,10 @@ def _execute_change_set(params):
from ministack.services.cloudformation import _stacks
cs_name = _p(params, "ChangeSetName")
stack_name = _p(params, "StackName")
- _, cs = _find_change_set(cs_name, stack_name)
+ _executed_cs_id, cs = _find_change_set(cs_name, stack_name)
if not cs:
- return _error("ChangeSetNotFoundException",
- f"ChangeSet [{cs_name}] does not exist")
+ return _error("ChangeSetNotFound",
+ f"ChangeSet [{cs_name}] does not exist", 404)
if cs["ExecutionStatus"] != "AVAILABLE":
return _error("InvalidChangeSetStatusException",
@@ -358,6 +370,13 @@ def _execute_change_set(params):
stack_id,
)
+ # Real AWS deletes the stack's other change sets on execute — they are no
+ # longer valid for the updated stack. #1418
+ from ministack.services.cloudformation import _change_sets as _cs_store
+ for _cid in [c for c, v in _cs_store.items()
+ if v.get("StackId") == stack_id and c != _executed_cs_id]:
+ _cs_store.pop(_cid, None)
+
# ExecutionStatus stays EXECUTE_IN_PROGRESS until the deploy finishes, when
# _track_change_set_execution sets EXECUTE_COMPLETE or EXECUTE_FAILED. Status
# is the ChangeSetStatus and stays CREATE_COMPLETE: EXECUTE_COMPLETE is not a
@@ -375,8 +394,8 @@ def _delete_change_set(params):
stack_name = _p(params, "StackName")
cs_id, cs = _find_change_set(cs_name, stack_name)
if not cs_id:
- return _error("ChangeSetNotFoundException",
- f"ChangeSet [{cs_name}] does not exist")
+ return _error("ChangeSetNotFound",
+ f"ChangeSet [{cs_name}] does not exist", 404)
_change_sets.pop(cs_id, None)
return _xml(200, "DeleteChangeSetResponse", "")
@@ -400,6 +419,7 @@ def _list_change_sets(params):
f"<StackId>{_esc(cs['StackId'])}</StackId>"
f"<StackName>{_esc(cs['StackName'])}</StackName>"
f"<Status>{cs['Status']}</Status>"
+ f"<StatusReason>{_esc(cs.get('StatusReason', ''))}</StatusReason>"
f"<ExecutionStatus>{cs['ExecutionStatus']}</ExecutionStatus>"
f"<CreationTime>{cs['CreationTime']}</CreationTime>"
f"<Description>{_esc(cs.get('Description', ''))}</Description>"
diff --git a/ministack/services/cloudformation/handlers.py b/ministack/services/cloudformation/handlers.py
index dc67901a..ec99d425 100644
--- a/ministack/services/cloudformation/handlers.py
+++ b/ministack/services/cloudformation/handlers.py
@@ -528,6 +528,15 @@ def _update_stack(params):
}
stack_id = stack["StackId"]
+
+ # A direct stack update supersedes any pending change set on the stack —
+ # real AWS marks them OBSOLETE (they no longer reflect the stack). #1418
+ from ministack.services.cloudformation import _change_sets
+ for _cs in _change_sets.values():
+ if (_cs.get("StackId") == stack_id
+ and _cs.get("ExecutionStatus") == "AVAILABLE"):
+ _cs["ExecutionStatus"] = "OBSOLETE"
+
stack["StackStatus"] = "UPDATE_IN_PROGRESS"
stack["LastUpdatedTime"] = now_iso()
stack["_template_body"] = template_body
diff --git a/tests/test_cfn.py b/tests/test_cfn.py
index a2793046..df8950fc 100644
--- a/tests/test_cfn.py
+++ b/tests/test_cfn.py
@@ -169,7 +169,7 @@ def test_cfn_region_scopes_stacks_change_sets_and_events():
assert west.list_change_sets(StackName=stack_name)["Summaries"] == []
with pytest.raises(ClientError) as exc:
west.describe_change_set(ChangeSetName=change_set_id)
- assert exc.value.response["Error"]["Code"] == "ChangeSetNotFoundException"
+ assert exc.value.response["Error"]["Code"] == "ChangeSetNotFound"
east.delete_stack(StackName=stack_name)
assert _wait_stack(east, stack_name)["StackStatus"] == "DELETE_COMPLETE"
@@ -914,6 +914,74 @@ def test_cfn_change_set_status_execution_and_lifecycle(cfn):
cfn.delete_stack(StackName=S)
+def test_cfn_change_set_no_changes_is_failed(cfn, ssm):
+ """A change set with no changes ends FAILED with the real-AWS reason, not
+ CREATE_COMPLETE/AVAILABLE (#1418)."""
+ S = "cfn-1418-nochg"
+ tpl = json.dumps({"Resources": {"P": {"Type": "AWS::SSM::Parameter",
+ "Properties": {"Name": "/cfn-1418-nochg/p", "Type": "String",
+ "Value": "v"}}}})
+ cfn.create_stack(StackName=S, TemplateBody=tpl)
+ _wait_stack(cfn, S)
+ try:
+ cfn.create_change_set(StackName=S, ChangeSetName="noop",
+ ChangeSetType="UPDATE", TemplateBody=tpl)
+ d = cfn.describe_change_set(ChangeSetName="noop", StackName=S)
+ assert d["Status"] == "FAILED"
+ assert d["ExecutionStatus"] == "UNAVAILABLE"
+ assert "didn't contain changes" in d["StatusReason"]
+ finally:
+ cfn.delete_stack(StackName=S)
+
+
+def test_cfn_execute_change_set_deletes_sibling_change_sets(cfn, ssm):
+ """Executing a change set deletes the stack's other change sets — they are
+ no longer valid for the updated stack (#1418)."""
+ S = "cfn-1418-sib"
+ def tpl(v):
+ return json.dumps({"Resources": {"P": {"Type": "AWS::SSM::Parameter",
+ "Properties": {"Name": "/cfn-1418-sib/p", "Type": "String",
+ "Value": v}}}})
+ cfn.create_stack(StackName=S, TemplateBody=tpl("v0"))
+ _wait_stack(cfn, S)
+ try:
+ cfn.create_change_set(StackName=S, ChangeSetName="cs-a",
+ ChangeSetType="UPDATE", TemplateBody=tpl("v1"))
+ cfn.create_change_set(StackName=S, ChangeSetName="cs-b",
+ ChangeSetType="UPDATE", TemplateBody=tpl("v2"))
+ cfn.execute_change_set(StackName=S, ChangeSetName="cs-a")
+ with pytest.raises(ClientError) as exc:
+ cfn.describe_change_set(ChangeSetName="cs-b", StackName=S)
+ assert "ChangeSetNotFound" in exc.value.response["Error"]["Code"]
+ _wait_stack(cfn, S)
+ finally:
+ cfn.delete_stack(StackName=S)
+
+
+def test_cfn_direct_update_marks_pending_change_sets_obsolete(cfn, ssm):
+ """A direct UpdateStack supersedes any pending change set — it becomes
+ OBSOLETE, not left AVAILABLE (#1418)."""
+ S = "cfn-1418-obs"
+ def tpl(v):
+ return json.dumps({"Resources": {"P": {"Type": "AWS::SSM::Parameter",
+ "Properties": {"Name": "/cfn-1418-obs/p", "Type": "String",
+ "Value": v}}}})
+ cfn.create_stack(StackName=S, TemplateBody=tpl("v0"))
+ _wait_stack(cfn, S)
+ try:
+ cfn.create_change_set(StackName=S, ChangeSetName="pending",
+ ChangeSetType="UPDATE", TemplateBody=tpl("v1"))
+ assert cfn.describe_change_set(
+ ChangeSetName="pending", StackName=S)["ExecutionStatus"] == "AVAILABLE"
+ # A direct update (not via the change set) supersedes it.
+ cfn.update_stack(StackName=S, TemplateBody=tpl("v2"))
+ d = cfn.describe_change_set(ChangeSetName="pending", StackName=S)
+ assert d["ExecutionStatus"] == "OBSOLETE"
+ _wait_stack(cfn, S)
+ finally:
+ cfn.delete_stack(StackName=S)
+
+
def test_cfn_ssm_parameter_value_type_resolves_stored_value(cfn, ssm, sqs):
"""A `AWS::SSM::Parameter::Value<String>` template parameter's Default/
provided value is an SSM parameter *name*, not the value itself — real