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
CloudFormation: fail loudly on undeletable resources + in-place update handlers (#1534)
iot-rocket4586908951ff
diff --git a/ministack/services/cloudformation/provisioners.py b/ministack/services/cloudformation/provisioners.pyindex f9d87110..7e2de4b4 100644--- a/ministack/services/cloudformation/provisioners.py+++ b/ministack/services/cloudformation/provisioners.py@@ -147,8 +147,17 @@ def _delete_resource(resource_type: str, physical_id: str, props: dict,resource_type=resource_type,)return- logger.warning("No delete handler for resource type %s (id=%s)",- resource_type, physical_id)+ # CloudFormation internal types went through the no-op branch of+ # _provision_resource, so there is nothing real to delete. (The registered+ # internal types carry their own explicit no-op delete handlers above.)+ if resource_type.startswith("AWS::CloudFormation::"):+ logger.info("CloudFormation internal type %s for %s -- noop", resource_type, physical_id)+ return+ # Anything else was genuinely provisioned — a create handler ran for it, or+ # _provision_resource would have refused the type — so a missing delete+ # handler means the resource leaks. Fail the delete instead of logging a+ # warning nobody sees.+ raise ValueError(f"No delete handler for resource type {resource_type} (id={physical_id})")def _requires_replacement_dynamodb(old_props, new_props):@@ -205,6 +214,22 @@ def _custom_named_replacement_error(resource_type, old_props, new_props):return None+def _rename_replacement(physical_id, old_props, new_props, stack_name, logical_id,+ declared_name, current_name, create_fn, delete_fn):+ """Shared prologue for the name-keyed update handlers: when the resource+ record is gone (current_name is None) or its create-only name property+ changed, the update is a replacement — create the new resource first, then+ delete the old one, in CloudFormation's replacement order. Returns the+ create result, or None when the update can proceed in place.+ """+ if current_name is not None and declared_name == current_name:+ return None+ created = create_fn(logical_id or physical_id, new_props, stack_name)+ if current_name is not None and created[0] != physical_id:+ delete_fn(physical_id, old_props)+ return created++def _update_resource(resource_type: str, physical_id: str, old_props: dict,new_props: dict, stack_name: str,logical_id: str | None = None,@@ -501,6 +526,16 @@ def _s3_bucket_policy_create(logical_id, props, stack_name):return f"{bucket}-policy", {}+def _s3_bucket_policy_update(physical_id, old_props, new_props, stack_name):+ if new_props.get("Bucket") != old_props.get("Bucket"):+ # Bucket is create-only on AWS — the policy is replaced onto the new+ # bucket and removed from the old one.+ result = _s3_bucket_policy_create(physical_id, new_props, stack_name)+ _s3_bucket_policy_delete(physical_id, old_props)+ return result+ return _s3_bucket_policy_create(physical_id, new_props, stack_name)++def _s3_bucket_policy_delete(physical_id, props):bucket = props.get("Bucket", "")_s3._bucket_policies.pop(bucket, None)@@ -557,6 +592,44 @@ def _sqs_create(logical_id, props, stack_name):return url, {"Arn": arn, "QueueName": name, "QueueUrl": url}+def _sqs_update(physical_id, old_props, new_props, stack_name, logical_id=None):+ """Update a queue's attributes in place, keeping its messages.++ QueueName (and the .fifo suffix it implies) is create-only on AWS: a+ change is a replacement, so the new queue is created and the old one+ removed. Everything else maps onto SetQueueAttributes semantics — the+ queue record (URL, messages, dedup state) survives.+ """+ name = new_props.get("QueueName") or _physical_name(+ stack_name, logical_id or physical_id, max_len=80+ )+ queue = _sqs._queues.get(physical_id)+ replaced = _rename_replacement(+ physical_id, old_props, new_props, stack_name, logical_id,+ name, queue.get("name") if queue else None, _sqs_create, _sqs_delete,+ )+ if replaced is not None:+ return replaced+ attributes = queue["attributes"]+ attributes["VisibilityTimeout"] = str(new_props.get("VisibilityTimeout", "30"))+ attributes["MaximumMessageSize"] = str(new_props.get("MaximumMessageSize", "262144"))+ attributes["MessageRetentionPeriod"] = str(new_props.get("MessageRetentionPeriod", "345600"))+ attributes["DelaySeconds"] = str(new_props.get("DelaySeconds", "0"))+ attributes["ReceiveMessageWaitTimeSeconds"] = str(new_props.get("ReceiveMessageWaitTimeSeconds", "0"))+ if queue["is_fifo"]:+ # Same omit-unless-truthy shape as create: an absent property must not+ # materialize as ContentBasedDeduplication "false" on the record.+ if new_props.get("ContentBasedDeduplication"):+ attributes["ContentBasedDeduplication"] = str(+ new_props["ContentBasedDeduplication"]+ ).lower()+ else:+ attributes.pop("ContentBasedDeduplication", None)+ attributes["LastModifiedTimestamp"] = str(int(time.time()))+ arn = attributes["QueueArn"]+ return physical_id, {"Arn": arn, "QueueName": name, "QueueUrl": physical_id}++def _sqs_delete(physical_id, props):queue = _sqs._queues.pop(physical_id, None)if queue:@@ -605,7 +678,10 @@ def _sns_create(logical_id, props, stack_name):"tags": {},}- # Handle Subscription property+ # Handle Subscription property. The private _cfn_inline marker is what+ # lets _sns_update's reconcile tell these records apart from standalone+ # AWS::SNS::Subscription resources (and plain Subscribe calls) carrying+ # the same protocol and endpoint.subscriptions = props.get("Subscription", [])for sub_def in subscriptions:protocol = sub_def.get("Protocol", "")@@ -618,7 +694,8 @@ def _sns_create(logical_id, props, stack_name):"endpoint": endpoint,"confirmed": protocol not in ("http", "https"),"owner": get_account_id(),- "attributes": {}+ "attributes": {},+ "_cfn_inline": True,}_sns._topics[arn]["subscriptions"].append(sub)_sns._sub_arn_to_topic[sub_arn] = arn@@ -626,6 +703,54 @@ def _sns_create(logical_id, props, stack_name):return arn, {"TopicArn": arn, "TopicName": name}+def _sns_update(physical_id, old_props, new_props, stack_name, logical_id=None):+ """Update a topic in place. TopicName is create-only on AWS (a change is a+ replacement); DisplayName and the inline Subscription list are mutable, so+ the topic ARN — and any standalone subscriptions attached to it — survive.+ """+ name = new_props.get("TopicName") or _physical_name(+ stack_name, logical_id or physical_id, max_len=256+ )+ topic = _sns._topics.get(physical_id)+ replaced = _rename_replacement(+ physical_id, old_props, new_props, stack_name, logical_id,+ name, topic.get("name") if topic else None, _sns_create, _sns_delete,+ )+ if replaced is not None:+ return replaced+ topic["attributes"]["DisplayName"] = new_props.get("DisplayName", name)+ # Reconcile the template-inline subscriptions by (Protocol, Endpoint).+ # Only records carrying the _cfn_inline marker are eligible for removal:+ # a standalone AWS::SNS::Subscription resource (or a plain Subscribe call)+ # can hold the same protocol and endpoint and must not be touched here.+ def _sub_key(sub_def):+ return (sub_def.get("Protocol", ""), sub_def.get("Endpoint", ""))+ old_subs = {_sub_key(s) for s in old_props.get("Subscription", [])}+ new_subs = {_sub_key(s) for s in new_props.get("Subscription", [])}+ for sub in [s for s in topic["subscriptions"]+ if s.get("_cfn_inline")+ and (s.get("protocol"), s.get("endpoint")) in old_subs - new_subs]:+ topic["subscriptions"].remove(sub)+ _sns._sub_arn_to_topic.pop(sub.get("arn", ""), None)+ # Additions dedupe against every existing subscription, as Subscribe does.+ existing = {(s.get("protocol"), s.get("endpoint")) for s in topic["subscriptions"]}+ for protocol, endpoint in sorted(new_subs - existing):+ sub_arn = f"{physical_id}:{new_uuid()}"+ sub = {+ "arn": sub_arn,+ "topic_arn": physical_id,+ "protocol": protocol,+ "endpoint": endpoint,+ "confirmed": protocol not in ("http", "https"),+ "owner": get_account_id(),+ "attributes": {},+ "_cfn_inline": True,+ }+ topic["subscriptions"].append(sub)+ _sns._sub_arn_to_topic[sub_arn] = physical_id+ return physical_id, {"TopicArn": physical_id, "TopicName": name}++def _sns_delete(physical_id, props):topic = _sns._topics.pop(physical_id, None)if topic:@@ -746,6 +871,99 @@ def _ddb_delete(physical_id, props):_dynamodb.drop_stream_records(physical_id)+def _ddb_update_call(data):+ resp = _dynamodb._update_table(data)+ if resp[0] >= 400:+ raise ValueError(f"AWS::DynamoDB::Table update failed: {resp[2]!r}")+++def _ddb_update(physical_id, old_props, new_props, stack_name, logical_id=None):+ """Update a table in place through UpdateTable, keeping its items.++ KeySchema and LocalSecondaryIndexes are treated as create-only, following+ the CloudFormation registry: changing them replaces the table. With an+ explicit, unchanged TableName that replacement is refused (the+ _CUSTOM_NAME_REPLACEMENT rule for attribute-type changes, extended to the+ other immutable shapes); an auto-named table is re-created under its+ deterministic physical name.+ """+ name = new_props.get("TableName") or _physical_name(+ stack_name, logical_id or physical_id, max_len=255+ )+ table = _dynamodb._tables.get(physical_id)+ replaced = _rename_replacement(+ physical_id, old_props, new_props, stack_name, logical_id,+ name, physical_id if table is not None else None, _ddb_create, _ddb_delete,+ )+ if replaced is not None:+ return replaced++ if (old_props.get("KeySchema") != new_props.get("KeySchema")+ or old_props.get("LocalSecondaryIndexes") != new_props.get("LocalSecondaryIndexes")):+ if old_props.get("TableName"):+ raise ValueError(+ "CloudFormation cannot update a stack when a custom-named "+ f"resource requires replacing. Rename {name} and update the "+ "stack again."+ )+ _ddb_delete(physical_id, old_props)+ return _ddb_create(logical_id or physical_id, new_props, stack_name)++ data = {"TableName": name}+ old_billing = old_props.get("BillingMode", "PROVISIONED")+ new_billing = new_props.get("BillingMode", "PROVISIONED")+ if old_billing != new_billing:+ data["BillingMode"] = new_billing+ if (new_billing == "PROVISIONED"+ and old_props.get("ProvisionedThroughput") != new_props.get("ProvisionedThroughput")+ and new_props.get("ProvisionedThroughput")):+ data["ProvisionedThroughput"] = new_props["ProvisionedThroughput"]+ old_stream = old_props.get("StreamSpecification")+ new_stream = new_props.get("StreamSpecification")+ if old_stream != new_stream:+ if new_stream:+ if new_stream.get("StreamViewType") and "StreamEnabled" not in new_stream:+ new_stream = {**new_stream, "StreamEnabled": True}+ data["StreamSpecification"] = new_stream+ else:+ data["StreamSpecification"] = {"StreamEnabled": False}+ if old_props.get("SSESpecification") != new_props.get("SSESpecification"):+ data["SSESpecification"] = new_props.get("SSESpecification") or {"Enabled": False}+ if old_props.get("DeletionProtectionEnabled") != new_props.get("DeletionProtectionEnabled"):+ data["DeletionProtectionEnabled"] = new_props.get("DeletionProtectionEnabled", False)+ if len(data) > 1:+ _ddb_update_call(data)++ # GSIs reconcile through GlobalSecondaryIndexUpdates, one action per call+ # as on AWS (and as _update_table's duplicate-index guard expects). A GSI+ # whose definition changed is deleted and re-created.+ old_gsis = {g["IndexName"]: g for g in old_props.get("GlobalSecondaryIndexes", [])}+ new_gsis = {g["IndexName"]: g for g in new_props.get("GlobalSecondaryIndexes", [])}+ for idx_name in sorted(old_gsis.keys() - new_gsis.keys()):+ _ddb_update_call({"TableName": name, "GlobalSecondaryIndexUpdates": [+ {"Delete": {"IndexName": idx_name}}]})+ for idx_name in sorted(new_gsis.keys() & old_gsis.keys()):+ if old_gsis[idx_name] != new_gsis[idx_name]:+ _ddb_update_call({"TableName": name, "GlobalSecondaryIndexUpdates": [+ {"Delete": {"IndexName": idx_name}}]})+ _ddb_update_call({+ "TableName": name,+ "AttributeDefinitions": new_props.get("AttributeDefinitions", []),+ "GlobalSecondaryIndexUpdates": [{"Create": new_gsis[idx_name]}],+ })+ for idx_name in sorted(new_gsis.keys() - old_gsis.keys()):+ _ddb_update_call({+ "TableName": name,+ "AttributeDefinitions": new_props.get("AttributeDefinitions", []),+ "GlobalSecondaryIndexUpdates": [{"Create": new_gsis[idx_name]}],+ })++ attrs = {"Arn": table["TableArn"]}+ if table.get("LatestStreamArn") and (table.get("StreamSpecification") or {}).get("StreamEnabled"):+ attrs["StreamArn"] = table["LatestStreamArn"]+ return name, attrs++def _ddb_global_table_create(logical_id, props, stack_name):"""Provision an AWS::DynamoDB::GlobalTable.@@ -898,6 +1116,92 @@ def _lambda_create(logical_id, props, stack_name):return name, {"Arn": arn}+def _lambda_update(physical_id, old_props, new_props, stack_name, logical_id=None):+ """Update a function in place via UpdateFunctionCode/-Configuration.++ The create fallback used to rebuild the whole function record on every+ stack update, wiping everything CloudFormation doesn't own on AWS either:+ published versions, aliases, the resource policy, tags, event invoke+ configs. Going through the Lambda module's own update paths keeps them.++ FunctionName is create-only (a change is a replacement, executed here as+ create-new-then-delete-old); a PackageType flip is also a replacement on+ AWS, but under the same deterministic physical name the closest local+ equivalent is the full re-provision the create fallback always did.+ """+ name = new_props.get("FunctionName") or _physical_name(+ stack_name, logical_id or physical_id, max_len=64+ )+ func = _lambda_svc._functions.get(physical_id)+ replaced = _rename_replacement(+ physical_id, old_props, new_props, stack_name, logical_id,+ name, physical_id if func is not None else None, _lambda_create, _lambda_delete,+ )+ if replaced is not None:+ return replaced++ code = new_props.get("Code", {})+ image_uri = code.get("ImageUri")+ is_image = new_props.get("PackageType") == "Image" or bool(image_uri)+ if is_image != (func["config"].get("PackageType") == "Image"):+ # The re-provision replaces the whole function record under the same+ # name — a stale warm worker or pooled container would keep serving+ # the old package. Invalidate both, the way _update_code does.+ _lambda_svc.invalidate_worker(+ name, account=get_account_id(), region=get_region()+ )+ _lambda_svc._pool_kill_function(get_account_id(), name)+ return _lambda_create(logical_id or physical_id, new_props, stack_name)++ runtime = new_props.get("Runtime", "" if is_image else "python3.12")+ handler = new_props.get("Handler", "" if is_image else "index.handler")++ if code != old_props.get("Code", {}):+ if is_image:+ code_data = {"ImageUri": image_uri}+ elif code.get("ZipFile"):+ code_data = {+ "ZipFile": base64.b64encode(+ _zip_inline(code["ZipFile"], handler, runtime)+ ).decode()+ }+ else:+ code_data = {+ "S3Bucket": code.get("S3Bucket"),+ "S3Key": code.get("S3Key"),+ "S3ObjectVersion": code.get("S3ObjectVersion"),+ }+ resp = _lambda_svc._update_code(name, code_data)+ if resp[0] >= 400:+ raise ValueError(f"AWS::Lambda::Function code update failed: {resp[2]!r}")++ # The same property-to-config mapping (defaults included) the create path+ # applies, so a property removed from the template reverts to its default,+ # as it does on AWS.+ config_data = {+ "Runtime": runtime,+ "Handler": handler,+ "Role": new_props.get("Role", f"arn:aws:iam::{get_account_id()}:role/dummy-role"),+ "Timeout": int(new_props.get("Timeout", 3)),+ "MemorySize": int(new_props.get("MemorySize", 128)),+ "Description": new_props.get("Description", ""),+ "Environment": {"Variables": new_props.get("Environment", {}).get("Variables", {})},+ "Layers": new_props.get("Layers", []),+ "TracingConfig": new_props.get("TracingConfig", {"Mode": "PassThrough"}),+ "EphemeralStorage": {"Size": new_props.get("EphemeralStorage", {}).get("Size", 512)},+ "LoggingConfig": new_props.get(+ "LoggingConfig", {"LogFormat": "Text", "LogGroup": f"/aws/lambda/{name}"}+ ),+ "Architectures": new_props.get("Architectures", ["x86_64"]),+ }+ if new_props.get("ImageConfig"):+ config_data["ImageConfig"] = new_props["ImageConfig"]+ resp = _lambda_svc._update_config(name, config_data)+ if resp[0] >= 400:+ raise ValueError(f"AWS::Lambda::Function configuration update failed: {resp[2]!r}")+ return name, {"Arn": func["config"]["FunctionArn"]}++def _lambda_delete(physical_id, props):_lambda_svc._functions.pop(physical_id, None)@@ -1016,8 +1320,78 @@ def _iam_role_create(logical_id, props, stack_name):return name, {"Arn": arn, "RoleId": role_id}+def _iam_role_update(physical_id, old_props, new_props, stack_name, logical_id=None):+ """Update a role in place, keeping its ARN and RoleId.++ RoleName is create-only (a change replaces the role; AWS sanctions the+ rename); Path also requires replacement, which AWS refuses for an+ explicitly-named role. Everything else — assume-role document, inline+ Policies, ManagedPolicyArns, Description, MaxSessionDuration, Tags —+ updates in place, so policies attached from outside the template survive+ (the create fallback used to rebuild the record and drop them).+ """+ name = new_props.get("RoleName") or _physical_name(+ stack_name, logical_id or physical_id, max_len=64+ )+ role = _iam._roles.get(physical_id)+ replaced = _rename_replacement(+ physical_id, old_props, new_props, stack_name, logical_id,+ name, physical_id if role is not None else None,+ _iam_role_create, _iam_role_delete,+ )+ if replaced is not None:+ return replaced++ if new_props.get("Path", "/") != old_props.get("Path", "/"):+ if old_props.get("RoleName"):+ raise ValueError(+ "CloudFormation cannot update a stack when a custom-named "+ f"resource requires replacing. Rename {name} and update the "+ "stack again."+ )+ return _iam_role_create(logical_id or physical_id, new_props, stack_name)++ assume_doc = new_props.get("AssumeRolePolicyDocument", {})+ if isinstance(assume_doc, dict):+ assume_doc = json.dumps(assume_doc)+ role["AssumeRolePolicyDocument"] = assume_doc+ role["Description"] = new_props.get("Description", "")+ role["MaxSessionDuration"] = int(new_props.get("MaxSessionDuration", 3600))++ old_managed = set(old_props.get("ManagedPolicyArns", []))+ new_managed = set(new_props.get("ManagedPolicyArns", []))+ for policy_arn in sorted(old_managed - new_managed):+ _iam.detach_managed_policy(role, policy_arn)+ for policy_arn in sorted(new_managed - old_managed):+ _iam.attach_managed_policy(role, policy_arn)++ # Reconcile only the template's own inline policies — one added with+ # PutRolePolicy outside the stack survives an update, as on AWS.+ old_inline = {p.get("PolicyName", "") for p in old_props.get("Policies", [])}+ new_inline = {}+ for pol in new_props.get("Policies", []):+ pol_doc = pol.get("PolicyDocument", {})+ if isinstance(pol_doc, dict):+ pol_doc = json.dumps(pol_doc)+ new_inline[pol.get("PolicyName", "")] = pol_doc+ for pol_name in old_inline - new_inline.keys():+ role["InlinePolicies"].pop(pol_name, None)+ role["InlinePolicies"].update(new_inline)+ role["Tags"] = [+ {"Key": t.get("Key", ""), "Value": t.get("Value", "")}+ for t in new_props.get("Tags", [])+ ]+ return name, {"Arn": role["Arn"], "RoleId": role["RoleId"]}++def _iam_role_delete(physical_id, props):- _iam._roles.pop(physical_id, None)+ role = _iam._roles.pop(physical_id, None)+ if role:+ # DeleteRole requires the managed policies detached first; going+ # through the helper keeps each policy's AttachmentCount honest+ # instead of leaking the attachment on the surviving policy record.+ for policy_arn in list(role.get("AttachedPolicies", [])):+ _iam.detach_managed_policy(role, policy_arn)# --- IAM Policy ---@@ -1455,6 +1829,24 @@ def _cwlogs_create(logical_id, props, stack_name):return name, {"Arn": arn}+def _cwlogs_update(physical_id, old_props, new_props, stack_name, logical_id=None):+ """Update a log group in place — retention is the mutable property; the+ LogGroupName is create-only, so a change replaces the group. Keeping the+ record keeps its streams and events, which the create fallback wiped."""+ name = new_props.get("LogGroupName") or f"/aws/cloudformation/{stack_name}/{logical_id or physical_id}"+ group = _cw_logs._log_groups.get(physical_id)+ replaced = _rename_replacement(+ physical_id, old_props, new_props, stack_name, logical_id,+ name, physical_id if group is not None else None,+ _cwlogs_create, _cwlogs_delete,+ )+ if replaced is not None:+ return replaced+ retention = new_props.get("RetentionInDays")+ group["retentionInDays"] = int(retention) if retention else None+ return name, {"Arn": group["arn"]}++def _cwlogs_delete(physical_id, props):_cw_logs._log_groups.pop(physical_id, None)@@ -1574,6 +1966,27 @@ def _iot_topic_rule_create(logical_id, props, stack_name):return name, {"Arn": _iot._topic_rule_arn(name)}+def _iot_topic_rule_update(physical_id, old_props, new_props, stack_name, logical_id=None):+ """Replace the rule payload in place, as ReplaceTopicRule does. RuleName+ is create-only on AWS, so a change is a replacement."""+ name = new_props.get("RuleName") or _physical_name(+ stack_name, logical_id or physical_id+ ).replace("-", "_")+ replaced = _rename_replacement(+ physical_id, old_props, new_props, stack_name, logical_id,+ name, physical_id, _iot_topic_rule_create, _iot_topic_rule_delete,+ )+ if replaced is not None:+ return replaced+ payload = _pascal_to_camel(new_props.get("TopicRulePayload", {}))+ existing = _iot._topic_rules.get(name)+ _iot.put_topic_rule(+ name, payload,+ created_at=existing.get("createdAt") if existing else None,+ )+ return name, {"Arn": _iot._topic_rule_arn(name)}++def _iot_topic_rule_delete(physical_id, props):_iot.delete_topic_rule(physical_id)@@ -1818,7 +2231,10 @@ def _lambda_version_create(logical_id, props, stack_name):ver_str = str(ver_num)ver_config = copy.deepcopy(func["config"])ver_config["Version"] = ver_str- ver_arn = f"{ver_config['FunctionArn']}"+ # Ref on AWS::Lambda::Version returns the *qualified* ARN+ # (arn:...:function:name:version) — that qualifier is also what lets+ # the delete handler find the version it published.+ ver_arn = f"{ver_config['FunctionArn']}:{ver_str}"func["versions"][ver_str] = {"config": ver_config,"code_zip": func.get("code_zip"),@@ -1828,6 +2244,23 @@ def _lambda_version_create(logical_id, props, stack_name):return ver_arn, {"Version": "1"}+def _lambda_version_delete(physical_id, props):+ """Remove the published version, as DeleteFunction with a qualifier does.++ Idempotent when the function (or the version itself) is already gone —+ the usual case when the same stack also owns the function and deletes it+ first in reverse dependency order.+ """+ func, _func_name, _resource_arn, _qualifier = _lambda_function_for_cfn_ref(+ props.get("FunctionName", "")+ )+ if func is None:+ return+ version = physical_id.rsplit(":", 1)[-1]+ if version.isdigit():+ func["versions"].pop(version, None)++# --- CloudFormation WaitCondition / WaitConditionHandle (no-ops) ---def _cfn_wait_condition_create(logical_id, props, stack_name):@@ -1843,6 +2276,12 @@ def _cfn_wait_condition_handle_create(logical_id, props, stack_name):return pid, {"Ref": url}+def _cfn_noop_delete(physical_id, props):+ """Explicit no-op delete for the intentionally stateless internal types,+ so that _delete_resource's missing-handler check stays reserved for types+ that really do leak state."""++# --- CloudFormation Nested Stack (AWS::CloudFormation::Stack) ---def _cfn_nested_stack_deploy(logical_id, props, parent_stack_name, *,@@ -2212,6 +2651,59 @@ def _apigw_rest_api_create(logical_id, props, stack_name):}+def _apigw_rest_api_update(physical_id, old_props, new_props, stack_name, logical_id=None):+ """Update a REST API in place. A changed OpenAPI Body re-imports the API+ (real CloudFormation applies it with PutRestApi mode=overwrite; the local+ import path only creates, so this is a replacement — the new id propagates+ to the dependent resources as the same update reprocesses them)."""+ if new_props.get("Body") != old_props.get("Body"):+ created = _apigw_rest_api_create(logical_id or physical_id, new_props, stack_name)+ _apigw_rest_api_delete(physical_id, old_props)+ return created++ patch_ops = []+ # Name falls back to the deterministic physical name create used, so+ # removing Name from the template never patches the name to None.+ name_default = _physical_name(stack_name, logical_id or physical_id, max_len=64)+ if new_props.get("Name", name_default) != old_props.get("Name", name_default):+ patch_ops.append({+ "op": "replace", "path": "/name",+ "value": new_props.get("Name") or name_default,+ })+ for prop, path, default in (+ ("Description", "/description", ""),+ ("Policy", "/policy", None),+ ("MinimumCompressionSize", "/minimumCompressionSize", None),+ ("BinaryMediaTypes", "/binaryMediaTypes", []),+ ("ApiKeySourceType", "/apiKeySource", "HEADER"),+ ("DisableExecuteApiEndpoint", "/disableExecuteApiEndpoint", False),+ ("EndpointConfiguration", "/endpointConfiguration", {"types": ["REGIONAL"]}),+ ):+ if new_props.get(prop, default) != old_props.get(prop, default):+ patch_ops.append({+ "op": "replace", "path": path,+ "value": new_props.get(prop, default),+ })+ if patch_ops:+ resp = _apigw_v1._update_rest_api(physical_id, {"patchOperations": patch_ops})+ if resp[0] >= 400:+ raise ValueError(f"AWS::ApiGateway::RestApi update failed: {resp[2]!r}")+ if new_props.get("Tags") != old_props.get("Tags"):+ api = _apigw_v1._rest_apis.get(physical_id)+ if api is not None:+ api["tags"] = {t["Key"]: t["Value"] for t in new_props.get("Tags", [])}++ root_id = ""+ for rid, res in _apigw_v1._resources.get(physical_id, {}).items():+ if res.get("path") == "/":+ root_id = rid+ break+ return physical_id, {+ "RootResourceId": root_id,+ "Arn": f"arn:aws:apigateway:{get_region()}::/restapis/{physical_id}",+ }++def _apigw_rest_api_delete(physical_id, props):_apigw_v1._delete_rest_api(physical_id)@@ -2229,6 +2721,14 @@ def _apigw_resource_create(logical_id, props, stack_name):return resource_id, {"ResourceId": resource_id}+def _apigw_resource_update(physical_id, old_props, new_props, stack_name):+ # All three properties (RestApiId, ParentId, PathPart) are create-only on+ # AWS — any change is a replacement.+ created = _apigw_resource_create(physical_id, new_props, stack_name)+ _apigw_resource_delete(physical_id, old_props)+ return created++def _apigw_resource_delete(physical_id, props):api_id = props.get("RestApiId", "")_apigw_v1._delete_resource(api_id, physical_id)@@ -2271,6 +2771,26 @@ def _apigw_method_create(logical_id, props, stack_name):return pid, {}+def _apigw_method_update(physical_id, old_props, new_props, stack_name):+ """Re-put the method under its identity, PutMethod/PutIntegration being+ overwrites. RestApiId, ResourceId and HttpMethod are create-only on AWS —+ a change replaces the method."""+ if any(+ new_props.get(key) != old_props.get(key)+ for key in ("RestApiId", "ResourceId", "HttpMethod")+ ):+ created = _apigw_method_create(physical_id, new_props, stack_name)+ _apigw_method_delete(physical_id, old_props)+ return created+ if old_props.get("Integration") and not new_props.get("Integration"):+ _apigw_v1._delete_integration(+ new_props.get("RestApiId", ""),+ new_props.get("ResourceId", ""),+ new_props.get("HttpMethod", "ANY"),+ )+ return _apigw_method_create(physical_id, new_props, stack_name)++def _apigw_method_delete(physical_id, props):api_id = props.get("RestApiId", "")resource_id = props.get("ResourceId", "")@@ -2426,6 +2946,42 @@ def _apigw_stage_create(logical_id, props, stack_name):return stage_name, {"StageName": stage_name}+def _apigw_stage_update(physical_id, old_props, new_props, stack_name):+ """Update a stage in place through UpdateStage. RestApiId and StageName+ are create-only on AWS — a change replaces the stage."""+ if any(+ new_props.get(key) != old_props.get(key)+ for key in ("RestApiId", "StageName")+ ):+ created = _apigw_stage_create(physical_id, new_props, stack_name)+ _apigw_stage_delete(physical_id, old_props)+ return created+ api_id = new_props.get("RestApiId", "")+ stage_name = new_props.get("StageName", "")+ patch_ops = []+ for prop, path, default in (+ ("DeploymentId", "/deploymentId", ""),+ ("Description", "/description", ""),+ ("Variables", "/variables", {}),+ ("MethodSettings", "/methodSettings", {}),+ ("TracingEnabled", "/tracingEnabled", False),+ ):+ if new_props.get(prop, default) != old_props.get(prop, default):+ patch_ops.append({+ "op": "replace", "path": path,+ "value": new_props.get(prop, default),+ })+ if patch_ops:+ resp = _apigw_v1._update_stage(api_id, stage_name, {"patchOperations": patch_ops})+ if resp[0] >= 400:+ raise ValueError(f"AWS::ApiGateway::Stage update failed: {resp[2]!r}")+ if new_props.get("Tags") != old_props.get("Tags"):+ stage = _apigw_v1._stages_v1.get(api_id, {}).get(stage_name)+ if stage is not None:+ stage["tags"] = {t["Key"]: t["Value"] for t in new_props.get("Tags", [])}+ return stage_name, {"StageName": stage_name}++def _apigw_stage_delete(physical_id, props):api_id = props.get("RestApiId", "")stage_name = props.get("StageName", "")@@ -3264,6 +3820,13 @@ def _appsync_schema_create(logical_id, props, stack_name):return f"{api_id}/schema", {}+def _appsync_schema_delete(physical_id, props):+ # The physical id is "{api_id}/schema" — fall back to parsing it when the+ # stored properties are missing the ApiId.+ api_id = props.get("ApiId") or physical_id.rsplit("/", 1)[0]+ _appsync._types.get(api_id, {}).pop("__schema__", None)++def _appsync_apikey_create(logical_id, props, stack_name):api_id = props.get("ApiId", "")key_id = new_uuid()[:8]@@ -3619,6 +4182,88 @@ def _iam_managed_policy_create(logical_id, props, stack_name):}+_IAM_POLICY_VERSION_LIMIT = 5+++def _iam_managed_policy_update(physical_id, old_props, new_props, stack_name, logical_id=None):+ """Apply a policy change the way CloudFormation does: a new PolicyDocument+ becomes a new default policy version (pruning the oldest non-default+ version at the five-version cap first, like the CFN handler), and the+ Roles / Users / Groups lists reconcile through attach/detach. The+ create-only properties (ManagedPolicyName, Path, Description) replace the+ policy; under an unchanged custom name that replacement is refused, as+ real CloudFormation refuses it."""+ name = new_props.get("ManagedPolicyName", f"{stack_name}-{logical_id or physical_id}")+ record = _iam._policies.get(physical_id)+ replaced = _rename_replacement(+ physical_id, old_props, new_props, stack_name, logical_id,+ name, record.get("PolicyName") if record else None,+ _iam_managed_policy_create, _iam_managed_policy_delete,+ )+ if replaced is not None:+ return replaced++ if (new_props.get("Path", "/") != old_props.get("Path", "/")+ or new_props.get("Description", "") != old_props.get("Description", "")):+ # Path and Description are create-only too, but the name didn't+ # change, so the replacement cannot move to a new ARN.+ if old_props.get("ManagedPolicyName"):+ raise ValueError(+ "CloudFormation cannot update a stack when a custom-named "+ f"resource requires replacing. Rename {name} and update the "+ "stack again."+ )+ return _iam_managed_policy_create(logical_id or physical_id, new_props, stack_name)++ if new_props.get("PolicyDocument") != old_props.get("PolicyDocument"):+ versions = record["Versions"]+ surplus = len(versions) - _IAM_POLICY_VERSION_LIMIT + 1+ if surplus > 0:+ prunable = sorted(+ (v["VersionId"] for v in versions.values() if not v["IsDefaultVersion"]),+ key=lambda vid: int(vid.lstrip("v")),+ )+ for version_id in prunable[:surplus]:+ _iam._delete_policy_version(+ {"PolicyArn": physical_id, "VersionId": version_id}+ )+ document = new_props.get("PolicyDocument", {})+ if not isinstance(document, str):+ document = json.dumps(document)+ resp = _iam._create_policy_version({+ "PolicyArn": physical_id,+ "PolicyDocument": document,+ "SetAsDefault": "true",+ })+ if resp[0] >= 400:+ raise ValueError(f"AWS::IAM::ManagedPolicy update failed: {resp[2]!r}")++ for prop, store in (("Roles", _iam._roles),+ ("Users", _iam._users),+ ("Groups", _iam._groups)):+ old_names = set(old_props.get(prop, []) or [])+ new_names = set(new_props.get(prop, []) or [])+ for entity_name in sorted(old_names - new_names):+ entity = store.get(entity_name)+ if entity is not None:+ _iam.detach_managed_policy(entity, physical_id)+ for entity_name in sorted(new_names - old_names):+ entity = store.get(entity_name)+ if entity is not None:+ _iam.attach_managed_policy(entity, physical_id)++ return physical_id, {+ "PolicyArn": physical_id,+ "PolicyId": record["PolicyId"],+ "AttachmentCount": record["AttachmentCount"],+ "PermissionsBoundaryUsageCount": 0,+ "DefaultVersionId": record["DefaultVersionId"],+ "IsAttachable": record["IsAttachable"],+ "CreateDate": record["CreateDate"],+ "UpdateDate": record["UpdateDate"],+ }++def _iam_managed_policy_delete(physical_id, props):_iam._policies.pop(physical_id, None)@@ -6049,27 +6694,56 @@ _RESOURCE_HANDLERS = {"delete": _opensearch_domain_delete,},"AWS::S3::Bucket": {"create": _s3_create, "update": _s3_update, "delete": _s3_delete},- "AWS::S3::BucketPolicy": {"create": _s3_bucket_policy_create, "delete": _s3_bucket_policy_delete},+ "AWS::S3::BucketPolicy": {+ "create": _s3_bucket_policy_create,+ "update": _s3_bucket_policy_update,+ "delete": _s3_bucket_policy_delete,+ },"AWS::S3Tables::TableBucket": {"create": _s3tables_bucket_create, "delete": _s3tables_bucket_delete},"AWS::S3Tables::Namespace": {"create": _s3tables_namespace_create, "delete": _s3tables_namespace_delete},"AWS::S3Tables::Table": {"create": _s3tables_table_create, "delete": _s3tables_table_delete},- "AWS::SQS::Queue": {"create": _sqs_create, "delete": _sqs_delete},- "AWS::SNS::Topic": {"create": _sns_create, "delete": _sns_delete},+ "AWS::SQS::Queue": {+ "create": _sqs_create,+ "update": _sqs_update,+ "update_with_logical_id": True,+ "delete": _sqs_delete,+ },+ "AWS::SNS::Topic": {+ "create": _sns_create,+ "update": _sns_update,+ "update_with_logical_id": True,+ "delete": _sns_delete,+ },"AWS::SNS::Subscription": {"create": _sns_sub_create, "delete": _sns_sub_delete},- "AWS::DynamoDB::Table": {"create": _ddb_create, "delete": _ddb_delete},+ "AWS::DynamoDB::Table": {+ "create": _ddb_create,+ "update": _ddb_update,+ "update_with_logical_id": True,+ "delete": _ddb_delete,+ },# CDK TableV2 emits AWS::DynamoDB::GlobalTable, even for single-region# tables. The schema differs from Table (no ProvisionedThroughput; capacity# comes from WriteProvisionedThroughputSettings; Replicas is required and# ignored locally), so it gets a dedicated provisioner that translates# before delegating to the Table engine."AWS::DynamoDB::GlobalTable": {"create": _ddb_global_table_create, "delete": _ddb_global_table_delete},- "AWS::Lambda::Function": {"create": _lambda_create, "delete": _lambda_delete},+ "AWS::Lambda::Function": {+ "create": _lambda_create,+ "update": _lambda_update,+ "update_with_logical_id": True,+ "delete": _lambda_delete,+ },"AWS::Lambda::Url": {"create": _lambda_url_create,"update": _lambda_url_update,"delete": _lambda_url_delete,},- "AWS::IAM::Role": {"create": _iam_role_create, "delete": _iam_role_delete},+ "AWS::IAM::Role": {+ "create": _iam_role_create,+ "update": _iam_role_update,+ "update_with_logical_id": True,+ "delete": _iam_role_delete,+ },"AWS::IAM::Policy": {"create": _iam_policy_create, "delete": _iam_policy_delete},"AWS::IAM::InstanceProfile": {"create": _iam_ip_create, "delete": _iam_ip_delete},"AWS::SSM::Parameter": {"create": _ssm_create, "update": _ssm_update, "delete": _ssm_delete},@@ -6097,7 +6771,12 @@ _RESOURCE_HANDLERS = {"create": _appconfig_deployment_create,"delete": _appconfig_deployment_delete,},- "AWS::Logs::LogGroup": {"create": _cwlogs_create, "delete": _cwlogs_delete},+ "AWS::Logs::LogGroup": {+ "create": _cwlogs_create,+ "update": _cwlogs_update,+ "update_with_logical_id": True,+ "delete": _cwlogs_delete,+ },"AWS::Logs::ResourcePolicy": {"create": _cwlogs_resource_policy_create,"update": _cwlogs_resource_policy_update,@@ -6113,9 +6792,9 @@ _RESOURCE_HANDLERS = {"delete": _firehose_delivery_stream_delete,},"AWS::Lambda::Permission": {"create": _lambda_permission_create, "delete": _lambda_permission_delete},- "AWS::Lambda::Version": {"create": _lambda_version_create},- "AWS::CloudFormation::WaitCondition": {"create": _cfn_wait_condition_create},- "AWS::CloudFormation::WaitConditionHandle": {"create": _cfn_wait_condition_handle_create},+ "AWS::Lambda::Version": {"create": _lambda_version_create, "delete": _lambda_version_delete},+ "AWS::CloudFormation::WaitCondition": {"create": _cfn_wait_condition_create, "delete": _cfn_noop_delete},+ "AWS::CloudFormation::WaitConditionHandle": {"create": _cfn_wait_condition_handle_create, "delete": _cfn_noop_delete},"AWS::CloudFormation::Stack": {"create": _cfn_nested_stack_create,"update": _cfn_nested_stack_update,@@ -6126,17 +6805,36 @@ _RESOURCE_HANDLERS = {# are intentionally absent — they route through the explicit if-branches in# _update_resource/_delete_resource so stack_name and logical_id reach the handler."AWS::CloudFormation::CustomResource": {"create": _custom_resource_create},- "AWS::ApiGateway::RestApi": {"create": _apigw_rest_api_create, "delete": _apigw_rest_api_delete},- "AWS::ApiGateway::Resource": {"create": _apigw_resource_create, "delete": _apigw_resource_delete},- "AWS::ApiGateway::Method": {"create": _apigw_method_create, "delete": _apigw_method_delete},+ "AWS::ApiGateway::RestApi": {+ "create": _apigw_rest_api_create,+ "update": _apigw_rest_api_update,+ "update_with_logical_id": True,+ "delete": _apigw_rest_api_delete,+ },+ "AWS::ApiGateway::Resource": {+ "create": _apigw_resource_create,+ "update": _apigw_resource_update,+ "delete": _apigw_resource_delete,+ },+ "AWS::ApiGateway::Method": {+ "create": _apigw_method_create,+ "update": _apigw_method_update,+ "delete": _apigw_method_delete,+ },"AWS::ApiGateway::Model": {"create": _apigw_model_create,"update": _apigw_model_update,"delete": _apigw_model_delete,},"AWS::ApiGateway::Authorizer": {"create": _apigw_authorizer_create, "delete": _apigw_authorizer_delete},+ # Deployment stays create-only: AWS treats a deployment as an immutable+ # snapshot — an update is a new deployment (CFN replaces the resource)."AWS::ApiGateway::Deployment": {"create": _apigw_deployment_create, "delete": _apigw_deployment_delete},- "AWS::ApiGateway::Stage": {"create": _apigw_stage_create, "delete": _apigw_stage_delete},+ "AWS::ApiGateway::Stage": {+ "create": _apigw_stage_create,+ "update": _apigw_stage_update,+ "delete": _apigw_stage_delete,+ },"AWS::ApiGateway::ApiKey": {"create": _apigw_api_key_create,"update": _apigw_api_key_update,@@ -6200,7 +6898,7 @@ _RESOURCE_HANDLERS = {"delete": _appsync_function_delete,},"AWS::AppSync::Resolver": {"create": _appsync_resolver_create, "delete": _appsync_resolver_delete},- "AWS::AppSync::GraphQLSchema": {"create": _appsync_schema_create},+ "AWS::AppSync::GraphQLSchema": {"create": _appsync_schema_create, "delete": _appsync_schema_delete},"AWS::AppSync::ApiKey": {"create": _appsync_apikey_create, "delete": _appsync_apikey_delete},"AWS::SecretsManager::Secret": {"create": _sm_secret_create, "delete": _sm_secret_delete},"AWS::Cognito::UserPool": {"create": _cognito_user_pool_create, "delete": _cognito_user_pool_delete},@@ -6214,7 +6912,12 @@ _RESOURCE_HANDLERS = {"AWS::ElasticLoadBalancingV2::TargetGroup": {"create": _elbv2_target_group_create, "delete": _elbv2_target_group_delete},"AWS::ElasticLoadBalancingV2::ListenerRule": {"create": _elbv2_listener_rule_create, "delete": _elbv2_listener_rule_delete},"AWS::CodeBuild::Project": {"create": _codebuild_project_create, "delete": _codebuild_project_delete},- "AWS::IAM::ManagedPolicy": {"create": _iam_managed_policy_create, "delete": _iam_managed_policy_delete},+ "AWS::IAM::ManagedPolicy": {+ "create": _iam_managed_policy_create,+ "update": _iam_managed_policy_update,+ "update_with_logical_id": True,+ "delete": _iam_managed_policy_delete,+ },"AWS::KMS::Key": {"create": _kms_key_create,"update": _kms_key_update,@@ -6272,7 +6975,12 @@ _RESOURCE_HANDLERS = {},"AWS::RDS::DBCluster": {"create": _rds_db_cluster_create, "delete": _rds_db_cluster_delete},"AWS::RDS::DBInstance": {"create": _rds_db_instance_create, "delete": _rds_db_instance_delete},- "AWS::IoT::TopicRule": {"create": _iot_topic_rule_create, "delete": _iot_topic_rule_delete},+ "AWS::IoT::TopicRule": {+ "create": _iot_topic_rule_create,+ "update": _iot_topic_rule_update,+ "update_with_logical_id": True,+ "delete": _iot_topic_rule_delete,+ },"AWS::IoT::ThingType": {"create": _iot_thing_type_create, "delete": _iot_thing_type_delete},"AWS::IoT::Policy": {"create": _iot_policy_create,diff --git a/ministack/services/cloudformation/stacks.py b/ministack/services/cloudformation/stacks.pyindex 7e123fbd..d2536751 100644--- a/ministack/services/cloudformation/stacks.py+++ b/ministack/services/cloudformation/stacks.py@@ -217,8 +217,14 @@ async def _deploy_stack_async(stack_name: str, stack_id: str, template: dict,else:_delete_resource(rtype, pid, old_props, stack_name, logical_id)except Exception as exc:- logger.warning("Failed to delete old resource %s: %s",- logical_id, exc)+ # A cleanup miss doesn't fail the update — real CloudFormation+ # reports the resource DELETE_FAILED during the+ # UPDATE_COMPLETE_CLEANUP phase and still lands the stack in+ # UPDATE_COMPLETE — but it must be visible, not a warning.+ logger.error("Failed to delete old resource %s: %s",+ logical_id, exc)+ _add_event(stack_id, stack_name, logical_id, rtype,+ "DELETE_FAILED", str(exc), pid)provisioned_resources.pop(logical_id, None)await asyncio.sleep(0)@@ -237,6 +243,7 @@ async def _deploy_stack_async(stack_name: str, stack_id: str, template: dict,"AWS::CloudFormation::Stack", stack["StackStatus"],"Rollback requested", stack_id)+ rollback_delete_failures = []for logical_id in reversed(created_in_this_run):res = provisioned_resources.get(logical_id, {})rtype = res.get("ResourceType", "")@@ -253,10 +260,11 @@ async def _deploy_stack_async(stack_name: str, stack_id: str, template: dict,_add_event(stack_id, stack_name, logical_id, rtype,"DELETE_COMPLETE", physical_id=pid)except Exception as del_exc:- logger.warning("Rollback delete of %s failed: %s",- logical_id, del_exc)+ logger.error("Rollback delete of %s failed: %s",+ logical_id, del_exc)_add_event(stack_id, stack_name, logical_id, rtype,"DELETE_FAILED", str(del_exc), pid)+ rollback_delete_failures.append(logical_id)provisioned_resources.pop(logical_id, None)if is_update and previous_stack:@@ -268,6 +276,21 @@ async def _deploy_stack_async(stack_name: str, stack_id: str, template: dict,stack["StackStatus"] = "UPDATE_ROLLBACK_COMPLETE"else:stack["StackStatus"] = "ROLLBACK_COMPLETE"+ if rollback_delete_failures:+ # A rollback that could not undo what it created must not+ # report success — real CloudFormation lands the stack in+ # (UPDATE_)ROLLBACK_FAILED and keeps the failure visible.+ stack["StackStatus"] = (+ "UPDATE_ROLLBACK_FAILED" if is_update and previous_stack+ else "ROLLBACK_FAILED"+ )+ reason = ("The following resource(s) failed to delete: "+ f"[{', '.join(sorted(rollback_delete_failures))}].")+ stack["StackStatusReason"] = reason+ _add_event(stack_id, stack_name, stack_name,+ "AWS::CloudFormation::Stack", stack["StackStatus"],+ reason, stack_id)+ return_add_event(stack_id, stack_name, stack_name,"AWS::CloudFormation::Stack", stack["StackStatus"],"Rollback complete", stack_id)@@ -340,6 +363,7 @@ async def _delete_stack_async(stack_name: str, stack_id: str):except ValueError:ordered = list(resources.keys())+ delete_failures = []for logical_id in reversed(ordered):res = resources.get(logical_id)if not res:@@ -360,10 +384,30 @@ async def _delete_stack_async(stack_name: str, stack_id: str):_delete_resource(rtype, pid, res_props, stack_name, logical_id)_add_event(stack_id, stack_name, logical_id, rtype,"DELETE_COMPLETE", physical_id=pid)+ resources.pop(logical_id, None)except Exception as exc:- logger.warning("Delete of %s (%s) failed: %s", logical_id, pid, exc)+ logger.error("Delete of %s (%s) failed: %s", logical_id, pid, exc)_add_event(stack_id, stack_name, logical_id, rtype,"DELETE_FAILED", str(exc), pid)+ res["ResourceStatus"] = "DELETE_FAILED"+ res["ResourceStatusReason"] = str(exc)+ delete_failures.append(logical_id)++ await asyncio.sleep(0)++ if delete_failures:+ # Real CloudFormation keeps deleting the other resources, then lands+ # the stack in DELETE_FAILED: the failed resources stay in the stack+ # (a retried DeleteStack picks them up again), everything that did+ # delete is gone, and the exports stay with the still-existing stack.+ reason = ("The following resource(s) failed to delete: "+ f"[{', '.join(sorted(delete_failures))}].")+ stack["StackStatus"] = "DELETE_FAILED"+ stack["StackStatusReason"] = reason+ _add_event(stack_id, stack_name, stack_name,+ "AWS::CloudFormation::Stack", "DELETE_FAILED",+ reason, stack_id)+ return# Remove exportsfor out in stack.get("Outputs", []):@@ -371,8 +415,6 @@ async def _delete_stack_async(stack_name: str, stack_id: str):if export_name:_exports.pop(export_name, None)- await asyncio.sleep(0)-stack["StackStatus"] = "DELETE_COMPLETE"_add_event(stack_id, stack_name, stack_name,"AWS::CloudFormation::Stack", "DELETE_COMPLETE",diff --git a/ministack/services/iam.py b/ministack/services/iam.pyindex f446318a..82f5906d 100644--- a/ministack/services/iam.py+++ b/ministack/services/iam.py@@ -904,6 +904,21 @@ def attach_managed_policy(entity: dict, policy_arn: str) -> None:pol["AttachmentCount"] = pol.get("AttachmentCount", 0) + 1+def detach_managed_policy(entity: dict, policy_arn: str) -> None:+ """Inverse of attach_managed_policy, with the same bookkeeping contract:+ the bare-ARN list and AttachmentCount stay in step, exactly as+ DetachRolePolicy keeps them. Idempotent when the policy isn't attached."""+ if policy_arn not in entity.get("AttachedPolicies", []):+ return+ entity["AttachedPolicies"].remove(policy_arn)+ if _is_aws_managed_arn(policy_arn):+ _bump_aws_managed_attachment(policy_arn, -1)+ else:+ pol = _policies.get(policy_arn)+ if pol:+ pol["AttachmentCount"] = max(pol.get("AttachmentCount", 1) - 1, 0)++def _attach_role_policy(p):role_name = _p(p, "RoleName")policy_arn = _p(p, "PolicyArn")diff --git a/tests/test_cfn.py b/tests/test_cfn.pyindex 473678ee..76473042 100644--- a/tests/test_cfn.py+++ b/tests/test_cfn.py@@ -9419,3 +9419,1019 @@ def test_cfn_ses_configuration_set_and_event_destination(cfn, ses, sesv2):with pytest.raises(ClientError):ses.describe_configuration_set(ConfigurationSetName=cs_name)+++# ===========================================================================+# Loud deletes — DELETE_FAILED / ROLLBACK_FAILED stack states, and the+# delete handlers for the types that used to leak silently+# ===========================================================================++_CR_HANDLER_DELETE_FAILS = """\+import json, urllib.request++def handler(event, context):+ status = "FAILED" if event["RequestType"] == "Delete" else "SUCCESS"+ payload = json.dumps({+ "Status": status,+ "Reason": "delete refused for testing",+ "RequestId": event["RequestId"],+ "StackId": event["StackId"],+ "LogicalResourceId": event["LogicalResourceId"],+ "PhysicalResourceId": event.get("PhysicalResourceId", "delete-fails-cr"),+ }).encode()+ req = urllib.request.Request(+ event["ResponseURL"],+ data=payload,+ method="PUT",+ headers={"content-type": "", "content-length": str(len(payload))},+ )+ urllib.request.urlopen(req, timeout=10)+"""+++def test_cfn_stack_delete_failure_lands_delete_failed(cfn, lam, sns):+ """A resource delete that fails lands the stack in DELETE_FAILED, not a+ silent DELETE_COMPLETE: the other resources are still deleted, the failed+ resource is retained, and a retried DeleteStack can finish the job."""+ suffix = _uuid_mod.uuid4().hex[:8]+ fn = f"cr-del-fail-{suffix}"+ stack_name = f"cfn-delete-failed-{suffix}"+ topic_name = f"cfn-delete-failed-topic-{suffix}"++ def create_handler():+ lam.create_function(+ FunctionName=fn,+ Runtime="python3.12",+ Role=_CR_LAMBDA_ROLE,+ Handler="index.handler",+ Code={"ZipFile": _cr_make_zip(_CR_HANDLER_SUCCESS)},+ )++ create_handler()+ template = {+ "AWSTemplateFormatVersion": "2010-09-09",+ "Resources": {+ "Topic": {+ "Type": "AWS::SNS::Topic",+ "Properties": {"TopicName": topic_name},+ },+ "CR": {+ "Type": "Custom::Tester",+ "Properties": {+ "ServiceToken": f"arn:aws:lambda:us-east-1:000000000000:function:{fn}",+ },+ },+ },+ }+ try:+ cfn.create_stack(StackName=stack_name, TemplateBody=json.dumps(template))+ stack = _wait_stack(cfn, stack_name)+ assert stack["StackStatus"] == "CREATE_COMPLETE", stack.get("StackStatusReason")++ # Remove the custom resource's handler so its Delete cannot be delivered.+ lam.delete_function(FunctionName=fn)++ cfn.delete_stack(StackName=stack_name)+ stack = _wait_stack(cfn, stack_name)+ assert stack["StackStatus"] == "DELETE_FAILED"+ assert "CR" in stack.get("StackStatusReason", "")++ # The deletable resource is gone regardless — CFN keeps deleting.+ topic_arns = {t["TopicArn"] for t in sns.list_topics()["Topics"]}+ assert not any(arn.endswith(f":{topic_name}") for arn in topic_arns)++ # The failed resource is retained on the still-existing stack.+ retained = cfn.describe_stack_resources(StackName=stack_name)["StackResources"]+ assert [r["LogicalResourceId"] for r in retained] == ["CR"]+ assert retained[0]["ResourceStatus"] == "DELETE_FAILED"++ # And the failure is visible as a stack-level event.+ events = cfn.describe_stack_events(StackName=stack_name)["StackEvents"]+ assert any(+ e["ResourceType"] == "AWS::CloudFormation::Stack"+ and e["ResourceStatus"] == "DELETE_FAILED"+ for e in events+ )++ # Restoring the handler and retrying the delete completes it.+ create_handler()+ cfn.delete_stack(StackName=stack_name)+ stack = _wait_stack(cfn, stack_name)+ assert stack["StackStatus"] == "DELETE_COMPLETE"+ finally:+ _delete_cfn_test_stack(cfn, stack_name)+ try:+ lam.delete_function(FunctionName=fn)+ except ClientError:+ pass+++def test_cfn_stack_delete_failed_keeps_exports(cfn, lam):+ """A stack landing in DELETE_FAILED keeps its exports — they belong to+ the still-existing stack and only go away with DELETE_COMPLETE."""+ suffix = _uuid_mod.uuid4().hex[:8]+ fn = f"cr-del-exp-{suffix}"+ stack_name = f"cfn-delete-failed-exp-{suffix}"+ export_name = f"cfn-del-exp-{suffix}"++ def create_handler():+ lam.create_function(+ FunctionName=fn,+ Runtime="python3.12",+ Role=_CR_LAMBDA_ROLE,+ Handler="index.handler",+ Code={"ZipFile": _cr_make_zip(_CR_HANDLER_SUCCESS)},+ )++ create_handler()+ template = {+ "AWSTemplateFormatVersion": "2010-09-09",+ "Resources": {+ "CR": {+ "Type": "Custom::Tester",+ "Properties": {+ "ServiceToken": f"arn:aws:lambda:us-east-1:000000000000:function:{fn}",+ },+ },+ },+ "Outputs": {+ "Kept": {"Value": "still-here", "Export": {"Name": export_name}},+ },+ }+ try:+ cfn.create_stack(StackName=stack_name, TemplateBody=json.dumps(template))+ stack = _wait_stack(cfn, stack_name)+ assert stack["StackStatus"] == "CREATE_COMPLETE", stack.get("StackStatusReason")++ # Remove the custom resource's handler so its Delete cannot be delivered.+ lam.delete_function(FunctionName=fn)+ cfn.delete_stack(StackName=stack_name)+ stack = _wait_stack(cfn, stack_name)+ assert stack["StackStatus"] == "DELETE_FAILED"++ exports = {e["Name"]: e["Value"] for e in cfn.list_exports()["Exports"]}+ assert exports.get(export_name) == "still-here"++ # The completed retry removes the export with the stack.+ create_handler()+ cfn.delete_stack(StackName=stack_name)+ stack = _wait_stack(cfn, stack_name)+ assert stack["StackStatus"] == "DELETE_COMPLETE"+ exports = {e["Name"] for e in cfn.list_exports()["Exports"]}+ assert export_name not in exports+ finally:+ _delete_cfn_test_stack(cfn, stack_name)+ try:+ lam.delete_function(FunctionName=fn)+ except ClientError:+ pass+++def test_cfn_create_rollback_delete_failure_lands_rollback_failed(cfn, lam):+ """A rollback that cannot undo what it created reports ROLLBACK_FAILED+ instead of pretending the rollback completed."""+ suffix = _uuid_mod.uuid4().hex[:8]+ fn = f"cr-rb-fail-{suffix}"+ stack_name = f"cfn-rollback-failed-{suffix}"+ lam.create_function(+ FunctionName=fn,+ Runtime="python3.12",+ Role=_CR_LAMBDA_ROLE,+ Handler="index.handler",+ Code={"ZipFile": _cr_make_zip(_CR_HANDLER_DELETE_FAILS)},+ )+ template = {+ "AWSTemplateFormatVersion": "2010-09-09",+ "Resources": {+ "CR": {+ "Type": "Custom::Tester",+ "Properties": {+ "ServiceToken": f"arn:aws:lambda:us-east-1:000000000000:function:{fn}",+ },+ },+ # Provisioned after CR; its unsupported type fails the create and+ # triggers the rollback that has to delete CR again.+ "Bad": {+ "Type": "AWS::Unsupported::DoesNotExist",+ "DependsOn": "CR",+ "Properties": {},+ },+ },+ }+ try:+ cfn.create_stack(StackName=stack_name, TemplateBody=json.dumps(template))+ stack = _wait_stack(cfn, stack_name)+ assert stack["StackStatus"] == "ROLLBACK_FAILED", stack.get("StackStatusReason")++ events = cfn.describe_stack_events(StackName=stack_name)["StackEvents"]+ assert any(+ e["LogicalResourceId"] == "CR" and e["ResourceStatus"] == "DELETE_FAILED"+ for e in events+ )+ assert any(+ e["ResourceType"] == "AWS::CloudFormation::Stack"+ and e["ResourceStatus"] == "ROLLBACK_FAILED"+ for e in events+ )+ finally:+ # Let the handler accept deletes again so the stack can be cleaned up.+ try:+ lam.update_function_code(+ FunctionName=fn, ZipFile=_cr_make_zip(_CR_HANDLER_SUCCESS)+ )+ except ClientError:+ pass+ _delete_cfn_test_stack(cfn, stack_name)+ try:+ lam.delete_function(FunctionName=fn)+ except ClientError:+ pass+++def test_cfn_update_rollback_delete_failure_lands_update_rollback_failed(cfn, lam):+ """An update rollback whose delete fails lands UPDATE_ROLLBACK_FAILED and+ keeps the previous resources on the stack."""+ suffix = _uuid_mod.uuid4().hex[:8]+ fn = f"cr-urb-fail-{suffix}"+ stack_name = f"cfn-update-rollback-failed-{suffix}"+ topic_name = f"cfn-urb-topic-{suffix}"+ lam.create_function(+ FunctionName=fn,+ Runtime="python3.12",+ Role=_CR_LAMBDA_ROLE,+ Handler="index.handler",+ Code={"ZipFile": _cr_make_zip(_CR_HANDLER_DELETE_FAILS)},+ )+ base = {+ "AWSTemplateFormatVersion": "2010-09-09",+ "Resources": {+ "Topic": {+ "Type": "AWS::SNS::Topic",+ "Properties": {"TopicName": topic_name},+ },+ },+ }+ updated = json.loads(json.dumps(base))+ updated["Resources"]["CR"] = {+ "Type": "Custom::Tester",+ "Properties": {+ "ServiceToken": f"arn:aws:lambda:us-east-1:000000000000:function:{fn}",+ },+ }+ updated["Resources"]["Bad"] = {+ "Type": "AWS::Unsupported::DoesNotExist",+ "DependsOn": "CR",+ "Properties": {},+ }+ try:+ cfn.create_stack(StackName=stack_name, TemplateBody=json.dumps(base))+ stack = _wait_stack(cfn, stack_name)+ assert stack["StackStatus"] == "CREATE_COMPLETE", stack.get("StackStatusReason")++ cfn.update_stack(StackName=stack_name, TemplateBody=json.dumps(updated))+ stack = _wait_stack(cfn, stack_name)+ assert stack["StackStatus"] == "UPDATE_ROLLBACK_FAILED", stack.get("StackStatusReason")++ # The pre-update resources survive on the stack.+ retained = cfn.describe_stack_resources(StackName=stack_name)["StackResources"]+ assert "Topic" in [r["LogicalResourceId"] for r in retained]++ events = cfn.describe_stack_events(StackName=stack_name)["StackEvents"]+ assert any(+ e["ResourceType"] == "AWS::CloudFormation::Stack"+ and e["ResourceStatus"] == "UPDATE_ROLLBACK_FAILED"+ for e in events+ )+ finally:+ try:+ lam.update_function_code(+ FunctionName=fn, ZipFile=_cr_make_zip(_CR_HANDLER_SUCCESS)+ )+ except ClientError:+ pass+ _delete_cfn_test_stack(cfn, stack_name)+ try:+ lam.delete_function(FunctionName=fn)+ except ClientError:+ pass+++def test_cfn_lambda_version_stack_delete_removes_version(cfn, lam):+ """AWS::Lambda::Version now has a real delete handler: deleting the stack+ removes the published version instead of leaking it, and Ref resolves to+ the qualified version ARN as on AWS."""+ suffix = _uuid_mod.uuid4().hex[:8]+ fn = f"cfn-ver-del-{suffix}"+ stack_name = f"cfn-ver-del-{suffix}"+ code = "def handler(e,c): return {}"+ buf = io.BytesIO()+ with zipfile.ZipFile(buf, "w") as zf:+ zf.writestr("index.py", code)+ lam.create_function(+ FunctionName=fn, Runtime="python3.12",+ Role=_CR_LAMBDA_ROLE, Handler="index.handler",+ Code={"ZipFile": buf.getvalue()},+ )+ template = {+ "AWSTemplateFormatVersion": "2010-09-09",+ "Resources": {+ "Ver": {+ "Type": "AWS::Lambda::Version",+ "Properties": {"FunctionName": fn},+ },+ },+ }+ try:+ cfn.create_stack(StackName=stack_name, TemplateBody=json.dumps(template))+ stack = _wait_stack(cfn, stack_name)+ assert stack["StackStatus"] == "CREATE_COMPLETE", stack.get("StackStatusReason")++ detail = cfn.describe_stack_resource(+ StackName=stack_name, LogicalResourceId="Ver"+ )["StackResourceDetail"]+ assert detail["PhysicalResourceId"].endswith(f":function:{fn}:1")++ versions = lam.list_versions_by_function(FunctionName=fn)["Versions"]+ assert "1" in [v["Version"] for v in versions]++ cfn.delete_stack(StackName=stack_name)+ stack = _wait_stack(cfn, stack_name)+ assert stack["StackStatus"] == "DELETE_COMPLETE"++ # The version is gone; the (externally owned) function is untouched.+ versions = lam.list_versions_by_function(FunctionName=fn)["Versions"]+ assert [v["Version"] for v in versions] == ["$LATEST"]+ finally:+ _delete_cfn_test_stack(cfn, stack_name)+ try:+ lam.delete_function(FunctionName=fn)+ except ClientError:+ pass+++def test_cfn_appsync_schema_stack_delete_removes_schema(cfn, appsync):+ """AWS::AppSync::GraphQLSchema now has a real delete handler: deleting the+ stack removes the schema from the (externally owned) API."""+ suffix = _uuid_mod.uuid4().hex[:8]+ stack_name = f"cfn-appsync-schema-{suffix}"+ api = appsync.create_graphql_api(+ name=f"cfn-schema-api-{suffix}", authenticationType="API_KEY"+ )["graphqlApi"]+ api_id = api["apiId"]+ template = {+ "AWSTemplateFormatVersion": "2010-09-09",+ "Resources": {+ "Schema": {+ "Type": "AWS::AppSync::GraphQLSchema",+ "Properties": {+ "ApiId": api_id,+ "Definition": "type Query { hello: String }",+ },+ },+ },+ }+ try:+ cfn.create_stack(StackName=stack_name, TemplateBody=json.dumps(template))+ stack = _wait_stack(cfn, stack_name)+ assert stack["StackStatus"] == "CREATE_COMPLETE", stack.get("StackStatusReason")+ assert len(appsync.list_types(apiId=api_id, format="SDL")["types"]) == 1++ cfn.delete_stack(StackName=stack_name)+ stack = _wait_stack(cfn, stack_name)+ assert stack["StackStatus"] == "DELETE_COMPLETE"+ assert appsync.list_types(apiId=api_id, format="SDL")["types"] == []+ finally:+ _delete_cfn_test_stack(cfn, stack_name)+ try:+ appsync.delete_graphql_api(apiId=api_id)+ except ClientError:+ pass+++# ===========================================================================+# In-place update handlers — deploy, update a mutable property, assert the+# physical id survived and the new value is visible through the service API+# ===========================================================================++def _cfn_update_roundtrip(cfn, stack_name, template_v1, template_v2):+ """Create with v1, assert CREATE_COMPLETE; update to v2, assert+ UPDATE_COMPLETE. Returns nothing — callers assert on the services."""+ cfn.create_stack(StackName=stack_name, TemplateBody=json.dumps(template_v1))+ stack = _wait_stack(cfn, stack_name)+ assert stack["StackStatus"] == "CREATE_COMPLETE", stack.get("StackStatusReason")+ cfn.update_stack(StackName=stack_name, TemplateBody=json.dumps(template_v2))+ stack = _wait_stack(cfn, stack_name)+ assert stack["StackStatus"] == "UPDATE_COMPLETE", stack.get("StackStatusReason")+++def _stack_physical_id(cfn, stack_name, logical_id):+ return cfn.describe_stack_resource(+ StackName=stack_name, LogicalResourceId=logical_id+ )["StackResourceDetail"]["PhysicalResourceId"]+++def test_cfn_update_lambda_function_in_place(cfn, lam):+ """A Lambda function updates through UpdateFunctionCode/-Configuration:+ the published version and the resource policy — which the old fall-back+ re-create wiped — survive, and the new code/config are live."""+ suffix = _uuid_mod.uuid4().hex[:8]+ stack_name = f"cfn-up-lambda-{suffix}"+ fn = f"cfn-up-lambda-{suffix}"++ def tpl(source, timeout, env_value):+ return {+ "AWSTemplateFormatVersion": "2010-09-09",+ "Resources": {+ "Fn": {+ "Type": "AWS::Lambda::Function",+ "Properties": {+ "FunctionName": fn,+ "Runtime": "python3.12",+ "Handler": "index.handler",+ "Role": _CR_LAMBDA_ROLE,+ "Timeout": timeout,+ "Environment": {"Variables": {"STAGE": env_value}},+ "Code": {"ZipFile": source},+ },+ },+ "Ver": {+ "Type": "AWS::Lambda::Version",+ "Properties": {"FunctionName": {"Ref": "Fn"}},+ },+ },+ }++ v1 = tpl("def handler(e,c): return 1", 3, "one")+ v2 = tpl("def handler(e,c): return 2", 7, "two")+ try:+ cfn.create_stack(StackName=stack_name, TemplateBody=json.dumps(v1))+ stack = _wait_stack(cfn, stack_name)+ assert stack["StackStatus"] == "CREATE_COMPLETE", stack.get("StackStatusReason")++ # State CloudFormation doesn't own: a resource-policy statement.+ lam.add_permission(+ FunctionName=fn, StatementId="ext", Action="lambda:InvokeFunction",+ Principal="s3.amazonaws.com",+ )+ sha_before = lam.get_function_configuration(FunctionName=fn)["CodeSha256"]++ cfn.update_stack(StackName=stack_name, TemplateBody=json.dumps(v2))+ stack = _wait_stack(cfn, stack_name)+ assert stack["StackStatus"] == "UPDATE_COMPLETE", stack.get("StackStatusReason")++ assert _stack_physical_id(cfn, stack_name, "Fn") == fn+ config = lam.get_function_configuration(FunctionName=fn)+ assert config["Timeout"] == 7+ assert config["Environment"]["Variables"] == {"STAGE": "two"}+ assert config["CodeSha256"] != sha_before++ # The version published by the stack and the externally added policy+ # statement both survive the update.+ versions = [v["Version"] for v in+ lam.list_versions_by_function(FunctionName=fn)["Versions"]]+ assert "1" in versions+ policy = json.loads(lam.get_policy(FunctionName=fn)["Policy"])+ assert [s["Sid"] for s in policy["Statement"]] == ["ext"]+ finally:+ _delete_cfn_test_stack(cfn, stack_name)+ try:+ lam.delete_function(FunctionName=fn)+ except ClientError:+ pass+++def test_cfn_update_dynamodb_table_in_place(cfn, ddb):+ """A DynamoDB table updates through UpdateTable: items survive, a GSI is+ added, the stream specification lands."""+ suffix = _uuid_mod.uuid4().hex[:8]+ stack_name = f"cfn-up-ddb-{suffix}"+ table = f"cfn-up-ddb-{suffix}"++ def tpl(with_gsi, with_stream):+ props = {+ "TableName": table,+ "AttributeDefinitions": [+ {"AttributeName": "pk", "AttributeType": "S"},+ {"AttributeName": "gsipk", "AttributeType": "S"},+ ],+ "KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}],+ "BillingMode": "PAY_PER_REQUEST",+ }+ if with_gsi:+ props["GlobalSecondaryIndexes"] = [{+ "IndexName": "by-gsipk",+ "KeySchema": [{"AttributeName": "gsipk", "KeyType": "HASH"}],+ "Projection": {"ProjectionType": "ALL"},+ }]+ if with_stream:+ props["StreamSpecification"] = {"StreamViewType": "NEW_AND_OLD_IMAGES"}+ return {+ "AWSTemplateFormatVersion": "2010-09-09",+ "Resources": {"Table": {"Type": "AWS::DynamoDB::Table", "Properties": props}},+ }++ try:+ cfn.create_stack(StackName=stack_name, TemplateBody=json.dumps(tpl(False, False)))+ stack = _wait_stack(cfn, stack_name)+ assert stack["StackStatus"] == "CREATE_COMPLETE", stack.get("StackStatusReason")++ ddb.put_item(TableName=table, Item={"pk": {"S": "x"}, "gsipk": {"S": "y"}})++ cfn.update_stack(StackName=stack_name, TemplateBody=json.dumps(tpl(True, True)))+ stack = _wait_stack(cfn, stack_name)+ assert stack["StackStatus"] == "UPDATE_COMPLETE", stack.get("StackStatusReason")++ assert _stack_physical_id(cfn, stack_name, "Table") == table+ desc = ddb.describe_table(TableName=table)["Table"]+ assert [g["IndexName"] for g in desc.get("GlobalSecondaryIndexes", [])] == ["by-gsipk"]+ assert desc.get("LatestStreamArn")+ # The item survived — the table was not re-created.+ item = ddb.get_item(TableName=table, Key={"pk": {"S": "x"}})+ assert item.get("Item", {}).get("gsipk", {}).get("S") == "y"+ finally:+ _delete_cfn_test_stack(cfn, stack_name)+++def test_cfn_update_apigw_rest_api_in_place(cfn, apigw_v1):+ """A REST API's mutable fields patch in place; the API id survives."""+ suffix = _uuid_mod.uuid4().hex[:8]+ stack_name = f"cfn-up-restapi-{suffix}"++ def tpl(description):+ return {+ "AWSTemplateFormatVersion": "2010-09-09",+ "Resources": {+ "Api": {+ "Type": "AWS::ApiGateway::RestApi",+ "Properties": {"Name": f"cfn-up-api-{suffix}", "Description": description},+ },+ },+ }++ try:+ _cfn_update_roundtrip(cfn, stack_name, tpl("before"), tpl("after"))+ api_id = _stack_physical_id(cfn, stack_name, "Api")+ api = apigw_v1.get_rest_api(restApiId=api_id)+ assert api["description"] == "after"+ assert api["name"] == f"cfn-up-api-{suffix}"+ finally:+ _delete_cfn_test_stack(cfn, stack_name)+++def test_cfn_update_apigw_resource_method_stage_in_place(cfn, apigw_v1):+ """Method and Stage update in place under a stable REST API; a Resource's+ PathPart change is a replacement (all its properties are create-only)."""+ suffix = _uuid_mod.uuid4().hex[:8]+ stack_name = f"cfn-up-apigw-{suffix}"++ def tpl(path_part, api_key_required, stage_description):+ return {+ "AWSTemplateFormatVersion": "2010-09-09",+ "Resources": {+ "Api": {+ "Type": "AWS::ApiGateway::RestApi",+ "Properties": {"Name": f"cfn-up-apigw-{suffix}"},+ },+ "Res": {+ "Type": "AWS::ApiGateway::Resource",+ "Properties": {+ "RestApiId": {"Ref": "Api"},+ "ParentId": {"Fn::GetAtt": ["Api", "RootResourceId"]},+ "PathPart": path_part,+ },+ },+ "Method": {+ "Type": "AWS::ApiGateway::Method",+ "Properties": {+ "RestApiId": {"Ref": "Api"},+ "ResourceId": {"Fn::GetAtt": ["Api", "RootResourceId"]},+ "HttpMethod": "GET",+ "AuthorizationType": "NONE",+ "ApiKeyRequired": api_key_required,+ "Integration": {"Type": "MOCK"},+ },+ },+ "Deployment": {+ "Type": "AWS::ApiGateway::Deployment",+ "DependsOn": "Method",+ "Properties": {"RestApiId": {"Ref": "Api"}},+ },+ "Stage": {+ "Type": "AWS::ApiGateway::Stage",+ "Properties": {+ "RestApiId": {"Ref": "Api"},+ "StageName": "test",+ "DeploymentId": {"Ref": "Deployment"},+ "Description": stage_description,+ },+ },+ },+ }++ try:+ _cfn_update_roundtrip(+ cfn, stack_name,+ tpl("orders", False, "before"),+ tpl("invoices", True, "after"),+ )+ api_id = _stack_physical_id(cfn, stack_name, "Api")++ # Resource replaced: only the new path exists.+ paths = {r["path"] for r in apigw_v1.get_resources(restApiId=api_id)["items"]}+ assert "/invoices" in paths+ assert "/orders" not in paths++ # Method updated in place on the same identity.+ root_id = next(+ r["id"] for r in apigw_v1.get_resources(restApiId=api_id)["items"]+ if r["path"] == "/"+ )+ method = apigw_v1.get_method(restApiId=api_id, resourceId=root_id, httpMethod="GET")+ assert method["apiKeyRequired"] is True++ # Stage updated in place under its stable name.+ stage = apigw_v1.get_stage(restApiId=api_id, stageName="test")+ assert stage["description"] == "after"+ finally:+ _delete_cfn_test_stack(cfn, stack_name)+++def test_cfn_update_iot_topic_rule_in_place(cfn, iot_client):+ """A topic rule's payload is replaced in place, as ReplaceTopicRule does;+ the rule name, ARN and creation time survive."""+ suffix = _uuid_mod.uuid4().hex[:8]+ stack_name = f"cfn-up-iotrule-{suffix}"+ rule_name = f"cfn_up_rule_{suffix}"++ def tpl(topic):+ return {+ "AWSTemplateFormatVersion": "2010-09-09",+ "Resources": {+ "Rule": {+ "Type": "AWS::IoT::TopicRule",+ "Properties": {+ "RuleName": rule_name,+ "TopicRulePayload": {+ "Sql": f"SELECT * FROM '{topic}'",+ "Actions": [{"Republish": {+ "Topic": "out/topic",+ "RoleArn": "arn:aws:iam::000000000000:role/iot-role",+ }}],+ },+ },+ },+ },+ }++ try:+ cfn.create_stack(StackName=stack_name, TemplateBody=json.dumps(tpl("things/in")))+ stack = _wait_stack(cfn, stack_name)+ assert stack["StackStatus"] == "CREATE_COMPLETE", stack.get("StackStatusReason")+ created_at = iot_client.get_topic_rule(ruleName=rule_name)["rule"]["createdAt"]++ cfn.update_stack(StackName=stack_name, TemplateBody=json.dumps(tpl("things/other")))+ stack = _wait_stack(cfn, stack_name)+ assert stack["StackStatus"] == "UPDATE_COMPLETE", stack.get("StackStatusReason")++ assert _stack_physical_id(cfn, stack_name, "Rule") == rule_name+ rule = iot_client.get_topic_rule(ruleName=rule_name)["rule"]+ assert rule["sql"] == "SELECT * FROM 'things/other'"+ assert rule["createdAt"] == created_at+ finally:+ _delete_cfn_test_stack(cfn, stack_name)+++def test_cfn_update_sns_topic_in_place(cfn, sns):+ """A topic's DisplayName updates in place: the ARN — and a subscription+ created outside the stack — survive."""+ suffix = _uuid_mod.uuid4().hex[:8]+ stack_name = f"cfn-up-sns-{suffix}"+ topic_name = f"cfn-up-sns-{suffix}"++ def tpl(display_name):+ return {+ "AWSTemplateFormatVersion": "2010-09-09",+ "Resources": {+ "Topic": {+ "Type": "AWS::SNS::Topic",+ "Properties": {"TopicName": topic_name, "DisplayName": display_name},+ },+ },+ }++ try:+ cfn.create_stack(StackName=stack_name, TemplateBody=json.dumps(tpl("before")))+ stack = _wait_stack(cfn, stack_name)+ assert stack["StackStatus"] == "CREATE_COMPLETE", stack.get("StackStatusReason")++ topic_arn = _stack_physical_id(cfn, stack_name, "Topic")+ sns.subscribe(TopicArn=topic_arn, Protocol="email", Endpoint="[email protected]")++ cfn.update_stack(StackName=stack_name, TemplateBody=json.dumps(tpl("after")))+ stack = _wait_stack(cfn, stack_name)+ assert stack["StackStatus"] == "UPDATE_COMPLETE", stack.get("StackStatusReason")++ assert _stack_physical_id(cfn, stack_name, "Topic") == topic_arn+ attrs = sns.get_topic_attributes(TopicArn=topic_arn)["Attributes"]+ assert attrs["DisplayName"] == "after"+ subs = sns.list_subscriptions_by_topic(TopicArn=topic_arn)["Subscriptions"]+ assert [s["Endpoint"] for s in subs] == ["[email protected]"]+ finally:+ _delete_cfn_test_stack(cfn, stack_name)+++def test_cfn_update_sns_topic_keeps_standalone_subscription(cfn, sns):+ """Removing an inline Subscription entry must not delete a standalone+ AWS::SNS::Subscription record carrying the same protocol and endpoint."""+ suffix = _uuid_mod.uuid4().hex[:8]+ stack_name = f"cfn-up-sns-sub-{suffix}"+ topic_name = f"cfn-up-sns-sub-{suffix}"+ endpoint = "[email protected]"++ def tpl(with_inline):+ topic_props = {"TopicName": topic_name}+ if with_inline:+ topic_props["Subscription"] = [{"Protocol": "email", "Endpoint": endpoint}]+ return {+ "AWSTemplateFormatVersion": "2010-09-09",+ "Resources": {+ "Topic": {"Type": "AWS::SNS::Topic", "Properties": topic_props},+ "Sub": {+ "Type": "AWS::SNS::Subscription",+ "Properties": {+ "TopicArn": {"Ref": "Topic"},+ "Protocol": "email",+ "Endpoint": endpoint,+ },+ },+ },+ }++ try:+ _cfn_update_roundtrip(cfn, stack_name, tpl(True), tpl(False))+ topic_arn = _stack_physical_id(cfn, stack_name, "Topic")+ standalone_arn = _stack_physical_id(cfn, stack_name, "Sub")+ subs = sns.list_subscriptions_by_topic(TopicArn=topic_arn)["Subscriptions"]+ assert [s["SubscriptionArn"] for s in subs] == [standalone_arn]+ finally:+ _delete_cfn_test_stack(cfn, stack_name)+++def test_cfn_update_sqs_queue_in_place(cfn, sqs):+ """A queue's attributes update in place: the URL and the messages in the+ queue survive."""+ suffix = _uuid_mod.uuid4().hex[:8]+ stack_name = f"cfn-up-sqs-{suffix}"+ queue_name = f"cfn-up-sqs-{suffix}"++ def tpl(visibility):+ return {+ "AWSTemplateFormatVersion": "2010-09-09",+ "Resources": {+ "Queue": {+ "Type": "AWS::SQS::Queue",+ "Properties": {"QueueName": queue_name, "VisibilityTimeout": visibility},+ },+ },+ }++ try:+ cfn.create_stack(StackName=stack_name, TemplateBody=json.dumps(tpl(30)))+ stack = _wait_stack(cfn, stack_name)+ assert stack["StackStatus"] == "CREATE_COMPLETE", stack.get("StackStatusReason")++ queue_url = _stack_physical_id(cfn, stack_name, "Queue")+ sqs.send_message(QueueUrl=queue_url, MessageBody="survives")++ cfn.update_stack(StackName=stack_name, TemplateBody=json.dumps(tpl(120)))+ stack = _wait_stack(cfn, stack_name)+ assert stack["StackStatus"] == "UPDATE_COMPLETE", stack.get("StackStatusReason")++ assert _stack_physical_id(cfn, stack_name, "Queue") == queue_url+ attrs = sqs.get_queue_attributes(+ QueueUrl=queue_url, AttributeNames=["VisibilityTimeout"]+ )["Attributes"]+ assert attrs["VisibilityTimeout"] == "120"+ messages = sqs.receive_message(QueueUrl=queue_url).get("Messages", [])+ assert [m["Body"] for m in messages] == ["survives"]+ finally:+ _delete_cfn_test_stack(cfn, stack_name)+++def test_cfn_update_sqs_queue_rename_replaces(cfn, sqs):+ """Changing QueueName is a replacement: the stack tracks the new queue's+ URL as the physical id and the old queue is gone."""+ suffix = _uuid_mod.uuid4().hex[:8]+ stack_name = f"cfn-up-sqs-rn-{suffix}"+ old_name = f"cfn-up-sqs-rn-old-{suffix}"+ new_name = f"cfn-up-sqs-rn-new-{suffix}"++ def tpl(queue_name):+ return {+ "AWSTemplateFormatVersion": "2010-09-09",+ "Resources": {+ "Queue": {+ "Type": "AWS::SQS::Queue",+ "Properties": {"QueueName": queue_name},+ },+ },+ }++ try:+ _cfn_update_roundtrip(cfn, stack_name, tpl(old_name), tpl(new_name))+ queue_url = _stack_physical_id(cfn, stack_name, "Queue")+ assert queue_url.endswith(f"/{new_name}")+ assert sqs.get_queue_url(QueueName=new_name)["QueueUrl"] == queue_url+ with pytest.raises(ClientError):+ sqs.get_queue_url(QueueName=old_name)+ finally:+ _delete_cfn_test_stack(cfn, stack_name)+++def test_cfn_update_logs_log_group_in_place(cfn, logs):+ """A log group's retention updates in place; its streams survive."""+ suffix = _uuid_mod.uuid4().hex[:8]+ stack_name = f"cfn-up-logs-{suffix}"+ group_name = f"/cfn/up/{suffix}"++ def tpl(retention):+ return {+ "AWSTemplateFormatVersion": "2010-09-09",+ "Resources": {+ "Group": {+ "Type": "AWS::Logs::LogGroup",+ "Properties": {"LogGroupName": group_name, "RetentionInDays": retention},+ },+ },+ }++ try:+ cfn.create_stack(StackName=stack_name, TemplateBody=json.dumps(tpl(7)))+ stack = _wait_stack(cfn, stack_name)+ assert stack["StackStatus"] == "CREATE_COMPLETE", stack.get("StackStatusReason")++ logs.create_log_stream(logGroupName=group_name, logStreamName="ext-stream")++ cfn.update_stack(StackName=stack_name, TemplateBody=json.dumps(tpl(30)))+ stack = _wait_stack(cfn, stack_name)+ assert stack["StackStatus"] == "UPDATE_COMPLETE", stack.get("StackStatusReason")++ group = logs.describe_log_groups(+ logGroupNamePrefix=group_name)["logGroups"][0]+ assert group["retentionInDays"] == 30+ streams = logs.describe_log_streams(logGroupName=group_name)["logStreams"]+ assert [s["logStreamName"] for s in streams] == ["ext-stream"]+ finally:+ _delete_cfn_test_stack(cfn, stack_name)+++def test_cfn_update_s3_bucket_policy_in_place(cfn, s3):+ """A bucket policy's document is rewritten in place on the same bucket."""+ suffix = _uuid_mod.uuid4().hex[:8]+ stack_name = f"cfn-up-bucketpolicy-{suffix}"+ bucket = f"cfn-up-bucketpolicy-{suffix}"+ s3.create_bucket(Bucket=bucket)++ def tpl(sid):+ return {+ "AWSTemplateFormatVersion": "2010-09-09",+ "Resources": {+ "Policy": {+ "Type": "AWS::S3::BucketPolicy",+ "Properties": {+ "Bucket": bucket,+ "PolicyDocument": {+ "Version": "2012-10-17",+ "Statement": [{+ "Sid": sid,+ "Effect": "Allow",+ "Principal": "*",+ "Action": "s3:GetObject",+ "Resource": f"arn:aws:s3:::{bucket}/*",+ }],+ },+ },+ },+ },+ }++ try:+ _cfn_update_roundtrip(cfn, stack_name, tpl("Before"), tpl("After"))+ policy = json.loads(s3.get_bucket_policy(Bucket=bucket)["Policy"])+ assert policy["Statement"][0]["Sid"] == "After"+ finally:+ _delete_cfn_test_stack(cfn, stack_name)+ try:+ s3.delete_bucket(Bucket=bucket)+ except ClientError:+ pass+++def test_cfn_update_iam_role_in_place(cfn, iam):+ """A role updates in place: ARN and RoleId survive, the template's inline+ policies and managed attachments reconcile, and an attachment made outside+ the stack is untouched."""+ suffix = _uuid_mod.uuid4().hex[:8]+ stack_name = f"cfn-up-role-{suffix}"+ role_name = f"cfn-up-role-{suffix}"+ ext_policy_arn = "arn:aws:iam::aws:policy/ReadOnlyAccess"+ assume = {+ "Version": "2012-10-17",+ "Statement": [{+ "Effect": "Allow",+ "Principal": {"Service": "lambda.amazonaws.com"},+ "Action": "sts:AssumeRole",+ }],+ }++ def tpl(inline_name, max_session):+ return {+ "AWSTemplateFormatVersion": "2010-09-09",+ "Resources": {+ "Role": {+ "Type": "AWS::IAM::Role",+ "Properties": {+ "RoleName": role_name,+ "AssumeRolePolicyDocument": assume,+ "MaxSessionDuration": max_session,+ "Policies": [{+ "PolicyName": inline_name,+ "PolicyDocument": {+ "Version": "2012-10-17",+ "Statement": [{+ "Effect": "Allow",+ "Action": "s3:ListBucket",+ "Resource": "*",+ }],+ },+ }],+ },+ },+ },+ }++ try:+ cfn.create_stack(StackName=stack_name, TemplateBody=json.dumps(tpl("inline-a", 3600)))+ stack = _wait_stack(cfn, stack_name)+ assert stack["StackStatus"] == "CREATE_COMPLETE", stack.get("StackStatusReason")++ role_id_before = iam.get_role(RoleName=role_name)["Role"]["RoleId"]+ iam.attach_role_policy(RoleName=role_name, PolicyArn=ext_policy_arn)++ cfn.update_stack(StackName=stack_name, TemplateBody=json.dumps(tpl("inline-b", 7200)))+ stack = _wait_stack(cfn, stack_name)+ assert stack["StackStatus"] == "UPDATE_COMPLETE", stack.get("StackStatusReason")++ role = iam.get_role(RoleName=role_name)["Role"]+ assert role["RoleId"] == role_id_before+ assert role["MaxSessionDuration"] == 7200+ inline = iam.list_role_policies(RoleName=role_name)["PolicyNames"]+ assert inline == ["inline-b"]+ attached = iam.list_attached_role_policies(RoleName=role_name)["AttachedPolicies"]+ assert [p["PolicyArn"] for p in attached] == [ext_policy_arn]+ finally:+ _delete_cfn_test_stack(cfn, stack_name)+++def test_cfn_update_iam_managed_policy_in_place(cfn, iam):+ """A managed policy's document update creates a new default version on+ the same ARN, exactly as CloudFormation's handler does."""+ suffix = _uuid_mod.uuid4().hex[:8]+ stack_name = f"cfn-up-mp-{suffix}"+ policy_name = f"cfn-up-mp-{suffix}"++ def tpl(action):+ return {+ "AWSTemplateFormatVersion": "2010-09-09",+ "Resources": {+ "Policy": {+ "Type": "AWS::IAM::ManagedPolicy",+ "Properties": {+ "ManagedPolicyName": policy_name,+ "PolicyDocument": {+ "Version": "2012-10-17",+ "Statement": [{+ "Effect": "Allow",+ "Action": action,+ "Resource": "*",+ }],+ },+ },+ },+ },+ }++ try:+ _cfn_update_roundtrip(cfn, stack_name, tpl("s3:ListBucket"), tpl("s3:GetObject"))+ policy_arn = _stack_physical_id(cfn, stack_name, "Policy")+ policy = iam.get_policy(PolicyArn=policy_arn)["Policy"]+ assert policy["DefaultVersionId"] == "v2"+ version = iam.get_policy_version(+ PolicyArn=policy_arn, VersionId="v2"+ )["PolicyVersion"]+ document = version["Document"]+ if isinstance(document, str):+ document = json.loads(document)+ assert document["Statement"][0]["Action"] == "s3:GetObject"+ finally:+ _delete_cfn_test_stack(cfn, stack_name)