Skip to content
This repository was archived by the owner on Mar 23, 2026. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -395,8 +395,8 @@ class NodeResource(ChangeSetNode):
condition_reference: Final[Maybe[TerminalValue]]
depends_on: Final[Maybe[NodeDependsOn]]
requires_replacement: Final[bool]
deletion_policy: Final[Maybe[ChangeSetTerminal]]
update_replace_policy: Final[Maybe[ChangeSetTerminal]]
deletion_policy: Final[Maybe[ChangeSetEntity]]
update_replace_policy: Final[Maybe[ChangeSetEntity]]
fn_transform: Final[Maybe[NodeIntrinsicFunctionFnTransform]]

def __init__(
Expand All @@ -409,8 +409,8 @@ def __init__(
condition_reference: Maybe[TerminalValue],
depends_on: Maybe[NodeDependsOn],
requires_replacement: bool,
deletion_policy: Maybe[ChangeSetTerminal],
update_replace_policy: Maybe[ChangeSetTerminal],
deletion_policy: Maybe[ChangeSetEntity],
update_replace_policy: Maybe[ChangeSetEntity],
fn_transform: Maybe[NodeIntrinsicFunctionFnTransform],
):
super().__init__(scope=scope, change_type=change_type)
Expand Down Expand Up @@ -1043,26 +1043,20 @@ def _visit_type(self, scope: Scope, before_type: Any, after_type: Any) -> Termin

def _visit_deletion_policy(
self, scope: Scope, before_deletion_policy: Any, after_deletion_policy: Any
) -> TerminalValue:
) -> ChangeSetEntity:
value = self._visit_value(
scope=scope, before_value=before_deletion_policy, after_value=after_deletion_policy
)
if not isinstance(value, TerminalValue):
# TODO: decide where template schema validation should occur.
raise RuntimeError()
return value

def _visit_update_replace_policy(
self, scope: Scope, before_update_replace_policy: Any, after_deletion_policy: Any
) -> TerminalValue:
) -> ChangeSetEntity:
value = self._visit_value(
scope=scope,
before_value=before_update_replace_policy,
after_value=after_deletion_policy,
)
if not isinstance(value, TerminalValue):
# TODO: decide where template schema validation should occur.
raise RuntimeError()
return value

