aws-cdk-lib: cross-stage references with PhysicalName.GENERATE_IF_NEEDED
- Dominant language
- TypeScript
- Stars
- 12.9k
- Forks
- 4.6k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 74
Description
### Describe the feature
I suggest to allow passing resorces between stages if resource name is marked with `PhysicalName.GENERATE_IF_NEEDED`.
### Use Case
I have a CDK Pipeline with multiple deployment stage running in a wave. 1 stage = 1 region, but same account. I also want to have 1 stage that deploying after all these stages. In my case, I want to deploy Cloudwatch dashboard that will show graph widget for the same lambda deployed to many regions (I want to use `addLeftMetric`, so graph widget will show 1 line per region on a single widget).
### Proposed Solution
It could be simple as this: https://github.com/aws/aws-cdk/commit/0e86b2e24fe434743d762e2818091eeb345d2947
The only concern I have:
Looking at [this code](https://github.com/aws/aws-cdk/blob/c610c979b742c9292273e30a43391826a3dba328/packages/aws-cdk-lib/core/lib/private/physical-name-generator.ts#L29) I can say that `PhysicalName.GENERATE_IF_NEEDED` can generate same names for different stages. It could be an issue if for stages deployed to the same account and region it will generage exactly the same resource names.
It could be solved by including this into name generator:
```ts
const stage = Stage.of(resource);
if (stage) {
sha256.update(stage.stageName);
}
```
But it is a breaking change (it will generate new names for people who was using `PhysicalName.GENERATE_IF_NEEDED` before this change).
### Other Information
I currently found a way to build a single dashboard using resources deployed by multiple stages. It looks like this: https://gist.github.com/braska/6f622fdac92d8c66d9d69300d480fbed
The only reason why it is possible - dashboard deployed to the different region where non of other stages deployed. You can see `env.region` for stages in `pipeline.ts`. It works because of [this condition](https://github.com/aws/aws-cdk/blob/c610c979b742c9292273e30a43391826a3dba328/packages/aws-cdk-lib/core/lib/resource.ts#L293C20-L293C20) CDK will generate static name.
But I still want to deploy dashboard into `us-east-1` and it is not possible with current implementation.
### Acknowledgements
- [X] I may be able to implement this feature request
- [X] This feature might incur a breaking change
### CDK version used
2.88.0
### Environment details (OS name and version, etc.)
(irrelevant)
Contributor guide
Research direction
Start with core/lib/private/physical-name-generator.ts and core/lib/resource.ts, then review the referenced commit and the pipeline.ts behavior described in the issue. Determine how cross-stage references and generated names should behave across regions and stages, including the breaking-change concern; done means the intended behavior is specified and covered by the relevant implementation tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- aws, typescript
- Domain
- cloud, infrastructure
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100