Skip to content

Commit aa186ac

Browse files
authored
fix(CLI): sam resources hidden in changeset diffs (#29223)
### Issue # (if applicable) Closes #29185 ### Reason for this change CFN applies the SAM transform before the changeset is created. This means that SAM resources become their underlying CFN types in the template that the changeset operates on. This means that the changeset is operating on resources that we don't see in our template. ### Description of changes Before, if we saw properties in our diff that were not in the changeset (like `codeUri` for `Serverless::Function`, which doesn't appear in the changeset, because it becomes `Code` for `Lambda::Function`), we'd filter them out of the diff. We now skip this process for SAM resources. ### Description of how you validated changes unit test ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 2e080fe commit aa186ac

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

packages/@aws-cdk/cloudformation-diff/lib/diff-template.ts

+4
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,10 @@ function addImportInformation(diff: types.TemplateDiff, changeSet: CloudFormatio
221221
function filterFalsePositivies(diff: types.TemplateDiff, changeSet: CloudFormation.DescribeChangeSetOutput) {
222222
const replacements = findResourceReplacements(changeSet);
223223
diff.resources.forEachDifference((logicalId: string, change: types.ResourceDifference) => {
224+
if (change.resourceType.includes('AWS::Serverless')) {
225+
// CFN applies the SAM transform before creating the changeset, so the changeset contains no information about SAM resources
226+
return;
227+
}
224228
change.forEachDifference((type: 'Property' | 'Other', name: string, value: types.Difference<any> | types.PropertyDifference<any>) => {
225229
if (type === 'Property') {
226230
if (!replacements[logicalId]) {

packages/@aws-cdk/cloudformation-diff/test/diff-template.test.ts

+49
Original file line numberDiff line numberDiff line change
@@ -1118,6 +1118,55 @@ describe('changeset', () => {
11181118
expect(differences.resources.differenceCount).toBe(1);
11191119
});
11201120

1121+
test('SAM Resources are rendered with changeset diffs', () => {
1122+
// GIVEN
1123+
const currentTemplate = {
1124+
Resources: {
1125+
ServerlessFunction: {
1126+
Type: 'AWS::Serverless::Function',
1127+
Properties: {
1128+
CodeUri: 's3://bermuda-triangle-1337-bucket/old-handler.zip',
1129+
},
1130+
},
1131+
},
1132+
};
1133+
1134+
// WHEN
1135+
const newTemplate = {
1136+
Resources: {
1137+
ServerlessFunction: {
1138+
Type: 'AWS::Serverless::Function',
1139+
Properties: {
1140+
CodeUri: 's3://bermuda-triangle-1337-bucket/new-handler.zip',
1141+
},
1142+
},
1143+
},
1144+
};
1145+
1146+
let differences = fullDiff(currentTemplate, newTemplate, {
1147+
Changes: [
1148+
{
1149+
Type: 'Resource',
1150+
ResourceChange: {
1151+
Action: 'Modify',
1152+
LogicalResourceId: 'ServerlessFunction',
1153+
ResourceType: 'AWS::Lambda::Function', // The SAM transform is applied before the changeset is created, so the changeset has a Lambda resource here!
1154+
Replacement: 'False',
1155+
Details: [{
1156+
Evaluation: 'Direct',
1157+
Target: {
1158+
Attribute: 'Properties',
1159+
Name: 'Code',
1160+
RequiresRecreation: 'Never',
1161+
},
1162+
}],
1163+
},
1164+
},
1165+
],
1166+
});
1167+
expect(differences.resources.differenceCount).toBe(1);
1168+
});
1169+
11211170
test('imports are respected for new stacks', async () => {
11221171
// GIVEN
11231172
const currentTemplate = {};

0 commit comments

Comments
 (0)