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

fix(cloudfront): function name is non-deterministic (under feature flag) #28641

Closed
wants to merge 3 commits into from

Conversation

laurelmay
Copy link
Contributor

@laurelmay laurelmay commented Jan 10, 2024

This fixes an issue where having a long stack name or resource ID could
result in inconsistent naming of the function (replacing it on each
deployment) because the length of the token that holds the region may
not be consistent. This uses a more deterministic method based on
checking whether the region is resolved and using the
uniqueResourceName function.

This change is behind a feature flag,
@aws-cdk/aws-cloudfront:useStableGeneratedFunctionName to avoid issues
with replacing existing functions.

Closes #28629


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

This fixes an issue where having a long stack name or resource ID could
result in inconsistent naming of the function (replacing it on each
deployment) because the length of the token that holds the region may
not be consistent. This uses a more deterministic method based on
checking whether the region is resolved and using the
`uniqueResourceName` function.

It is important to note that while this doesn't change the name of any
current resources in the unit/integration tests, I am not 100% certain
that this won't impact any existing users.
@github-actions github-actions bot added the star-contributor [Pilot] contributed between 25-49 PRs to the CDK label Jan 10, 2024
@aws-cdk-automation aws-cdk-automation requested a review from a team January 10, 2024 04:04
@github-actions github-actions bot added bug This issue is a bug. effort/medium Medium work item – several days of effort p2 labels Jan 10, 2024
@aws-cdk-automation aws-cdk-automation added the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label Jan 10, 2024
// then we use the name of the longest known region.
const serviceMaxNameLength = 64;
const region = Stack.of(this).region;
const regionNameLength = !Token.isUnresolved(region) ? region.length : Math.max(...Fact.regions.map((r) => r.length));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to me that this can potentially trigger re-deploy for those cases where region is not resolved.
Given that the issue needs to be fixed and it's probably going to be a breaking change anyway, I think it may be worth considering a refactor/simplification of the name generation function (under a feature flag) to avoid future issues, eg:

return Lazy.string({
  produce: () => Names.uniqueResourceName(this, { maxLength: 64, allowedSpecialCharacters: '-_' }),
});

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that including the region name is, unfortunately, a very intentional decision so that the same stack can be deployed in multiple regions. #14511 (comment)

But then again, any callers can always just pass functionName: `${this.env.region}Name`, if that's really an use case for them under the feature flag.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the feature flag and including a note about region inclusion as part of the detailsMd. Let me know what you think!

@laurelmay laurelmay changed the title fix(cloudfront): function name is non-deterministic fix(cloudfront): function name is non-deterministic (under feature flag) Jan 14, 2024
@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: 092fdc3
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@aws-cdk-automation aws-cdk-automation added pr/needs-maintainer-review This PR needs a review from a Core Team Member and removed pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. labels Jan 17, 2024
@GavinZZ
Copy link
Contributor

GavinZZ commented Jan 30, 2024

an issue where having a long stack name or resource ID could
result in inconsistent naming of the function (replacing it on each
deployment)

For my own understanding, how does the existing behaviour cause redeployment if stack name and id are not changed?

@laurelmay
Copy link
Contributor Author

For my own understanding, how does the existing behaviour cause redeployment if stack name and id are not changed?

You should be able to see this if you run the newly added integration tests a couple of times. You will see the function name property change. The CloudFormation documentation for this resource is wrong -- editing the FunctionName property does trigger resource replacement (this can be seen by viewing the CloudFormation stack events after the integration test shows a changed name). I believe there is more information in the linked issue.

While replacing a CloudFront function is a minimally disruptive operation, it is still disruptive and a resource has been replaced. We should avoid arbitrarily changing resource physical names within the CDK.

GavinZZ
GavinZZ previously approved these changes Mar 7, 2024
Copy link
Contributor

@GavinZZ GavinZZ left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for contributing.

@GavinZZ GavinZZ dismissed their stale review March 7, 2024 01:05

Dismissing my approval as I noticed a lot of conflicts. Please address the conflicts and ping me again for another pass.

@GavinZZ
Copy link
Contributor

GavinZZ commented Apr 3, 2024

@kylelaker would you be able to fix the merge conflicts? This PR looks good and I'm happy to approve once conflicts are resolved.

@aaythapa aaythapa added the pr-linter/do-not-close The PR linter will not close this PR while this label is present label Apr 18, 2024
@aws-cdk-automation aws-cdk-automation removed the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Apr 18, 2024
@Leo10Gama
Copy link
Member

Closing this as it seems the PR has not been active for quite some time, and another PR has been opened to resolve this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. effort/medium Medium work item – several days of effort p2 pr-linter/do-not-close The PR linter will not close this PR while this label is present star-contributor [Pilot] contributed between 25-49 PRs to the CDK
Projects
None yet
Development

Successfully merging this pull request may close these issues.

(cloudfront): Function name is created non-deterministically
6 participants