Skip to content
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): Function name is created non-deterministically #28629

Closed
kylelaker opened this issue Jan 9, 2024 · 3 comments
Closed

(cloudfront): Function name is created non-deterministically #28629

kylelaker opened this issue Jan 9, 2024 · 3 comments
Labels
@aws-cdk/aws-cloudfront Related to Amazon CloudFront bug This issue is a bug. effort/medium Medium work item – several days of effort p2

Comments

@kylelaker
Copy link
Contributor

kylelaker commented Jan 9, 2024

Describe the bug

Creating a CloudFront Function without specifying name can result in non-deterministic naming when the Node ID is relatively long. This results in unexpected function renames.

Expected Behavior

The method used to create a CloudFront Function name should be deterministic, predicable, and stable.

Current Behavior

The name of Functions is unstable, resulting in resource renames and snapshot failures.

Reproduction Steps

Run the following sample integration test several times. See that eventually it bounces between:

  • AReallyLonIdIsAlsoPrettyLongIGuess25040025
  • AReallyLoIdIsAlsoPrettyLongIGuess25040025
    for the name of the function.
import * as cdk from 'aws-cdk-lib';
import * as cloudfront from 'aws-cdk-lib/aws-cloudfront';
import * as integ from '@aws-cdk/integ-tests-alpha';

const app = new cdk.App();
const stack = new cdk.Stack(app, 'A-Really-Long-Stack-Name-Example');

const fn = new cloudfront.Function(stack, 'ExampleFunctionIdIsAlsoPrettyLongIGuess', {
  code: cloudfront.FunctionCode.fromInline('function handler(event) { return event.request }'),
});

new integ.IntegTest(app, 'FunctionNameLong', {
  testCases: [stack],
});

Possible Solution

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;
}
}
Should probably be reworked but that might result in yet again renaming functions. Names.uniqueResourceName should probably be used instead.

Additional Information/Context

This is also causing the failure on #28571

CDK CLI Version

2.118.0

Framework Version

2.118.0

Node.js Version

v20.2.0

OS

Linux

Language

TypeScript

Language Version

No response

Other information

No response

@kylelaker kylelaker added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Jan 9, 2024
@github-actions github-actions bot added the @aws-cdk/aws-cloudfront Related to Amazon CloudFront label Jan 9, 2024
@pahud
Copy link
Contributor

pahud commented Jan 9, 2024

Yes I guess we should use Names.uniqueResourceName.

@pahud pahud added p2 effort/medium Medium work item – several days of effort and removed needs-triage This issue or PR still needs to be triaged. labels Jan 9, 2024
@kylelaker
Copy link
Contributor Author

kylelaker commented Jan 10, 2024

Actually making that change should be pretty trivial and non-disruptive; apparently function name resources require no interruption for updates. I'll work on the PR. https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-cloudfront-function.html#cfn-cloudfront-function-name

Edit: Just kidding... the CloudFormation docs are wrong. aws-cloudformation/cloudformation-coverage-roadmap#1893 has been opened to report the documentation issue.

mergify bot pushed a commit that referenced this issue Jun 11, 2024
### Issue # (if applicable)

Closes #20017 as well as #15523 and #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*
Copy link

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

mazyu36 pushed a commit to mazyu36/aws-cdk that referenced this issue Jun 22, 2024
### 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*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-cloudfront Related to Amazon CloudFront bug This issue is a bug. effort/medium Medium work item – several days of effort p2
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants