aws-cdk-lib: cross-stage references with PhysicalName.GENERATE_IF_NEEDED
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 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:
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 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)
I am not sure if this is a good idea as this introduces breaking change but having cross-stage reference is a good feature request.
@pahud thanks for the feedback. I agree. What could be an alternative?
I have 2 ideas:
- Add boolean
crossStageparameter here and here and here. It can be passed byResourcefrom here. Based on this parameter physical name generator will optionally includestagepart into hash. - Introduce a new marker:
PhysicalName.GENERATE. It will work almost the same way asPhysicalName.GENERATE_IF_NEEDED, but it will always generate name in synth time without any conditions. But I don't like thatPhysicalName.GENERATEwill work for cross-stage references andPhysicalName.GENERATE_IF_NEEDEDwill not. These names don't imply this difference.
We probably need more feedback from more people but if your proposed solution would not introduce any breaking changes, we always welcome community PRs for that. Also if you need to discuss the design with the core team SDE in the PR planning stage, feel free to schedule an office hour slot with the team. Stay tuned on this thread or the contributing channel on cdk.dev.
Thanks @pahud. I will book a slot for an office hour event as soon it will be announced.
FWIW I found using ._enableCrossEnvironment() on the resources in question (while also adding GENERATE_IF_NEEDED) seems to force CDK to generate a physical name even in cross-stage cases. The fact this method is prefixed _ suggests to me I should not be doing this though :)