aws-cdk: (aws-cloudfront): Unexpected diffs caused by cloudfront.Function
Non deterministic auto-generated function name causing unexpected diffs.
Reproduction Steps
Using this code:
new cloudfront.Function(stack, 'Function', {
code: cloudfront.FunctionCode.fromInline('function handler(event) { return event.request }'),
});
- synth and extract the function name
- synth again and extract the function name
The function name is different for every synth.
What did you expect to happen?
The function name should be the same.
What actually happened?
Function name changes per synth.
Environment
- CDK CLI Version : ALL
- Framework Version: 1.109.0
- Node.js Version: ALL
- OS : ALL
- Language (Version): ALL
Other
This happens because of:
Where Stack.of(this).region is an unresolved token that can have a different sequence number every time the program is executed.
A workaround right now would be to provide a name explicitly and not rely on this logic.
Workaround
Provide an explicit name for cloudfront.Function. Using this.node.addr or Node.of(this).addr will return a stable fixed length unique id. Example:
const functionId = `MyFunction${this.node.addr}`;
new cloudfront.Function(stack, functionId, {
code: cloudfront.FunctionCode.fromInline('function handler(event) { return event.request }'),
functionName: functionId
});
This is π Bug Report
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 20
- Comments: 19 (15 by maintainers)
Commits related to this issue
- chore: solve snapshots (#171) Explicitly set the CloudFront function name so snapshot is stable (see https://github.com/aws/aws-cdk/issues/15523). This also requires updating the logical ID of the... — committed to cdklabs/construct-hub by iliapolo 3 years ago
- fix(flaky-test): use stable logical id for CloudFront function There's an upstream bug in CDK that results in unstable generation of CloudFront function names: https://github.com/aws/aws-cdk/issues/1... — committed to capralifecycle/liflig-cdk by stekern 3 years ago
- fix(cdk): unstable CloudFront function name - The bug that a CloudFront function name fluctuated over deployment to deployment is worked around. There is a bug in CDK that it may generate differe... — committed to kikuomax/codemonger by kikuomax 2 years ago
@madeline-k I donβt believe I would tag it as p2 as basically the entire
cloudfront.Functionfeature is broken in this situation. I would tag it as a p1.Also, please add the workaround to the issue description so people donβt have to search for it.
Here is what we did in construct-hub (source).
This sounds like the behaviour Iβve just seen.
I had deployed a stack containing a cloudfront function, then went to deploy it again and got an error that it couldnβt find a function with the expected ID:
The ID itβs trying to find is almost correct, but has one character missing in the middle compared with the actual ID of the created function.
I hadnβt actually made any changes to the anything in the stack containing the cloudfront function, diff shows just the ID changing:
Any update on this? Sometimes my deployment succeeds; sometimes it doesnβt.
I think the problem is not the sequence number but actually the length of the unresolved token which is truncated (but I am not 100% sure). This requires taking a closer look.