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 }'),
});
  1. synth and extract the function name
  2. 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:

https://github.com/aws/aws-cdk/blob/94f81c441f9e2cb8dc70eb2e772d2cd75e468b67/packages/%40aws-cdk/aws-cloudfront/lib/function.ts#L178-L185

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

Most upvoted comments

@madeline-k I don’t believe I would tag it as p2 as basically the entire cloudfront.Function feature 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).

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 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:

3:58:23 pm | UPDATE_FAILED        | AWS::CloudFront::Function                       | CfnFunctionD7182995
Resource handler returned message: "Resource of type 'AWS::CloudFront::Function' with identifier 'ap-southeast-2kiernanSuSheetWebStackCfnFunction6F1B71FB' was not fo
und."

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:

cdk diff kiernan-...omitted/WebStack
Resources
[~] AWS::CloudFront::Function WebStack/CfnFunction CfnFunctionD7182995
 β”œβ”€ [~] FunctionConfig
 β”‚   └─ [~] .Comment:
 β”‚       └─ [~] .Fn::Join:
 β”‚           └─ @@ -4,6 +4,6 @@
 β”‚              [ ]     {
 β”‚              [ ]       "Ref": "AWS::Region"
 β”‚              [ ]     },
 β”‚              [-]     "kiernanSupSheetWebStackCfnFunction6F1B71FB"
 β”‚              [+]     "kiernanSuSheetWebStackCfnFunction6F1B71FB"
 β”‚              [ ]   ]
 β”‚              [ ] ]
 └─ [~] Name
     └─ [~] .Fn::Join:
         └─ @@ -4,6 +4,6 @@
            [ ]     {
            [ ]       "Ref": "AWS::Region"
            [ ]     },
            [-]     "kiernanSupSheetWebStackCfnFunction6F1B71FB"
            [+]     "kiernanSuSheetWebStackCfnFunction6F1B71FB"
            [ ]   ]
            [ ] ]

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.