-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(cloudfront): (cloudfront function generated name is not deterministic and will change between synthesis) #20017
(cloudfront): (cloudfront function generated name is not deterministic and will change between synthesis) #20017
Comments
This is an interesting bug, thank you very much for the deep dive and thorough explanation of what's happening here. It's much appreciated 🙂 In case you're unaware, you can override the properties generated by the CDK with escape hatches to ensure your function name stays consistent until we get this fixed up. |
Thank you very much @peterwoodworth. I am aware of the escape hatches. Currently I can workaround the issue by specifying
|
|
1 similar comment
|
### Issue # (if applicable) Closes aws#20017 as well as aws#15523 and aws#28629 ### Reason for this change Due to the way function names are generated using token strings with either single- or double-digit numbers, longer function names can be truncated differently, leading to inconsistency in generated CloudFormation templates. ### Description of changes To ensure backwards compatibility, if names are longer than 64 characters and use region tokens, if the token uses a single-digit region number, it takes the first **31** characters + the last 32 characters; if the token uses a double-digit region number or otherwise, it takes the first **32** characters + the last 32 characters. This ensures it will always take the same first chunk of the actual function's name. ### Description of how you validated changes A new unit test was added to verify the consistency of function names in the template. ### 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*
Describe the bug
When using
cloudfront.Function
without specifying the function name, the constructcloudfront.Function
will generate a name automatically. However this generated function name will change from time to time between synthesis due to the name truncated to a fixed length with a region token prefix in it. This change of name caused deployment failure when deploying onto an existing stack.For example, with below code in a stack named
CdkIssueWithALongNameStack
Running
cdk synth
multiple times, It can be observed that during some runs, in the synthesised CloudFormation template, we can find below. Please note the function name isCdkIssueWtackMyCloudFrontFunction6157953B
.However, some other runs, the name will change to
CdkIssueWitackMyCloudFrontFunction6157953B
. Note the additionali
afterCdkIssueW
.This name change of the cloudfront function will cause failure of deployment on an existing stack as CDK will report error like:
Expected Behavior
The automatically generated function name should remain the same during multiple cdk synth.
Current Behavior
The name changes due to the following reason:
The following code snippet is how
cloudfront.Function
generate its function name if the name is not specified in the props.aws-cdk/packages/@aws-cdk/aws-cloudfront/lib/function.ts
Line 178 in 9487b39
it added
Stack.of(this).region
in front of the unique id of this construct and then took the first 32 chars and then last 32 chars if the string is longer than 64 chars, and concatenated these two parts together to form the name of the function. However, the region added in front of the name is still a token at this time, so the name string became:${Token[AWS.Region.7]}CdkIssueWithALongNameStackMyCloudFrontFunction6157953B
This string is then sent through
name.substring(0, 32) + name.substring(name.length - 32);
and resulted in two parts:${Token[AWS.Region.7]}CdkIssueWi
andtackMyCloudFrontFunction6157953B
and then they are concatenated together to make the name${Token[AWS.Region.7]}CdkIssueWitackMyCloudFrontFunction6157953B
, which eventually became the name in the CloudFormation template.However, the region token string could be different every time cdk synth runs, sometimes, the region token will have a two digits value like 11 instead of 7, then the name above will be like:
${Token[AWS.Region.11]}CdkIssueWithALongNameStackMyCloudFrontFunction6157953B
, which run throughname.substring(0, 32) + name.substring(name.length - 32);
and resulted in two parts:${Token[AWS.Region.11]}CdkIssueW
andtackMyCloudFrontFunction6157953B
and then they are concatenated together make the name${Token[AWS.Region.11]}CdkIssueWtackMyCloudFrontFunction6157953B
. This name is different from the name when the number in region token is one digit, and missed thati
.Since the number in the region token is unpredictable so after a few rounds of cdk synth command, the name will change between those two results.
Reproduction Steps
cdk init sample-app --language=typescript
.lib/<app name>-stack.ts
bin/<app name>.ts
. Make sure the stack name is long enoughnpm run build
and run cdk synth withnpx cdk synth -q
, the following content will be printed out during thecdk synth
cdk.out
and search forAWS::CloudFront::Function
resource and there should be something like below:Write down the name
npx cdk synth -q
multiple times until you can see the number in the region token is one digitthen check the generated CloudFormation template in
cdk.out
, and search forAWS::CloudFront::Function
resource and you can see the name of the resource becameCdkIssueWitackMyCloudFrontFunction6157953B
with an additionali
.Possible Solution
The quoted code snippet below are from
cloudfront.Function
construct, instead of adding the region token into the name string first, it could perform the name truncating and concatenation without adding the region token in front of it, and then add the region as prefix after to make the new name. So the concatenated name will be stable and won't change due to the different length of the region token (the digits of the number in it)Below is one possible fix and it might need further validation:
Current code in
cloudfront.Function
:aws-cdk/packages/@aws-cdk/aws-cloudfront/lib/function.ts
Line 178 in 9487b39
Additional Information/Context
No response
CDK CLI Version
2.20.0 (build 738ef49)
Framework Version
No response
Node.js Version
v14.18.2
OS
macOS Monterey 12.2.1
Language
Typescript
Language Version
Typescript(3.9.7)
Other information
No response
The text was updated successfully, but these errors were encountered: