Skip to content

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

fix(cloudformation): send change set members as AWS does for each action (#1860)

iot-rocket7614fc3c6989
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3872f1e5..07bb27a4 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,13 @@ Versioning follows [Semantic Versioning](https://semver.org/).
## [Unreleased]
+### Fixed
+
+- **CloudFormation — change set members follow the action** — `Add` and `Remove` changes carried `Replacement: False`, a `Remove` had no physical id, `PolicyAction` was never sent and a `Metadata` or policy detail had no `RequiresRecreation`. `Remove` and `Modify` now name the physical resource, a `Remove` answers `PolicyAction: Delete` and a replacing `Modify` `ReplaceAndDelete` unless the resource retains or snapshots, and attribute details answer `Never`. Contributed by @iot-rocket.
+- **CloudFormation — `AWS::SQS::Queue` applies every queue property** — `RedrivePolicy`, `RedriveAllowPolicy`, `KmsMasterKeyId`, `KmsDataKeyReusePeriodSeconds`, `SqsManagedSseEnabled`, `DeduplicationScope` and `FifoThroughputLimit` were dropped on create and update, so a dead-letter queue declared in a template never received messages, and a value SQS refuses now fails the resource instead of being stored. Contributed by @iot-rocket.
+- **Lambda — Docker executor honors timeouts above 300 seconds** — pass the configured `Timeout` to AWS RIE through `AWS_LAMBDA_FUNCTION_TIMEOUT`, preventing its default 300-second limit from ending longer invocations early. Timeout updates recycle warm containers so the RIE deadline follows the new configuration (#1844).
+- **KMS — `GenerateDataKey` returns `KeyMaterialId`** — requests without `Recipient` now include the identifier of the wrapping key material. The identifier remains stable across requests and persistence reloads, and changes when the wrapping material changes. Fixes #1853.
+- **CloudFormation — a nested stack's update deletes the resources its template drops** — a resource removed from the child template, or created by a failed child update that was rolled back, stayed in its service and in the nested stack's resource list. Contributed by @iot-rocket.
### Added
- **RDS — IAM database authentication for MySQL and Aurora MySQL** — users created `IDENTIFIED WITH AWSAuthenticationPlugin AS 'RDS'` log in with an SDK-generated token over `mysql_clear_password`, as on AWS. The instance or cluster must have `IAMDatabaseAuthenticationEnabled`; with `AUTH=true` the token and the `rds-db:connect` policy are verified too. `ModifyDBInstance` accepts `EnableIAMDatabaseAuthentication`. Contributed by @Areson.
diff --git a/ministack/services/cloudformation/changesets.py b/ministack/services/cloudformation/changesets.py
index c0b17aa2..1f69b821 100644
--- a/ministack/services/cloudformation/changesets.py
+++ b/ministack/services/cloudformation/changesets.py
@@ -497,7 +497,7 @@ def _create_change_set(params):
with _stack_region_context(stack, stack_id):
old_resolved = _resolve_props_for_diff(old_template, old_params, stack_name, stack_id)
new_resolved = _resolve_props_for_diff(template, param_values, stack_name, stack_id)
- changes = _diff_resources(old_resolved, new_resolved)
+ changes = _diff_resources(old_resolved, new_resolved, stack.get("_resources"))
import_failure = None
if cs_type == "IMPORT":
# An import describes the resources being adopted, not the template
@@ -611,8 +611,8 @@ def _describe_change_set(params):
"</member>"
)
# botocore reads an empty element as "", so a member the change does
- # not carry (an Import's Replacement, a Remove's unknown physical id)
- # is left out instead of written empty.
+ # not carry (an Import's Replacement, an Add's physical id) is left
+ # out instead of written empty.
physical_xml = (
f"<PhysicalResourceId>{_esc(rc['PhysicalResourceId'])}</PhysicalResourceId>"
if rc.get("PhysicalResourceId") else ""
@@ -620,6 +620,9 @@ def _describe_change_set(params):
replacement_xml = (
f"<Replacement>{rc['Replacement']}</Replacement>" if "Replacement" in rc else ""
)
+ policy_xml = (
+ f"<PolicyAction>{rc['PolicyAction']}</PolicyAction>" if "PolicyAction" in rc else ""
+ )
# "Resource" is the one ChangeType, and AWS reports it on every change.
changes_xml += (
"<member><Type>Resource</Type><ResourceChange>"
@@ -628,6 +631,7 @@ def _describe_change_set(params):
f"{physical_xml}"
f"<ResourceType>{_esc(rc.get('ResourceType', ''))}</ResourceType>"
f"{replacement_xml}"
+ f"{policy_xml}"
f"<Scope>{scope_xml}</Scope>"
f"<Details>{details_xml}</Details>"
"</ResourceChange></member>"
diff --git a/ministack/services/cloudformation/stacks.py b/ministack/services/cloudformation/stacks.py
index ad060462..5582efd9 100644
--- a/ministack/services/cloudformation/stacks.py
+++ b/ministack/services/cloudformation/stacks.py
@@ -933,13 +933,22 @@ _DIFFED_ATTRIBUTES = (
)
-def _diff_resources(old_template: dict, new_template: dict) -> list:
+def _policy_action(res_def: dict, attribute: str, prefix: str = "") -> dict:
+ """The PolicyAction member of a change that deletes the old resource, else none."""
+ if res_def.get(attribute, _default_resource_policy(res_def, attribute)) != "Delete":
+ return {}
+ return {"PolicyAction": prefix + "Delete"}
+
+
+def _diff_resources(old_template: dict, new_template: dict, resources: dict | None = None) -> list:
"""Diff two templates and return a list of change dicts.
A resource is a ``Modify`` when its ``Properties`` differ or when one of the
attributes in ``_DIFFED_ATTRIBUTES`` differs; each changed attribute becomes
a ``Details`` entry (``Target.Attribute``, plus the property name for
``Properties``) and is listed in ``Scope``, as the API reference defines them.
+ ``resources`` are the stack's provisioned resources, whose physical ids a
+ ``Remove`` or ``Modify`` reports.
"""
old_res = old_template.get("Resources", {})
new_res = new_template.get("Resources", {})
@@ -947,13 +956,14 @@ def _diff_resources(old_template: dict, new_template: dict) -> list:
all_keys = old_res.keys() | new_res.keys()
for key in sorted(all_keys):
+ pid = (resources or {}).get(key, {}).get("PhysicalResourceId")
+ physical = {"PhysicalResourceId": pid} if pid else {}
if key not in old_res:
changes.append({
"ResourceChange": {
"Action": "Add",
"LogicalResourceId": key,
"ResourceType": new_res[key].get("Type", ""),
- "Replacement": "False",
}
})
elif key not in new_res:
@@ -962,8 +972,8 @@ def _diff_resources(old_template: dict, new_template: dict) -> list:
"Action": "Remove",
"LogicalResourceId": key,
"ResourceType": old_res[key].get("Type", ""),
- "PhysicalResourceId": "",
- "Replacement": "False",
+ **physical,
+ **_policy_action(old_res[key], "DeletionPolicy"),
}
})
else:
@@ -984,7 +994,7 @@ def _diff_resources(old_template: dict, new_template: dict) -> list:
for attr in _DIFFED_ATTRIBUTES:
if old_res[key].get(attr) != new_res[key].get(attr):
details.append({
- "Target": {"Attribute": attr},
+ "Target": {"Attribute": attr, "RequiresRecreation": "Never"},
"Evaluation": "Static",
"ChangeSource": "DirectModification",
})
@@ -1007,7 +1017,10 @@ def _diff_resources(old_template: dict, new_template: dict) -> list:
"Action": "Modify",
"LogicalResourceId": key,
"ResourceType": new_res[key].get("Type", ""),
+ **physical,
"Replacement": replacement,
+ **(_policy_action(new_res[key], "UpdateReplacePolicy", "ReplaceAnd")
+ if replacement == "True" else {}),
"Scope": scope,
"Details": details,
}
diff --git a/tests/test_cfn.py b/tests/test_cfn.py
index abfbf5fb..cb02339c 100644
--- a/tests/test_cfn.py
+++ b/tests/test_cfn.py
@@ -25166,6 +25166,39 @@ def test_sqs_change_set_reports_the_issue_reproduction(cfn, stack):
assert _requirements(change) == {"QueueName": "Always", "DelaySeconds": "Never"}
[email protected]("edit,logical,action,replacement,policy,recreation", [
+ (lambda r, name: r.update(Extra={"Type": "AWS::SQS::Queue", "Properties": {"QueueName": name + "-b"}}),
+ "Extra", "Add", None, None, set()),
+ (lambda r, name: r.pop("Param"), "Param", "Remove", None, "Delete", set()),
+ (lambda r, name: r["Queue"]["Properties"].update(QueueName=name + "-new"),
+ "Queue", "Modify", "True", "ReplaceAndDelete", {"Always"}),
+ (lambda r, name: r["Param"]["Properties"].update(Value="b"), "Param", "Modify", "False", None, {"Never"}),
+ (lambda r, name: r["Param"].update(Metadata={"owner": "review"}), "Param", "Modify", "False", None, {"Never"}),
+], ids=["add", "remove", "modify-replace", "modify-in-place", "modify-metadata"])
+def test_change_set_members_follow_the_action(cfn, stack, edit, logical, action, replacement,
+ policy, recreation):
+ """Replacement, PhysicalResourceId, PolicyAction and RequiresRecreation are sent as AWS sends them."""
+ name = f"repl-{_uuid_mod.uuid4().hex[:10]}"
+
+ def template():
+ return {"Resources": {
+ "Queue": {"Type": "AWS::SQS::Queue", "Properties": {"QueueName": name}},
+ "Param": {"Type": "AWS::SSM::Parameter", "Properties": {"Name": name, "Type": "String", "Value": "a"}},
+ }}
+
+ stack_name = stack(template())
+ physical = {r["LogicalResourceId"]: r["PhysicalResourceId"]
+ for r in cfn.describe_stack_resources(StackName=stack_name)["StackResources"]}
+ new = template()
+ edit(new["Resources"], name)
+ change = _change(cfn, stack_name, new)
+ assert (change["LogicalResourceId"], change["Action"]) == (logical, action)
+ assert change.get("Replacement") == replacement
+ assert change.get("PolicyAction") == policy
+ assert change.get("PhysicalResourceId") == physical.get(logical)
+ assert {d["Target"].get("RequiresRecreation") for d in change["Details"]} == recreation
+
+
def test_cfn_appconfig_application_updates_in_place(cfn, appconfig_client):
"""An application update keeps its id."""
suffix = _uuid_mod.uuid4().hex[:8]