-
Notifications
You must be signed in to change notification settings - Fork 4.5k
(aws-cloudfront): Unexpected diffs caused by cloudfront.Function #15523
Copy link
Copy link
Closed
Labels
@aws-cdk/aws-cloudfrontRelated to Amazon CloudFrontRelated to Amazon CloudFrontbugThis issue is a bug.This issue is a bug.effort/mediumMedium work item – several days of effortMedium work item – several days of effortp2
Description
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:
aws-cdk/packages/@aws-cdk/aws-cloudfront/lib/function.ts
Lines 178 to 185 in 94f81c4
| private generateName(): string { | |
| const name = Stack.of(this).region + Names.uniqueId(this); | |
| if (name.length > 64) { | |
| return name.substring(0, 32) + name.substring(name.length - 32); | |
| } | |
| return name; | |
| } | |
| } |
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
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
@aws-cdk/aws-cloudfrontRelated to Amazon CloudFrontRelated to Amazon CloudFrontbugThis issue is a bug.This issue is a bug.effort/mediumMedium work item – several days of effortMedium work item – several days of effortp2