Skip to content

Commit

Permalink
Only replace parameter values in Resources (#3908)
Browse files Browse the repository at this point in the history
  • Loading branch information
kddejong authored Jan 13, 2025
1 parent 0d17484 commit 3b07bf7
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/cfnlint/template/transforms/_sam.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,9 @@ def _replace_variables_with_language_extension(self):
if isinstance(v, dict) and v.get("Default"):
parameters[k] = v.get("Default")

self._template = self._find_and_replace(self._template, parameters)
self._template["Resources"] = self._find_and_replace(
self._template.get("Resources", {}), parameters
)

def transform_template(self):
"""
Expand Down
5 changes: 5 additions & 0 deletions src/cfnlint/template/transforms/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ def transform(self, cfn: Any) -> list[Match]:
continue

matches, template = transform(cfn)
LOGGER.debug(
"Transformed template from %s: \n%s",
name,
format_json_string(cfn.template),
)
if matches:
return matches
cfn.template = template
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,21 @@ Parameters:
VpcId:
Type: "AWS::SSM::Parameter::Value<AWS::EC2::VPC::Id>"
Default: "/network/vpc/primary/id"
Environment:
Type: String
Default: Prod
Mappings:
StackIdMap01:
teststack1:
DeletionPolicy: Retain
teststack2:
DeletionPolicy: Delete
Rules:
IsAutPublishAliasParameterProd:
RuleCondition: !Equals [ !Ref Environment, "Prod" ]
Assertions:
- Assert: !Not
- !Equals [ !Ref DBPolicy, "" ]
Resources:
WaitHandleRef:
Type: AWS::CloudFormation::WaitConditionHandle
Expand Down
15 changes: 15 additions & 0 deletions test/unit/module/transform/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,21 @@ def test_sam_with_language_extension(self):
template = cfn_yaml.load(filename)
transformed_template = Transform(filename, template, region)
results = transformed_template.transform_template()
self.assertDictEqual(
transformed_template._template.get("Rules"),
{
"IsAutPublishAliasParameterProd": {
"Assertions": [
{
"Assert": {
"Fn::Not": [{"Fn::Equals": [{"Ref": "DBPolicy"}, ""]}]
}
}
],
"RuleCondition": {"Fn::Equals": [{"Ref": "Environment"}, "Prod"]},
}
},
)
self.assertEqual(results, [])

def test_parameter_for_autopublish_code_sha256(self):
Expand Down

0 comments on commit 3b07bf7

Please sign in to comment.