def _visit_resource(
Expand Down
166 changes: 166 additions & 0 deletions tests/aws/services/cloudformation/engine/test_conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,172 @@ def test_references_to_disabled_resources(
)
snapshot.match("resources-description", describe_resources)

@markers.aws.validated
@pytest.mark.parametrize(
"environment,expected_policy",
[
("dev", "Delete"),
("prod", "Retain"),
],
)
@markers.snapshot.skip_snapshot_verify(paths=["$..LastOperations"])
def test_deletion_policy_with_conditional(
self, deploy_cfn_template, aws_client, snapshot, environment, expected_policy
):
snapshot.add_transformer(snapshot.transform.cloudformation_api())
snapshot.add_transformer(snapshot.transform.key_value("TopicArn"))

topic_name = f"test-topic-{environment}-{short_uid()}"
snapshot.add_transformer(
snapshot.transform.regex(topic_name, f"<topic-name-{environment}>")
)

template = {
"AWSTemplateFormatVersion": "2010-09-09",
"Parameters": {
"Environment": {
"Type": "String",
"Default": "dev",
"AllowedValues": ["dev", "prod"],
},
"TopicName": {
"Type": "String",
},
},
"Conditions": {
"IsProduction": {"Fn::Equals": [{"Ref": "Environment"}, "prod"]},
},
"Resources": {
"MyTopic": {
"Type": "AWS::SNS::Topic",
"DeletionPolicy": {"Fn::If": ["IsProduction", "Retain", "Delete"]},
"UpdateReplacePolicy": {"Fn::If": ["IsProduction", "Retain", "Delete"]},
"Properties": {
"TopicName": {"Ref": "TopicName"},
},
}
},
"Outputs": {
"TopicArn": {"Value": {"Ref": "MyTopic"}},
},
}

stack = deploy_cfn_template(
template=json.dumps(template),
parameters={"Environment": environment, "TopicName": topic_name},
)

stack_description = aws_client.cloudformation.describe_stacks(StackName=stack.stack_id)
snapshot.match("stack-description", stack_description)

stack_resources = aws_client.cloudformation.describe_stack_resources(
StackName=stack.stack_id
)
snapshot.match("stack-resources", stack_resources)

@markers.aws.validated
@markers.snapshot.skip_snapshot_verify(paths=["$..LastOperations"])
def test_deletion_policy_with_fn_find_in_map(self, deploy_cfn_template, aws_client, snapshot):
snapshot.add_transformer(snapshot.transform.cloudformation_api())
snapshot.add_transformer(snapshot.transform.key_value("TopicArn"))

topic_name = f"test-topic-findmap-{short_uid()}"
snapshot.add_transformer(snapshot.transform.regex(topic_name, "<topic-name>"))

template = {
"AWSTemplateFormatVersion": "2010-09-09",
"Parameters": {
"Environment": {
"Type": "String",
"AllowedValues": ["dev", "staging", "prod"],
},
"TopicName": {"Type": "String"},
},
"Mappings": {
"EnvironmentPolicies": {
"dev": {"DeletionPolicy": "Delete"},
"staging": {"DeletionPolicy": "Retain"},
"prod": {"DeletionPolicy": "Retain"},
}
},
"Resources": {
"MyTopic": {
"Type": "AWS::SNS::Topic",
"DeletionPolicy": {
"Fn::FindInMap": [
"EnvironmentPolicies",
{"Ref": "Environment"},
"DeletionPolicy",
]
},
"UpdateReplacePolicy": {
"Fn::FindInMap": [
"EnvironmentPolicies",
{"Ref": "Environment"},
"DeletionPolicy",
]
},
"Properties": {"TopicName": {"Ref": "TopicName"}},
}
},
"Outputs": {"TopicArn": {"Value": {"Ref": "MyTopic"}}},
}

stack = deploy_cfn_template(
template=json.dumps(template),
parameters={"Environment": "staging", "TopicName": topic_name},
)

stack_description = aws_client.cloudformation.describe_stacks(StackName=stack.stack_id)
snapshot.match("stack-description", stack_description)

stack_resources = aws_client.cloudformation.describe_stack_resources(
StackName=stack.stack_id
)
snapshot.match("stack-resources", stack_resources)

@markers.aws.validated
@markers.snapshot.skip_snapshot_verify(paths=["$..LastOperations"])
def test_deletion_policy_with_ref_parameter(self, deploy_cfn_template, aws_client, snapshot):
snapshot.add_transformer(snapshot.transform.cloudformation_api())
snapshot.add_transformer(snapshot.transform.key_value("TopicArn"))

topic_name = f"test-topic-ref-{short_uid()}"
snapshot.add_transformer(snapshot.transform.regex(topic_name, "<topic-name>"))

template = {
"AWSTemplateFormatVersion": "2010-09-09",
"Parameters": {
"DeletionPolicyParam": {
"Type": "String",
"AllowedValues": ["Delete", "Retain", "Snapshot"],
},
"TopicName": {"Type": "String"},
},
"Resources": {
"MyTopic": {
"Type": "AWS::SNS::Topic",
"DeletionPolicy": {"Ref": "DeletionPolicyParam"},
"UpdateReplacePolicy": {"Ref": "DeletionPolicyParam"},
"Properties": {"TopicName": {"Ref": "TopicName"}},
}
},
"Outputs": {"TopicArn": {"Value": {"Ref": "MyTopic"}}},
}

stack = deploy_cfn_template(
template=json.dumps(template),
parameters={"DeletionPolicyParam": "Retain", "TopicName": topic_name},
)

stack_description = aws_client.cloudformation.describe_stacks(StackName=stack.stack_id)
snapshot.match("stack-description", stack_description)

stack_resources = aws_client.cloudformation.describe_stack_resources(
StackName=stack.stack_id
)
snapshot.match("stack-resources", stack_resources)


class TestValidateConditions:
@markers.aws.validated
Expand Down
Loading
Loading