Skip to content

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

feat(cloudformation): replace a resource when a create-only property changes (#1949)

iot-rocketd054865eaa4f
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 1202869a..d2d43b34 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -9,6 +9,7 @@ Versioning follows [Semantic Versioning](https://semver.org/).
### Added
+- **CloudFormation — stack updates replace resources** — a change to a property the change set reports as `RequiresRecreation: Always` now creates a new resource under a new generated name, points its dependents at it and deletes the old one after the update succeeds (a rollback deletes the new one instead). With an explicit, unchanged name the update fails with the AWS message naming the physical id; this now also covers `AWS::Lambda::Function`. A named SQS queue or SNS topic fails with AWS's already-exists error instead and keeps its messages or subscriptions.
- **CloudFormation — `IMPORT` change sets execute** — existing SQS queues, SSM parameters, S3 buckets, DynamoDB tables, IAM roles, log groups, Lambda functions, IoT policies, IoT CA certificates and Cognito user pools are adopted into a new or existing stack without being changed (`IMPORT_IN_PROGRESS` to `IMPORT_COMPLETE`, or a rollback when a resource is gone), and an import that changes `Outputs` or stack tags is refused, as on AWS.
- **CloudFormation — `Capabilities` check without `AUTH`** — `CFN_ENFORCE_CAPABILITIES=1` refuses a template whose IAM resources or macros the request does not acknowledge also without `AUTH=true`, as AWS always does.
diff --git a/ministack/services/cloudformation/provisioners.py b/ministack/services/cloudformation/provisioners.py
index db5f8387..b301938d 100644
--- a/ministack/services/cloudformation/provisioners.py
+++ b/ministack/services/cloudformation/provisioners.py
@@ -68,11 +68,15 @@ logger = logging.getLogger("cloudformation")
# so AWS::Region / ARNs reflect the caller's request region (#398).
REGION = os.environ.get("MINISTACK_REGION", "us-east-1")
_MINISTACK_HOST = os.environ.get("MINISTACK_HOST", "localhost")
+# Mixed into the suffix of a generated name. A replacement draws a new one so
+# the new resource does not take its predecessor's name; the engine keeps it
+# on the resource record and sets it again for every later update.
+_NAME_SEED = contextvars.ContextVar("cfn_name_seed", default="")
def _physical_name(stack_name: str, logical_id: str, *,
lowercase: bool = False, max_len: int = 128,
- replacing: str = "") -> str:
+ seeded: bool = True) -> str:
"""Generate an AWS-style physical resource name: {stack}-{logicalId}-{SUFFIX}.
Matches the pattern AWS CloudFormation uses for auto-named resources so that
@@ -91,10 +95,8 @@ def _physical_name(stack_name: str, logical_id: str, *,
update silently produced a brand new, empty resource under a new name,
orphaning the real one — and anything referencing it via Ref/Fn::GetAtt
picked up that new (wrong) identity the moment it was reprocessed later in
- the same update. Resource *replacement* (a property change real AWS can't
- apply in place) is not detected here: a handler that replaces passes
- ``replacing`` (the predecessor's physical id), so the replacement gets a
- name of its own.
+ the same update. A replacement changes the suffix through ``_NAME_SEED``,
+ unless ``seeded`` is false.
Truncates the `{stack}-{logicalId}-` prefix, never the suffix: a naive
`base[:max_len]` on the full concatenated string drops whatever falls past
@@ -111,8 +113,9 @@ def _physical_name(stack_name: str, logical_id: str, *,
cut, since the suffix alone (hashed from stack_name *and* logical_id) is
what actually guarantees uniqueness here.
"""
- seed = f"{stack_name}:{logical_id}" + (f":{replacing}" if replacing else "")
- suffix = hashlib.sha256(seed.encode()).hexdigest()[:13].upper()
+ seed = _NAME_SEED.get() if seeded else ""
+ key = f"{stack_name}:{logical_id}:{seed}" if seed else f"{stack_name}:{logical_id}"
+ suffix = hashlib.sha256(key.encode()).hexdigest()[:13].upper()
prefix = f"{stack_name}-{logical_id}-"
available = max(max_len - len(suffix), 0)
base = prefix[:available] + suffix
@@ -662,6 +665,10 @@ def _requires_replacement_pipes(old_props, new_props):
# custom-named resource (you must rename it first), so MiniStack must fail the
# update instead of silently executing the replacement and destroying data
# (issue #1433).
+# A replacement is a change to a property of the type's _REPLACING_PROPERTIES
+# row, or one ``requires_replacement`` adds. A type with an ``exists`` message
+# is not refused up front: AWS runs the create, which fails with that message
+# because the predecessor still holds the name.
_CUSTOM_NAME_REPLACEMENT = {
"AWS::DynamoDB::Table": {
"name": "TableName",
@@ -735,6 +742,15 @@ _CUSTOM_NAME_REPLACEMENT = {
!= new.get("TemplateType", "FLEET_PROVISIONING")
),
},
+ "AWS::Lambda::Function": {"name": "FunctionName"},
+ "AWS::SQS::Queue": {
+ "name": "QueueName",
+ "exists": "Queue creation failed because the queue already exists",
+ },
+ "AWS::SNS::Topic": {
+ "name": "TopicName",
+ "exists": "Topic creation failed because the topic already exists",
+ },
# "If you specify a name, you cannot perform updates that require
# replacement of this resource, but you can perform other updates"
# (aws-resource-elasticloadbalancingv2-loadbalancer), which is this rule
@@ -811,7 +827,8 @@ _CUSTOM_NAME_REPLACEMENT = {
}
-def _custom_named_replacement_error(resource_type, old_props, new_props):
+def _custom_named_replacement_error(resource_type, old_props, new_props,
+ physical_id=None):
"""Return the real-AWS error when a stack update would replace a
custom-named resource, else None.
@@ -819,29 +836,30 @@ def _custom_named_replacement_error(resource_type, old_props, new_props):
sanctioned escape hatch (the replacement proceeds under the new name), and
an auto-generated name can always be replaced.
"""
- spec = _CUSTOM_NAME_REPLACEMENT.get(resource_type)
- if not spec:
+ name = _kept_custom_name(resource_type, old_props, new_props)
+ if not name or "exists" in _CUSTOM_NAME_REPLACEMENT[resource_type]:
return None
- old_name = old_props.get(spec["name"])
- new_name = new_props.get(spec["name"])
- if not old_name or old_name != new_name:
- return None
- if spec["requires_replacement"](old_props, new_props):
+ if _requires_replacement(resource_type, old_props, new_props):
return (
"CloudFormation cannot update a stack when a custom-named resource "
- f"requires replacing. Rename {old_name} and update the stack again."
+ f"requires replacing. Rename {physical_id or name} and update "
+ "the stack again."
)
return None
-def _requires_replacement(resource_type, old_props, new_props):
- """Whether a replacing property of the type changed, read from the table
- ``_custom_named_replacement_error`` reads. An explicit physical name
- refuses that replacement above the handler; under a generated name there
- is nothing to refuse and the handler performs it, so both answers come
- from one definition."""
+def _kept_custom_name(resource_type, old_props, new_props):
+ """The explicit name an update keeps unchanged, else None."""
spec = _CUSTOM_NAME_REPLACEMENT.get(resource_type)
- return bool(spec and spec["requires_replacement"](old_props, new_props))
+ name = old_props.get(spec["name"]) if spec else None
+ return name if name and name == new_props.get(spec["name"]) else None
+
+
+def _requires_replacement(resource_type, old_props, new_props):
+ """Whether a create-only property or an entry's extra predicate changed."""
+ extra = _CUSTOM_NAME_REPLACEMENT.get(resource_type, {}).get("requires_replacement")
+ return (_replacing_change(resource_type, old_props, new_props)
+ or bool(extra and extra(old_props, new_props)))
# Set by the stack engine around an update whose resource carries
@@ -897,13 +915,14 @@ def _delete_predecessor(delete_fn, *args, **kwargs):
retaining set): the engine then records the DELETE_SKIPPED event and the
predecessor stays, as on AWS. Every update handler that creates the
replacement itself removes the old resource through this, so the policy
- cannot be forgotten at one site, with these exceptions. The DynamoDB
- table, the Location tracker, the IoT thing type, the Glue trigger and the
- RDS DB cluster and instance have a deterministic generated name: the
- replacement takes the name back, so there is nothing left to retain. The
- other is the Lambda permission's degenerate ``Id`` branch, which removes
- and re-puts one statement under a Sid that cannot change: the physical id
- is kept, nothing is replaced, and the policy does not apply.
+ cannot be forgotten at one site, with five exceptions. Four have a
+ deterministic generated name (the DynamoDB table, the IoT thing type and
+ the ElastiCache cache cluster and replication group): the replacement
+ takes the name back, so there is nothing left to retain. The fifth is the
+ Lambda permission's degenerate ``Id`` branch,
+ which removes and re-puts one statement under a Sid that cannot change:
+ the physical id is kept, nothing is replaced, and the policy does not
+ apply.
"""
if _RETAIN_REPLACED.get():
return
@@ -914,16 +933,21 @@ def _delete_predecessor(delete_fn, *args, **kwargs):
delete_fn(*args, **kwargs)
-def _replacing_update(create_fn, delete_fn):
- """An update handler for a type whose every property is create-only."""
- def update(physical_id, old_props, new_props, stack_name, logical_id=None):
- # An unchanged custom name was refused above the handler; a generated
- # name gets a new one, so the predecessor survives until cleanup.
- created = create_fn(logical_id or physical_id, new_props, stack_name,
- replacing=physical_id)
- _delete_predecessor(delete_fn, physical_id, old_props)
- return created
- return update
+def _replace_resource(resource_type, physical_id, old_props, new_props,
+ stack_name, logical_id):
+ """Create the resource anew under a new generated name."""
+ name = _kept_custom_name(resource_type, old_props, new_props)
+ if name:
+ raise ValueError(_CUSTOM_NAME_REPLACEMENT[resource_type]["exists"].format(name=name))
+ # Not reset: the caller keeps the seed on the resource record.
+ _NAME_SEED.set(new_uuid()[:8])
+ created = _provision_resource(resource_type, logical_id or physical_id,
+ new_props, stack_name)
+ # A stack deletes the predecessor in its cleanup phase; a nested stack has none.
+ if created[0] != physical_id and _DEFERRED_PREDECESSOR_DELETES.get() is None:
+ _delete_predecessor(_delete_resource, resource_type, physical_id,
+ old_props, stack_name, logical_id)
+ return created
def _update_resource(resource_type: str, physical_id: str, old_props: dict,
@@ -963,10 +987,13 @@ def _update_resource(resource_type: str, physical_id: str, old_props: dict,
resource_type, physical_id)
return physical_id, old_attrs or {}
replacement_error = _custom_named_replacement_error(
- resource_type, old_props, new_props
+ resource_type, old_props, new_props, physical_id
)
if replacement_error:
raise ValueError(replacement_error)
+ if _replacing_change(resource_type, old_props, new_props):
+ return _replace_resource(resource_type, physical_id, old_props,
+ new_props, stack_name, logical_id)
if handler and "update" in handler:
if handler.get("update_with_logical_id"):
return handler["update"](
@@ -1920,9 +1947,7 @@ def _ddb_update(physical_id, old_props, new_props, stack_name, logical_id=None):
# Not routed through _delete_predecessor: the emulator's generated
# name is deterministic, so the table comes back under the same name
# and the old one is lost even under UpdateReplacePolicy Retain (AWS
- # would mint a new name and keep the old table). AWS::Location::Tracker
- # is the other type with that shape and is left alone for the same
- # reason.
+ # would mint a new name and keep the old table).
_ddb_delete(physical_id, old_props)
return _ddb_create(logical_id or physical_id, new_props, stack_name)
@@ -2163,9 +2188,9 @@ def _lambda_update(physical_id, old_props, new_props, stack_name, logical_id=Non
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
+ FunctionName, PackageType and TenancyConfig changes are replaced in
+ ``_update_resource`` before this runs. A DurableConfig change may require
+ replacement on AWS; under the same physical name the closest local
equivalent is the full re-provision the create fallback always did.
"""
name = new_props.get("FunctionName") or _physical_name(
@@ -2182,12 +2207,8 @@ def _lambda_update(physical_id, old_props, new_props, stack_name, logical_id=Non
code = new_props.get("Code", {})
image_uri = code.get("ImageUri")
is_image = new_props.get("PackageType") == "Image" or bool(image_uri)
- # DurableConfig and TenancyConfig are "Update requires: Replacement" in the
- # resource reference, like a PackageType flip: same local equivalent.
- replacing = is_image != (func["config"].get("PackageType") == "Image") or any(
- old_props.get(p) != new_props.get(p)
- for p in ("DurableConfig", "TenancyConfig")
- )
+ replacing = (is_image != (func["config"].get("PackageType") == "Image")
+ or old_props.get("DurableConfig") != new_props.get("DurableConfig"))
if replacing:
# The re-provision replaces the whole function record under the same
# name — a stale warm worker or pooled container would keep serving
@@ -2683,11 +2704,10 @@ def _iam_ip_roles(props):
def _iam_ip_create(logical_id, props, stack_name):
name = props.get("InstanceProfileName") or _physical_name(stack_name, logical_id, max_len=128)
path = props.get("Path", "/")
- # A generated name is the emulator's deterministic one, so a replacement
- # lands under the same name (AWS would mint a new one) and must not be
- # refused; a custom name goes through CreateInstanceProfile as it is, and
- # its EntityAlreadyExists keeps one stack from writing over a profile
- # another stack or the API owns.
+ # A generated name belongs to this stack resource, so a profile left under
+ # it is taken over; a custom name goes through CreateInstanceProfile as it
+ # is, and its EntityAlreadyExists keeps one stack from writing over a
+ # profile another stack or the API owns.
if not props.get("InstanceProfileName"):
_iam._instance_profiles.pop(name, None)
resp = _iam._create_instance_profile({"InstanceProfileName": [name], "Path": [path]})
@@ -4297,6 +4317,7 @@ def _cfn_nested_stack_deploy(logical_id, props, parent_stack_name, *,
_add_event(child_stack_id, child_name, child_logical_id, resource_type,
f"{status_prefix}_IN_PROGRESS")
+ name_seed = ""
try:
prev = prev_resources.get(child_logical_id)
new_tagged = _with_stack_tags(
@@ -4317,12 +4338,15 @@ def _cfn_nested_stack_deploy(logical_id, props, parent_stack_name, *,
# The child has no cleanup phase of its own: its handlers
# delete the predecessor at once, not into the parent's queue.
deferred_token = _DEFERRED_PREDECESSOR_DELETES.set(None)
+ seed_token = _NAME_SEED.set(prev.get("_name_seed", ""))
try:
physical_id, attrs = _update_resource(
resource_type, prev.get("PhysicalResourceId", child_logical_id),
old_tagged, new_tagged, child_name, child_logical_id,
)
+ name_seed = _NAME_SEED.get()
finally:
+ _NAME_SEED.reset(seed_token)
_DEFERRED_PREDECESSOR_DELETES.reset(deferred_token)
_RETAIN_REPLACED.reset(token)
else:
@@ -4347,6 +4371,8 @@ def _cfn_nested_stack_deploy(logical_id, props, parent_stack_name, *,
"Attributes": attrs,
"Timestamp": now_iso(),
}
+ if name_seed:
+ provisioned[child_logical_id]["_name_seed"] = name_seed
_add_event(child_stack_id, child_name, child_logical_id, resource_type,
f"{status_prefix}_COMPLETE", physical_id=physical_id)
@@ -6760,36 +6786,15 @@ def _cognito_user_pool_group_create(logical_id, props, stack_name):
def _cognito_user_pool_group_update(physical_id, old_props, new_props, stack_name,
logical_id=None):
"""Update a group in place, keeping its name (what Ref returns) and its
- members. GroupName and UserPoolId require replacement
- (https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-cognito-userpoolgroup.html):
- a rename, or a move to another pool, creates the new group before the
- old one is removed; a move that keeps an explicit GroupName was already
- refused by _custom_named_replacement_error, the way CloudFormation
- refuses to replace a custom-named resource. Description, Precedence and
+ members. GroupName and UserPoolId changes are replaced in
+ ``_update_resource`` before this runs. Description, Precedence and
RoleArn are what UpdateGroup takes, applied to the record directly since
the service has no UpdateGroup handler.
-
- The replacement prologue is spelled out here rather than going through
- _rename_replacement because a group is keyed by (pool, name), and the
- helper only compares names: a move to another pool under a generated
- name keeps the same name, so the helper would see no replacement and
- the old group would stay stranded in the old pool. The pool has to be
- part of the replacement test.
"""
- name = new_props.get("GroupName") or _physical_name(
- stack_name, logical_id or physical_id, max_len=128
- )
- old_pid = old_props.get("UserPoolId", "")
- new_pid = new_props.get("UserPoolId", "")
- pool = _cognito._user_pools.get(old_pid)
+ pool = _cognito._user_pools.get(new_props.get("UserPoolId", ""))
group = pool["_groups"].get(physical_id) if pool else None
- if group is None or name != physical_id or new_pid != old_pid:
- created = _cognito_user_pool_group_create(
- logical_id or physical_id, new_props, stack_name
- )
- if group is not None:
- _delete_predecessor(_cognito_user_pool_group_delete, physical_id, old_props)
- return created
+ if group is None:
+ return _cognito_user_pool_group_create(logical_id or physical_id, new_props, stack_name)
group["Description"] = new_props.get("Description", "")
group["RoleArn"] = new_props.get("RoleArn", "")
@@ -6798,7 +6803,7 @@ def _cognito_user_pool_group_update(physical_id, old_props, new_props, stack_nam
else:
group.pop("Precedence", None)
group["LastModifiedDate"] = _cognito._now_epoch()
- return name, {}
+ return physical_id, {}
def _cognito_user_pool_group_delete(physical_id, props):
@@ -8247,13 +8252,8 @@ def _elbv2_load_balancer_update(physical_id, old_props, new_props, stack_name,
``LoadBalancerAttributes`` and ``Tags`` are "Update requires: No
interruption" on the resource reference
(https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-elasticloadbalancingv2-loadbalancer.html);
- ``Name``, ``Scheme`` and ``Type`` require replacement. A changed name
- replaces through the shared prologue, and a ``Scheme`` or ``Type`` change
- under a custom name is refused by ``_custom_named_replacement_error``,
- which is what the reference states for this type: "If you specify a name,
- you cannot perform updates that require replacement of this resource, but
- you can perform other updates." Under a generated name there is nothing to
- refuse and the replacement is performed here. ``SubnetMappings`` is No
+ ``Name``, ``Scheme`` and ``Type`` require replacement, which
+ ``_update_resource`` performs before this runs. ``SubnetMappings`` is No
interruption as well and is modelled on neither path, here or in the
create.
"""
@@ -8267,15 +8267,6 @@ def _elbv2_load_balancer_update(physical_id, old_props, new_props, stack_name,
)
if replaced is not None:
return replaced
- if _requires_replacement("AWS::ElasticLoadBalancingV2::LoadBalancer",
- old_props, new_props):
- # Scheme and Type replace. A generated name does not refuse that, and
- # the new ARN carries a fresh id, so the predecessor is a resource of
- # its own and an UpdateReplacePolicy of Retain can keep it.
- created = _elbv2_load_balancer_create(
- logical_id or physical_id, new_props, stack_name)
- _delete_predecessor(_elbv2_load_balancer_delete, physical_id, old_props)
- return created
lb["IpAddressType"] = new_props.get("IpAddressType", "ipv4")
lb["Subnets"] = _elbv2_as_list(new_props.get("Subnets"))
@@ -8423,7 +8414,9 @@ def _elbv2_listener_update(physical_id, old_props, new_props, stack_name,
# ---------------------------------------------------------------------------
def _lambda_layer_create(logical_id, props, stack_name):
- layer_name = props.get("LayerName") or _physical_name(stack_name, logical_id, max_len=64)
+ # A replacement publishes the next version of the same layer.
+ layer_name = props.get("LayerName") or _physical_name(
+ stack_name, logical_id, max_len=64, seeded=False)
runtimes = props.get("CompatibleRuntimes", [])
architectures = props.get("CompatibleArchitectures", [])
@@ -8653,12 +8646,9 @@ def _sfn_state_machine_update(physical_id, old_props, new_props, stack_name,
its ARN (what Ref returns), its executions and its published versions.
StateMachineName and StateMachineType require replacement
(https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-stepfunctions-statemachine.html):
- a rename creates the new machine before the old one is removed; a type
- change under an unchanged name is refused — for a custom name by
- _custom_named_replacement_error, as CloudFormation refuses it, and here
- for a generated name, whose deterministic derivation cannot yield a
- fresh identity. Definition, DefinitionSubstitutions, RoleArn,
- LoggingConfiguration and Tags update without interruption;
+ ``_update_resource`` performs it before this runs. Definition,
+ DefinitionSubstitutions, RoleArn, LoggingConfiguration and Tags update
+ without interruption;
TracingConfiguration and EncryptionConfiguration are accepted without
effect, the store has no field for them.
"""
@@ -8674,15 +8664,6 @@ def _sfn_state_machine_update(physical_id, old_props, new_props, stack_name,
if replaced is not None:
return replaced
- old_type = old_props.get("StateMachineType", "STANDARD")
- new_type = new_props.get("StateMachineType", "STANDARD")
- if new_type != old_type:
- raise ValueError(
- f"AWS::StepFunctions::StateMachine StateMachineType ({old_type} -> "
- f"{new_type}) requires replacement, which MiniStack does not perform "
- f"for {name}; set a StateMachineName to create the replacement."
- )
-
data = {
"stateMachineArn": physical_id,
"definition": _sfn_definition(new_props),
@@ -8923,13 +8904,9 @@ def _elbv2_target_group_update(physical_id, old_props, new_props, stack_name,
No interruption" on the resource reference
(https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-elasticloadbalancingv2-targetgroup.html);
``Name``, ``Port``, ``Protocol``, ``ProtocolVersion``, ``TargetType``,
- ``VpcId`` and ``IpAddressType`` require replacement. A changed name
- replaces through the shared prologue; one of the others under an
- unchanged custom name is refused by ``_custom_named_replacement_error``,
- as CloudFormation refuses to replace a custom-named resource, and this
- name "must be unique per region per account". Under a generated name the
- replacement is performed here, and the registered targets go with the old
- group.
+ ``VpcId`` and ``IpAddressType`` require replacement, which
+ ``_update_resource`` performs before this runs; the registered targets
+ go with the old group.
"""
name = new_props.get("Name") or _physical_name(
stack_name, logical_id or physical_id, max_len=32)
@@ -8941,15 +8918,6 @@ def _elbv2_target_group_update(physical_id, old_props, new_props, stack_name,
)
if replaced is not None:
return replaced
- if _requires_replacement("AWS::ElasticLoadBalancingV2::TargetGroup",
- old_props, new_props):
- # Port, Protocol, ProtocolVersion, TargetType, VpcId and IpAddressType
- # replace; the targets registered in the old group stay with it, as on
- # AWS, and its ARN carries a fresh id, so Retain can keep it.
- created = _elbv2_target_group_create(
- logical_id or physical_id, new_props, stack_name)
- _delete_predecessor(_elbv2_target_group_delete, physical_id, old_props)
- return created
for prop, (field, default, cast) in _ELBV2_TG_HEALTH_CHECK.items():
value = new_props.get(prop, default)
@@ -10187,10 +10155,8 @@ def _waf_web_acl_update(physical_id, old_props, new_props, stack_name,
``VisibilityConfig`` are "Update requires: No interruption" on the
resource reference
(https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-wafv2-webacl.html);
- ``Name`` and ``Scope`` require replacement. A changed name replaces
- through the shared prologue; a ``Scope`` change under an unchanged
- explicit name is refused by ``_custom_named_replacement_error``, and under
- a generated name it is performed here. ``Tags`` are left alone on purpose:
+ ``Name`` and ``Scope`` require replacement, which ``_update_resource``
+ performs before this runs. ``Tags`` are left alone on purpose:
the reference states that "With AWS CloudFormation, you can only add tags
to AWS WAF resources during resource creation".
"""
@@ -10210,14 +10176,6 @@ def _waf_web_acl_update(physical_id, old_props, new_props, stack_name,
)
if replaced is not None:
return replaced
- if _requires_replacement("AWS::WAFv2::WebACL", old_props, new_props):
- # The create lands the ACL in the new scope's store, the delete
- # removes the record from the one it was created in, and the new id
- # is a fresh uuid, so Retain can keep the predecessor.
- created = _waf_web_acl_create(
- logical_id or physical_id, new_props, stack_name)
- _delete_predecessor(_waf_web_acl_delete, physical_id, old_props)
- return created
_waf.update_web_acl_record(acl, new_props)
return physical_id, {"Arn": acl["ARN"], "Id": acl_id}
@@ -11370,12 +11328,7 @@ def _ec_parameter_group_create(logical_id, props, stack_name):
def _ec_parameter_group_update(physical_id, old_props, new_props, stack_name, logical_id=None):
- if (physical_id not in _ec._param_groups
- or old_props.get("CacheParameterGroupFamily") != new_props.get("CacheParameterGroupFamily")):
- # CacheParameterGroupFamily is the type's create-only property. The
- # generated name does not change, so the old group goes first.
- if physical_id in _ec._param_groups:
- _ec_parameter_group_delete(physical_id, old_props)
+ if physical_id not in _ec._param_groups:
return _ec_parameter_group_create(logical_id or physical_id, new_props, stack_name)
old_values = {str(k): str(v) for k, v in (old_props.get("Properties") or {}).items()}
new_values = {str(k): str(v) for k, v in (new_props.get("Properties") or {}).items()}
@@ -12042,12 +11995,6 @@ def _glue_trigger_update(physical_id, old_props, new_props, stack_name, logical_
_glue_trigger_create, _glue_trigger_delete)
if replaced:
return replaced
- if _requires_replacement("AWS::Glue::Trigger", old_props, new_props):
- # Type and WorkflowName are create-only. An explicit Name is refused
- # above the handler; a generated one is taken back by the replacement,
- # so the old trigger goes first and there is nothing left to retain.
- _glue_trigger_delete(physical_id, old_props)
- return _glue_trigger_create(logical_id or physical_id, new_props, stack_name)
trigger_update = {k: new_props[k] for k in ("Actions", *_GLUE_TRIGGER_DEFAULTS)
if k in new_props}
trigger_update.update(_glue_reset_dropped(old_props, new_props, _GLUE_TRIGGER_DEFAULTS))
@@ -12173,23 +12120,12 @@ def _iot_thing_group_create(logical_id, props, stack_name):
def _iot_thing_group_update(physical_id, old_props, new_props, stack_name, logical_id=None):
"""Update a thing group in place through UpdateThingGroup: ThingGroupProperties
is the one property the reference marks No interruption that the service
- stores. ThingGroupName and ParentGroupName require replacement
- (https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-iot-thinggroup.html):
- the new group is created before the old one is removed, as CloudFormation
- orders a replacement, and Ref follows the new id. QueryString and Tags are
- accepted without effect — the service has no dynamic groups and no tag
- store for thing groups. An auto-named group takes its deterministic name
- back on a replacement, so the predecessor comes off first or
- CreateThingGroup answers ResourceAlreadyExistsException; same shape as the
- DynamoDB key-schema and Location tracker branches."""
+ stores. ThingGroupName and ParentGroupName changes are replaced in
+ ``_update_resource`` before this runs. QueryString and Tags are accepted
+ without effect — the service has no dynamic groups and no tag store for
+ thing groups."""
rec = _iot_thing_group_record(physical_id)
- replacement = rec is None or any(
- new_props.get(key) != old_props.get(key)
- for key in ("ThingGroupName", "ParentGroupName")
- )
- if replacement:
- if rec is not None and not new_props.get("ThingGroupName"):
- _iot_thing_group_delete(physical_id, old_props)
+ if rec is None:
return _iot_thing_group_create(logical_id or physical_id, new_props, stack_name)
name = rec["thingGroupName"]
resp = _iot._update_thing_group(
@@ -12342,20 +12278,12 @@ def _iot_provisioning_template_update(physical_id, old_props, new_props, stack_n
an UpdateProvisioningTemplate member (AWS stores it as a new version via
CreateProvisioningTemplateVersion, which MiniStack does not model), so a
changed body is written onto the stored record directly — the template
- always stays at defaultVersionId 1. TemplateType requires replacement: under
- an explicit name _custom_named_replacement_error refuses it as CloudFormation
- does, and under a generated name the deterministic value is reused, so the
- predecessor comes off first.
+ always stays at defaultVersionId 1. TemplateType requires replacement,
+ which ``_update_resource`` performs before this runs.
"""
name = new_props.get("TemplateName") or _physical_name(
stack_name, logical_id or physical_id, max_len=36
)
- if (old_props.get("TemplateType", "FLEET_PROVISIONING")
- != new_props.get("TemplateType", "FLEET_PROVISIONING")):
- _iot_provisioning_template_delete(physical_id, old_props)
- return _iot_provisioning_template_create(
- logical_id or physical_id, new_props, stack_name
- )
if name != physical_id:
created = _iot_provisioning_template_create(
logical_id or physical_id, new_props, stack_name
@@ -12800,12 +12728,7 @@ def _location_tracker_update(physical_id, old_props, new_props, stack_name,
Tags change is reconciled on the record. TrackerName and KmsKeyId require
replacement
(https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-location-tracker.html):
- a renamed tracker is created before the old one, with its device
- positions, is removed; a KmsKeyId change re-creates an auto-named
- tracker under its deterministic physical name (the name is reused, so
- the predecessor cannot be retained, as for a DynamoDB table), and with
- an explicit, unchanged TrackerName it is refused by the
- _CUSTOM_NAME_REPLACEMENT rule."""
+ ``_update_resource`` performs it before this runs."""
import ministack.services.location as _location
name = new_props.get("TrackerName") or _physical_name(
stack_name, logical_id or physical_id, max_len=100
@@ -12818,12 +12741,6 @@ def _location_tracker_update(physical_id, old_props, new_props, stack_name,
)
if replaced is not None:
return replaced
- if old_props.get("KmsKeyId") != new_props.get("KmsKeyId"):
- # Not routed through _delete_predecessor, like the DynamoDB key-schema
- # branch: the auto-generated name is deterministic, so the replacement
- # takes it back and retaining the predecessor is not possible here.
- _location_tracker_delete(physical_id, old_props)
- return _location_tracker_create(logical_id or physical_id, new_props, stack_name)
changes = {}
for prop, default in _LOCATION_TRACKER_UPDATABLE.items():
if prop in new_props:
@@ -12845,14 +12762,14 @@ def _location_tracker_delete(physical_id, props):
_location._delete_tracker(physical_id)
-# CloudFormation reporting rules, checked against DescribeType and change sets.
-# A row lists the schema's createOnlyProperties (Always); the conditional table
-# below lists its conditionalCreateOnlyProperties (Conditionally). Any other
-# property of a listed type is in place (Never), as AWS reports it. Service API
-# immutability is different: an update may fail without being reported as a
-# replacement (for example Cognito sign-in attributes). Keep the execution
-# predicates separate until their behavior has been reconciled. Types without
-# a row keep the conservative Conditionally answer.
+# CloudFormation replacement rules, checked against DescribeType and change
+# sets. A row lists the schema's createOnlyProperties (Always); the conditional
+# table below lists its conditionalCreateOnlyProperties (Conditionally). Any
+# other property of a listed type is in place (Never), as AWS reports it. A
+# stack update replaces the resource when an Always property changes. Service
+# API immutability is different: an update may fail without being reported as
+# a replacement (for example Cognito sign-in attributes). Types without a row
+# keep the conservative Conditionally answer and their handler's behavior.
_REPLACING_PROPERTIES: dict[str, tuple[str, ...]] = {
"AWS::DynamoDB::Table": ("TableName", "ImportSourceSpecification"),
"AWS::StepFunctions::StateMachine": ("StateMachineName", "StateMachineType"),
@@ -12938,6 +12855,13 @@ def _property_recreation(resource_type: str, name: str) -> str:
return "Never"
+def _replacing_change(resource_type, old_props, new_props) -> bool:
+ """Whether a changed property is one DescribeChangeSet reports as Always."""
+ return any(_property_recreation(resource_type, name) == "Always"
+ for name in old_props.keys() | new_props.keys()
+ if old_props.get(name) != new_props.get(name))
+
+
_RESOURCE_HANDLERS = {
"AWS::OpenSearchService::Domain": {
"create": _opensearch_domain_create,
diff --git a/ministack/services/cloudformation/stacks.py b/ministack/services/cloudformation/stacks.py
index a0992def..556edb62 100644
--- a/ministack/services/cloudformation/stacks.py
+++ b/ministack/services/cloudformation/stacks.py
@@ -24,12 +24,14 @@ from .engine import (
)
from .provisioners import (
_DEFERRED_PREDECESSOR_DELETES,
+ _NAME_SEED,
_RETAIN_REPLACED,
_RETAINING_POLICIES,
_custom_named_replacement_error,
_delete_resource,
_property_recreation,
_provision_resource,
+ _replacing_change,
_snapshot_resource,
_tag_map,
_update_resource,
@@ -85,6 +87,11 @@ def _runs_on_worker_thread(resource_type: str) -> bool:
"AWS::ElastiCache::ReplicationGroup"))
+def _update_keeping_seed(*args):
+ """Return _update_resource's result and the name seed it left, which a worker thread would lose."""
+ return _update_resource(*args), _NAME_SEED.get()
+
+
# ===========================================================================
# Stack Events helper
# ===========================================================================
@@ -240,6 +247,7 @@ async def _revert_update(stack_id, stack_name, logical_id, rtype, physical_id,
type's update handler; ``record`` takes the attributes it answers."""
_add_event(stack_id, stack_name, logical_id, rtype, "UPDATE_IN_PROGRESS",
physical_id=physical_id)
+ seed_token = _NAME_SEED.set((record or {}).get("_name_seed", ""))
try:
if _runs_on_worker_thread(rtype):
new_pid, new_attrs = await run_reentrant(
@@ -254,6 +262,8 @@ async def _revert_update(stack_id, stack_name, logical_id, rtype, physical_id,
_add_event(stack_id, stack_name, logical_id, rtype, "UPDATE_FAILED",
str(exc), physical_id)
raise
+ finally:
+ _NAME_SEED.reset(seed_token)
if record is not None:
if new_pid != physical_id:
logger.warning("Rollback update of %s moved it from %s to %s",
@@ -607,6 +617,7 @@ async def _deploy_stack_async(stack_name: str, stack_id: str, template: dict,
resource_type = res_def.get("Type", "AWS::CloudFormation::CustomResource")
raw_props = res_def.get("Properties", {})
update_attempt = None
+ name_seed = ""
try:
# Resolve properties
@@ -674,22 +685,29 @@ async def _deploy_stack_async(stack_name: str, stack_id: str, template: dict,
stack_name, stack_id, logical_id)
pending_deletes = []
deferred_token = _DEFERRED_PREDECESSOR_DELETES.set(pending_deletes)
- # A custom-named refusal never reaches the handler: nothing to send back.
- if not _custom_named_replacement_error(
- resource_type, old_tagged, new_tagged):
+ seed_token = _NAME_SEED.set(prev_resource.get("_name_seed", ""))
+ # A refusal or a replacement leaves the resource as it was: nothing to send back.
+ refused = _custom_named_replacement_error(resource_type, old_tagged, new_tagged)
+ if not refused and _replacing_change(resource_type, old_tagged, new_tagged):
+ _add_event(stack_id, stack_name, logical_id, resource_type,
+ "UPDATE_IN_PROGRESS",
+ "Requested update requires the creation of a new "
+ "physical resource; hence creating one.", old_pid)
+ elif not refused:
update_attempt = (old_pid, new_tagged, old_tagged, old_attrs)
try:
if _runs_on_worker_thread(resource_type):
- physical_id, attrs = await run_reentrant(
- _update_resource, resource_type, old_pid, old_tagged,
+ (physical_id, attrs), name_seed = await run_reentrant(
+ _update_keeping_seed, resource_type, old_pid, old_tagged,
new_tagged, stack_name, logical_id, old_attrs
)
else:
- physical_id, attrs = _update_resource(
+ (physical_id, attrs), name_seed = _update_keeping_seed(
resource_type, old_pid, old_tagged, new_tagged,
stack_name, logical_id, old_attrs
)
finally:
+ _NAME_SEED.reset(seed_token)
_DEFERRED_PREDECESSOR_DELETES.reset(deferred_token)
_RETAIN_REPLACED.reset(token)
if physical_id != old_pid or pending_deletes:
@@ -742,6 +760,8 @@ async def _deploy_stack_async(stack_name: str, stack_id: str, template: dict,
}
if dynamic_values:
provisioned_resources[logical_id]["_dynamic"] = dynamic_values
+ if name_seed:
+ provisioned_resources[logical_id]["_name_seed"] = name_seed
created_in_this_run.append(logical_id)
_add_event(stack_id, stack_name, logical_id, resource_type,
diff --git a/tests/test_cfn.py b/tests/test_cfn.py
index 7987e6c6..652f9cdc 100644
--- a/tests/test_cfn.py
+++ b/tests/test_cfn.py
@@ -9243,7 +9243,7 @@ def test_cfn_location_tracker_generated_name(cfn, location):
PositionFiltering, EventBridgeEnabled and KmsKeyEnableGeospatialQueries
update in place and revert to what a fresh create has (the last one
absent) when the template drops them; a KmsKeyId change replaces the
- auto-named tracker under the same name, so its positions are gone."""
+ auto-named tracker under a new generated name, without its positions."""
stack_name = f"cfn-loc-gen-{_uuid_mod.uuid4().hex[:8]}"
def template(props):
@@ -9296,16 +9296,16 @@ def test_cfn_location_tracker_generated_name(cfn, location):
Updates=[{"DeviceId": "veh-1", "Position": [1.0, 1.0],
"SampleTime": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())}],
)
- # KmsKeyId requires replacement: the auto-named tracker is re-created
- # under the same physical name, without its positions.
cfn.update_stack(StackName=stack_name,
TemplateBody=template({"KmsKeyId": "alias/probe"}))
stack = _wait_stack(cfn, stack_name)
assert stack["StackStatus"] == "UPDATE_COMPLETE", stack.get("StackStatusReason")
- assert _output(stack, "Name") == name
- assert location.describe_tracker(TrackerName=name)["KmsKeyId"] == "alias/probe"
+ new_name = _output(stack, "Name")
+ assert new_name != name and new_name.startswith(f"{stack_name}-Tracker-")
+ assert location.describe_tracker(TrackerName=new_name)["KmsKeyId"] == "alias/probe"
with pytest.raises(ClientError):
- location.get_device_position(TrackerName=name, DeviceId="veh-1")
+ location.get_device_position(TrackerName=new_name, DeviceId="veh-1")
+ assert name not in [t["TrackerName"] for t in location.list_trackers()["Entries"]]
finally:
_delete_cfn_test_stack(cfn, stack_name)
@@ -20613,10 +20613,9 @@ def test_cfn_cognito_user_pool_group_dropped_properties_revert(cfn, cognito_idp)
def test_cfn_cognito_user_pool_group_under_generated_name_moves_and_renames(cfn, cognito_idp):
"""A group without a GroupName carries a generated name, which
CloudFormation may replace: a UserPoolId change moves it to the other
- pool (same generated name, so Ref is unchanged, and the old pool is
- left without it), and declaring a GroupName afterwards replaces it once
- more with Ref following the new name. The pools live outside the
- stack."""
+ pool under a new generated name (the old pool is left without it), and
+ declaring a GroupName afterwards replaces it once more with Ref
+ following the new name. The pools live outside the stack."""
uid = _uuid_mod.uuid4().hex[:8]
stack_name = f"cfn-cog-group-gen-{uid}"
pool_a = cognito_idp.create_user_pool(PoolName=f"cfn-group-gen-a-{uid}")["UserPool"]["Id"]
@@ -20644,9 +20643,10 @@ def test_cfn_cognito_user_pool_group_under_generated_name_moves_and_renames(cfn,
cfn.update_stack(StackName=stack_name, TemplateBody=template(pool_b))
stack = _wait_stack(cfn, stack_name)
assert stack["StackStatus"] == "UPDATE_COMPLETE", stack.get("StackStatusReason")
- assert _output(stack, "GroupRef") == generated
+ moved = _output(stack, "GroupRef")
+ assert moved != generated and moved.startswith(f"{stack_name}-Group-")
assert group_names(pool_a) == []
- assert group_names(pool_b) == [generated]
+ assert group_names(pool_b) == [moved]
cfn.update_stack(StackName=stack_name, TemplateBody=template(pool_b, "admins"))
stack = _wait_stack(cfn, stack_name)
@@ -21830,47 +21830,101 @@ def test_cfn_state_machine_type_change_under_custom_name_fails_loudly(cfn, sfn):
stack = _wait_stack(cfn, stack_name)
assert stack["StackStatus"] == "UPDATE_ROLLBACK_COMPLETE"
reasons = _stack_event_reasons(cfn, stack_name)
- assert "custom-named resource requires replacing" in reasons
+ assert f"requires replacing. Rename {arn} and update the stack again." in reasons
assert sfn.describe_state_machine(stateMachineArn=arn)["type"] == "STANDARD"
finally:
_delete_cfn_test_stack(cfn, stack_name)
-def test_cfn_state_machine_type_change_under_generated_name_fails_loudly(cfn, sfn):
- """Under a generated name the deterministic derivation cannot yield a
- fresh identity for the replacement, so MiniStack fails the update
- naming the property instead of rebuilding the machine in place: the
- stack rolls back and the machine keeps its ARN and type."""
- uid = _uuid_mod.uuid4().hex[:8]
- stack_name = f"cfn-sfn-gentype-{uid}"
-
- def template(sm_type):
- return json.dumps({
- "Resources": {"SM": {"Type": "AWS::StepFunctions::StateMachine", "Properties": {
+def _sfn_replacement_template(sm_type, comment="generated", fail=False):
+ """A generated-name machine, a parameter that holds its ARN and, with
+ ``fail``, a resource after both that fails."""
+ template = {
+ "Resources": {
+ "SM": {"Type": "AWS::StepFunctions::StateMachine", "Properties": {
"StateMachineType": sm_type,
- "DefinitionString": _sfn_definition_json("generated"),
+ "DefinitionString": _sfn_definition_json(comment),
"RoleArn": "arn:aws:iam::000000000000:role/sfn-role",
- }}},
- "Outputs": {"Arn": {"Value": {"Ref": "SM"}}},
- })
+ }},
+ "Param": {"Type": "AWS::SSM::Parameter", "Properties": {
+ "Type": "String", "Value": {"Ref": "SM"}}},
+ },
+ "Outputs": {"Arn": {"Value": {"Ref": "SM"}},
+ "Param": {"Value": {"Ref": "Param"}}},
+ }
+ body = json.dumps(template)
+ return _cfn_with_failing_resource(body, "Param") if fail else body
- cfn.create_stack(StackName=stack_name, TemplateBody=template("STANDARD"))
+
+def test_cfn_state_machine_type_change_under_generated_name_replaces_it(cfn, sfn, ssm):
+ """Under a generated name a StateMachineType change replaces the machine:
+ the new one has a new name and ARN, the parameter that references it is
+ updated, the old one is deleted after the update, and a later in-place
+ update stays on the new machine."""
+ stack_name = f"cfn-sfn-gentype-{_uuid_mod.uuid4().hex[:8]}"
+ cfn.create_stack(StackName=stack_name, TemplateBody=_sfn_replacement_template("STANDARD"))
try:
stack = _wait_stack(cfn, stack_name)
assert stack["StackStatus"] == "CREATE_COMPLETE", stack.get("StackStatusReason")
- arn = _output(stack, "Arn")
+ old_arn = _output(stack, "Arn")
- cfn.update_stack(StackName=stack_name, TemplateBody=template("EXPRESS"))
+ cfn.update_stack(StackName=stack_name, TemplateBody=_sfn_replacement_template("EXPRESS"))
stack = _wait_stack(cfn, stack_name)
- assert stack["StackStatus"] == "UPDATE_ROLLBACK_COMPLETE", stack.get("StackStatusReason")
- reasons = _stack_event_reasons(cfn, stack_name)
- assert "StateMachineType (STANDARD -> EXPRESS) requires replacement" in reasons
- assert _output(stack, "Arn") == arn
- assert sfn.describe_state_machine(stateMachineArn=arn)["type"] == "STANDARD"
+ assert stack["StackStatus"] == "UPDATE_COMPLETE", stack.get("StackStatusReason")
+ new_arn = _output(stack, "Arn")
+ assert new_arn != old_arn
+ described = sfn.describe_state_machine(stateMachineArn=new_arn)
+ assert described["type"] == "EXPRESS"
+ assert described["name"].startswith(f"{stack_name}-SM-")
+ assert _ssm_value(ssm, _output(stack, "Param")) == new_arn
+ with pytest.raises(ClientError):
+ sfn.describe_state_machine(stateMachineArn=old_arn)
+ assert ("Requested update requires the creation of a new physical resource; "
+ "hence creating one.") in _stack_event_reasons(cfn, stack_name)
+
+ cfn.update_stack(StackName=stack_name,
+ TemplateBody=_sfn_replacement_template("EXPRESS", "in place"))
+ stack = _wait_stack(cfn, stack_name)
+ assert stack["StackStatus"] == "UPDATE_COMPLETE", stack.get("StackStatusReason")
+ assert _output(stack, "Arn") == new_arn
+ described = sfn.describe_state_machine(stateMachineArn=new_arn)
+ assert json.loads(described["definition"])["Comment"] == "in place"
finally:
_delete_cfn_test_stack(cfn, stack_name)
+def test_cfn_lambda_function_tenancy_change_under_custom_name_is_refused(cfn, lam):
+ """TenancyConfig requires replacement, which CloudFormation refuses for a
+ function with an explicit, unchanged FunctionName: the stack rolls back
+ and the function keeps its configuration."""
+ name = f"cfn-fn-tenancy-{_uuid_mod.uuid4().hex[:8]}"
+
+ def template(tenancy):
+ props = {
+ "FunctionName": name, "Runtime": "python3.12", "Handler": "index.handler",
+ "Role": "arn:aws:iam::000000000000:role/lambda-role",
+ "Code": {"ZipFile": "def handler(e, c):\n return {}\n"},
+ }
+ if tenancy:
+ props["TenancyConfig"] = {"TenantIsolationMode": "PER_TENANT"}
+ return json.dumps({"Resources": {"Fn": {"Type": "AWS::Lambda::Function",
+ "Properties": props}}})
+
+ cfn.create_stack(StackName=name, TemplateBody=template(False))
+ try:
+ stack = _wait_stack(cfn, name)
+ assert stack["StackStatus"] == "CREATE_COMPLETE", stack.get("StackStatusReason")
+
+ cfn.update_stack(StackName=name, TemplateBody=template(True))
+ stack = _wait_stack(cfn, name)
+ assert stack["StackStatus"] == "UPDATE_ROLLBACK_COMPLETE", stack.get("StackStatusReason")
+ assert (f"requires replacing. Rename {name} and update the stack again."
+ in _stack_event_reasons(cfn, name))
+ assert "TenancyConfig" not in lam.get_function_configuration(FunctionName=name)
+ finally:
+ _delete_cfn_test_stack(cfn, name)
+
+
def test_cfn_state_machine_rename_replaces_the_machine(cfn, sfn):
"""StateMachineName requires replacement: the renamed machine is created
before the old one is removed, Ref follows the new ARN, and a logging
@@ -21909,6 +21963,131 @@ def test_cfn_state_machine_rename_replaces_the_machine(cfn, sfn):
_delete_cfn_test_stack(cfn, stack_name)
+def test_cfn_state_machine_replacement_rolled_back_deletes_only_the_new_machine(cfn, sfn, ssm):
+ """A replacement whose update fails later is rolled back: the new machine
+ is deleted, the old one and the parameter's value stay."""
+ stack_name = f"cfn-sfn-genrb-{_uuid_mod.uuid4().hex[:8]}"
+ cfn.create_stack(StackName=stack_name, TemplateBody=_sfn_replacement_template("STANDARD"))
+ try:
+ stack = _wait_stack(cfn, stack_name)
+ assert stack["StackStatus"] == "CREATE_COMPLETE", stack.get("StackStatusReason")
+ old_arn = _output(stack, "Arn")
+
+ cfn.update_stack(StackName=stack_name,
+ TemplateBody=_sfn_replacement_template("EXPRESS", fail=True))
+ stack = _wait_stack(cfn, stack_name)
+ assert stack["StackStatus"] == "UPDATE_ROLLBACK_COMPLETE", stack.get("StackStatusReason")
+ assert "failed to create: [Bad]" in _stack_event_reasons(cfn, stack_name)
+ assert _output(stack, "Arn") == old_arn
+ assert sfn.describe_state_machine(stateMachineArn=old_arn)["type"] == "STANDARD"
+ assert _ssm_value(ssm, _output(stack, "Param")) == old_arn
+ machines = [m["name"] for m in _all_pages(sfn, "list_state_machines", "stateMachines")
+ if m["name"].startswith(f"{stack_name}-SM-")]
+ assert machines == [old_arn.rsplit(":", 1)[1]]
+ finally:
+ _delete_cfn_test_stack(cfn, stack_name)
+
+
+def test_cfn_sqs_fifo_change_under_custom_name_keeps_the_queue(cfn, sqs):
+ """FifoQueue requires replacement, and adding it, even as false, is a
+ change. Under an explicit, unchanged QueueName the create of the new
+ queue fails because the name is taken: the stack rolls back and the
+ queue keeps its messages."""
+ name = f"cfn-q-fifo-{_uuid_mod.uuid4().hex[:8]}"
+
+ def template(fifo):
+ props = {"QueueName": name}
+ if fifo:
+ props["FifoQueue"] = False
+ return json.dumps({"Resources": {"Q": {"Type": "AWS::SQS::Queue", "Properties": props}}})
+
+ cfn.create_stack(StackName=name, TemplateBody=template(False))
+ try:
+ stack = _wait_stack(cfn, name)
+ assert stack["StackStatus"] == "CREATE_COMPLETE", stack.get("StackStatusReason")
+ url = sqs.get_queue_url(QueueName=name)["QueueUrl"]
+ sqs.send_message(QueueUrl=url, MessageBody="kept")
+
+ cfn.update_stack(StackName=name, TemplateBody=template(True))
+ stack = _wait_stack(cfn, name)
+ assert stack["StackStatus"] == "UPDATE_ROLLBACK_COMPLETE", stack.get("StackStatusReason")
+ reasons = _stack_event_reasons(cfn, name)
+ assert "Queue creation failed because the queue already exists" in reasons
+ assert ("Requested update requires the creation of a new physical resource; "
+ "hence creating one.") in reasons
+ attrs = sqs.get_queue_attributes(QueueUrl=url, AttributeNames=["All"])["Attributes"]
+ assert attrs["ApproximateNumberOfMessages"] == "1"
+ assert "FifoQueue" not in attrs
+ finally:
+ _delete_cfn_test_stack(cfn, name)
+
+
+def test_cfn_sns_fifo_change_under_custom_name_keeps_the_topic(cfn, sns):
+ """A FifoTopic change under an explicit, unchanged TopicName fails because
+ the name is taken: the stack rolls back and the topic keeps its
+ subscriptions."""
+ name = f"cfn-t-fifo-{_uuid_mod.uuid4().hex[:8]}"
+
+ def template(fifo):
+ props = {"TopicName": name}
+ if fifo:
+ props["FifoTopic"] = False
+ return json.dumps({"Resources": {"T": {"Type": "AWS::SNS::Topic", "Properties": props}},
+ "Outputs": {"Arn": {"Value": {"Ref": "T"}}}})
+
+ cfn.create_stack(StackName=name, TemplateBody=template(False))
+ try:
+ stack = _wait_stack(cfn, name)
+ assert stack["StackStatus"] == "CREATE_COMPLETE", stack.get("StackStatusReason")
+ arn = _output(stack, "Arn")
+ sns.subscribe(TopicArn=arn, Protocol="email", Endpoint="[email protected]")
+
+ cfn.update_stack(StackName=name, TemplateBody=template(True))
+ stack = _wait_stack(cfn, name)
+ assert stack["StackStatus"] == "UPDATE_ROLLBACK_COMPLETE", stack.get("StackStatusReason")
+ assert ("Topic creation failed because the topic already exists"
+ in _stack_event_reasons(cfn, name))
+ subs = sns.list_subscriptions_by_topic(TopicArn=arn)["Subscriptions"]
+ assert [s["Endpoint"] for s in subs] == ["[email protected]"]
+ finally:
+ _delete_cfn_test_stack(cfn, name)
+
+
+def test_cfn_layer_version_replacement_publishes_the_next_version(cfn, s3, lam):
+ """A LayerVersion without a LayerName is replaced by the next version of
+ the same layer, and the old version is deleted."""
+ stack_name = f"cfn-layer-next-{_uuid_mod.uuid4().hex[:8]}"
+ s3.create_bucket(Bucket=stack_name)
+ buf = io.BytesIO()
+ with zipfile.ZipFile(buf, "w") as z:
+ z.writestr("python/layer_mod.py", "X = 1\n")
+ s3.put_object(Bucket=stack_name, Key="layer.zip", Body=buf.getvalue())
+
+ def template(description):
+ return json.dumps({"Resources": {"Layer": {
+ "Type": "AWS::Lambda::LayerVersion", "Properties": {
+ "Description": description,
+ "Content": {"S3Bucket": stack_name, "S3Key": "layer.zip"}}}},
+ "Outputs": {"Arn": {"Value": {"Ref": "Layer"}}}})
+
+ cfn.create_stack(StackName=stack_name, TemplateBody=template("one"))
+ try:
+ stack = _wait_stack(cfn, stack_name)
+ assert stack["StackStatus"] == "CREATE_COMPLETE", stack.get("StackStatusReason")
+ layer_arn, version = _output(stack, "Arn").rsplit(":", 1)
+
+ cfn.update_stack(StackName=stack_name, TemplateBody=template("two"))
+ stack = _wait_stack(cfn, stack_name)
+ assert stack["StackStatus"] == "UPDATE_COMPLETE", stack.get("StackStatusReason")
+ assert _output(stack, "Arn") == f"{layer_arn}:{int(version) + 1}"
+ versions = lam.list_layer_versions(LayerName=layer_arn.rsplit(":", 1)[1])["LayerVersions"]
+ assert [(v["Version"], v["Description"]) for v in versions] == [(int(version) + 1, "two")]
+ finally:
+ _delete_cfn_test_stack(cfn, stack_name)
+ s3.delete_object(Bucket=stack_name, Key="layer.zip")
+ s3.delete_bucket(Bucket=stack_name)
+
+
def test_cfn_state_machine_update_keeps_an_undeclared_logging_configuration(cfn, sfn):
"""A property the template never declared is not the stack's to reset: a
LoggingConfiguration set through UpdateStateMachine outside the stack
@@ -23399,9 +23578,9 @@ def test_cfn_iam_instance_profile_path_change_under_custom_name_fails_loudly(cfn
def test_cfn_iam_instance_profile_path_change_under_generated_name_replaces_it(cfn, iam):
- """Under a generated name a Path change is a replacement: the profile
- is re-created under the new path, so its ARN changes and its role
- comes along."""
+ """Under a generated name a Path change is a replacement: a profile
+ with a new generated name is created under the new path, its role comes
+ along, and the old profile is deleted."""
uid = _uuid_mod.uuid4().hex[:8]
stack_name = f"cfn-iam-ip-gen-{uid}"
@@ -23422,12 +23601,14 @@ def test_cfn_iam_instance_profile_path_change_under_generated_name_replaces_it(c
stack = _wait_stack(cfn, stack_name)
assert stack["StackStatus"] == "UPDATE_COMPLETE", stack.get("StackStatusReason")
new_arn = _output(stack, "ProfileArn")
- assert new_arn != old_arn
- assert new_arn.endswith(f":instance-profile/moved/{name}")
- profile = iam.get_instance_profile(InstanceProfileName=name)["InstanceProfile"]
+ new_name = new_arn.rsplit("/", 1)[1]
+ assert new_name != name and new_arn.endswith(f":instance-profile/moved/{new_name}")
+ profile = iam.get_instance_profile(InstanceProfileName=new_name)["InstanceProfile"]
assert profile["Arn"] == new_arn
assert profile["Path"] == "/moved/"
assert [r["RoleName"] for r in profile["Roles"]] == [f"cfn-ip-a-{uid}"]
+ with pytest.raises(ClientError):
+ iam.get_instance_profile(InstanceProfileName=name)
finally:
_delete_cfn_test_stack(cfn, stack_name)
@@ -31795,7 +31976,7 @@ def test_cfn_elasticache_generated_cluster_replaced_on_create_only_change(cfn, e
s = _uuid_mod.uuid4().hex[:8]
stack = f"Ec-Gen-{s}"
- def template(subnet_ref):
+ def template(subnet_ref, retention=0):
return {
"Resources": {
"A": {"Type": "AWS::ElastiCache::SubnetGroup", "Properties": {
@@ -31804,7 +31985,7 @@ def test_cfn_elasticache_generated_cluster_replaced_on_create_only_change(cfn, e
"Description": "b", "SubnetIds": ["subnet-b"]}},
"C": {"Type": "AWS::ElastiCache::CacheCluster", "Properties": {
"Engine": "redis", "CacheNodeType": "cache.t3.micro", "NumCacheNodes": 1,
- "CacheSubnetGroupName": {"Ref": subnet_ref}}},
+ "CacheSubnetGroupName": {"Ref": subnet_ref}, "SnapshotRetentionLimit": retention}},
},
"Outputs": {k: {"Value": {"Ref": k}} for k in ("A", "B", "C")},
}
@@ -31816,14 +31997,21 @@ def test_cfn_elasticache_generated_cluster_replaced_on_create_only_change(cfn, e
# cluster name fits the 50-character limit.
assert out["A"] == out["A"].lower() and out["A"].startswith(f"ec-gen-{s}-a-")
assert out["C"] == out["C"].lower() and len(out["C"]) <= 50
- first = ec.describe_cache_clusters(CacheClusterId=out["C"])["CacheClusters"][0]
- # CacheSubnetGroupName is create-only: the cluster is replaced and
- # takes its generated name back.
+ # CacheSubnetGroupName is create-only: the cluster is replaced under a
+ # new generated name and the old one is deleted.
_ec_update(cfn, stack, template("B"))
- assert _ec_outputs(cfn, stack)["C"] == out["C"]
- second = ec.describe_cache_clusters(CacheClusterId=out["C"])["CacheClusters"][0]
+ replaced = _ec_outputs(cfn, stack)["C"]
+ assert replaced != out["C"] and replaced.startswith(f"ec-gen-{s}-c-") and len(replaced) <= 50
+ second = ec.describe_cache_clusters(CacheClusterId=replaced)["CacheClusters"][0]
assert second["CacheSubnetGroupName"] == out["B"]
- assert second["CacheClusterCreateTime"] >= first["CacheClusterCreateTime"]
+ assert _ec_error_code(lambda: ec.describe_cache_clusters(
+ CacheClusterId=out["C"])) == "CacheClusterNotFound"
+ # SnapshotRetentionLimit updates in place: the replaced cluster stays.
+ _ec_update(cfn, stack, template("B", 1))
+ assert _ec_outputs(cfn, stack)["C"] == replaced
+ third = ec.describe_cache_clusters(CacheClusterId=replaced)["CacheClusters"][0]
+ assert third["SnapshotRetentionLimit"] == 1
+ assert third["CacheClusterCreateTime"] == second["CacheClusterCreateTime"]
finally:
_delete_cfn_test_stack(cfn, stack)