Skip to content

Commit 8a26869

Browse files
authored
fix(cloudfront): Function ARN reference changed from GetAtt to Ref (#35547)
When copying the underlying `CfnFunction`'s implementation of `IFunctionRef`, the source of the `functionArn` value changed from a `{ GetAtt }` to a `{ Ref }`. Both expression return the same value, but if the ARN is exported to another stack, CloudFormation will reject any change to the expression under the assumption that `different expression ⇒ different value`, and that's not allowed. Override the implementation for the L2, returning the same expression as previously. Closes #35531 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 761dde2 commit 8a26869

File tree

6 files changed

+42
-6
lines changed

6 files changed

+42
-6
lines changed

packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-function-runtime.js.snapshot/integ-distribution-function.template.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,19 @@
3535
{
3636
"EventType": "viewer-request",
3737
"FunctionARN": {
38-
"Ref": "FunctionRequest95528B2F"
38+
"Fn::GetAtt": [
39+
"FunctionRequest95528B2F",
40+
"FunctionARN"
41+
]
3942
}
4043
},
4144
{
4245
"EventType": "viewer-response",
4346
"FunctionARN": {
44-
"Ref": "FunctionResponse4EF2D1D3"
47+
"Fn::GetAtt": [
48+
"FunctionResponse4EF2D1D3",
49+
"FunctionARN"
50+
]
4551
}
4652
}
4753
],

packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-function.js.snapshot/integ-distribution-function.template.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@
2323
{
2424
"EventType": "viewer-request",
2525
"FunctionARN": {
26-
"Ref": "Function76856677"
26+
"Fn::GetAtt": [
27+
"Function76856677",
28+
"FunctionARN"
29+
]
2730
}
2831
}
2932
],

packages/aws-cdk-lib/aws-cloudfront/lib/function.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,11 @@ export class Function extends Resource implements IFunction {
223223
name: this.functionName,
224224
});
225225

226-
this.functionRef = resource.functionRef;
227226
this.functionArn = resource.attrFunctionArn;
228227
this.functionStage = resource.attrStage;
228+
this.functionRef = {
229+
functionArn: this.functionArn,
230+
};
229231
}
230232

231233
private generateName(): string {

packages/aws-cdk-lib/aws-cloudfront/test/distribution.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -920,7 +920,10 @@ describe('with CloudFront functions', () => {
920920
{
921921
EventType: 'viewer-request',
922922
FunctionARN: {
923-
Ref: 'TestFunction22AD90FC',
923+
'Fn::GetAtt': [
924+
'TestFunction22AD90FC',
925+
'FunctionARN',
926+
],
924927
},
925928
},
926929
],

packages/aws-cdk-lib/aws-cloudfront/test/function.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,5 +292,24 @@ describe('CloudFront Function', () => {
292292
},
293293
});
294294
});
295+
296+
test('CloudFront FunctionRef uses GetAtt, not Ref', () => {
297+
// Both GetAtt and Ref are valid ways to satisfy the contract, but only
298+
// GetAtt is backwards compatible.
299+
const stack = new Stack();
300+
301+
const fn = new Function(stack, 'TestFn', {
302+
code: FunctionCode.fromInline('code'),
303+
runtime: FunctionRuntime.JS_2_0,
304+
keyValueStore: undefined,
305+
});
306+
307+
expect(stack.resolve(fn.functionRef.functionArn)).toEqual({
308+
'Fn::GetAtt': [
309+
'TestFn04335C60',
310+
'FunctionARN',
311+
],
312+
});
313+
});
295314
});
296315
});

packages/aws-cdk-lib/aws-cloudfront/test/web-distribution.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,10 @@ added the ellipsis so a user would know there was more to r...`,
810810
{
811811
'EventType': 'viewer-request',
812812
'FunctionARN': {
813-
'Ref': 'TestFunction22AD90FC',
813+
'Fn::GetAtt': [
814+
'TestFunction22AD90FC',
815+
'FunctionARN',
816+
],
814817
},
815818
},
816819
],

0 commit comments

Comments
 (0)