Skip to content

Conversation

@mrgrain
Copy link
Contributor

@mrgrain mrgrain commented Nov 11, 2025

Reason for this change

Refactor repeated pattern of anonymous classes that throw UnscopedValidationError when accessing node or env properties.

This pattern exists because certain legacy CDK APIs like ManagedPolicy.fromAwsManagedPolicyName() and CloudFront's fromManagedCachePolicy() return construct-like objects without requiring a scope parameter. These APIs predate modern CDK patterns where all constructs require a scope. They cannot be changed without breaking existing customer code, so they return "detached" constructs that throw errors when used in APIs that require a proper construct tree context.

But we also sometimes add new features to resource interfaces like IManagedPolicy. When this happens, we have to update this fake implementation in a number of places. It also prevents us (and users) from providing augmentations for the IConstruct interface like proposed in #36013.

With this change, we have reduced the number of places to update to one. It's now also possible to augment IConstruct.

Description of changes

  • Created DetachedConstruct base class in core/lib/private/detached-construct.ts that encapsulates the pattern of throwing UnscopedValidationError when accessing node or env
  • Refactored CloudFront policy classes (CachePolicy, OriginRequestPolicy, ResponseHeadersPolicy) to extend DetachedConstruct instead of implementing the pattern inline
  • Refactored ManagedPolicy.fromAwsManagedPolicyName() to use DetachedConstruct
  • Refactored ElasticBeanstalkDeployAction to use DetachedConstruct for managed policy reference
  • Added unit tests for DetachedConstruct to verify error throwing behavior

This change reduces code duplication and makes the pattern more maintainable. The DetachedConstruct class is marked as internal and documented to discourage use in new APIs.

Describe any new or updated permissions being added

No new or updated permissions.

Description of how you validated changes

  • Added unit tests in core/test/private/detached-construct.test.ts that verify UnscopedValidationError is thrown when accessing node and env properties
  • Existing tests continue to pass, ensuring no behavioral changes

Checklist


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

@mrgrain mrgrain requested a review from a team as a code owner November 11, 2025 19:14
@aws-cdk-automation aws-cdk-automation requested a review from a team November 11, 2025 19:14
@github-actions github-actions bot added the p2 label Nov 11, 2025
@mergify mergify bot added the contribution/core This is a PR that came from AWS. label Nov 11, 2025
@mrgrain mrgrain force-pushed the mrgrain/chore/detached-construct branch from 0fc5514 to 08f0a39 Compare November 11, 2025 19:49
@mergify
Copy link
Contributor

mergify bot commented Nov 12, 2025

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@mergify
Copy link
Contributor

mergify bot commented Nov 12, 2025

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@mergify mergify bot merged commit 667de25 into main Nov 12, 2025
18 of 19 checks passed
@mergify mergify bot deleted the mrgrain/chore/detached-construct branch November 12, 2025 09:08
@github-actions
Copy link
Contributor

Comments on closed issues and PRs are hard for our team to see.
If you need help, please open a new issue that references this one.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 12, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

contribution/core This is a PR that came from AWS. p2

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants