feat(examples): set cross-stack reference strength explicitly to weak - #412
Open
laazyj wants to merge 1 commit into
Open
feat(examples): set cross-stack reference strength explicitly to weak#412laazyj wants to merge 1 commit into
laazyj wants to merge 1 commit into
Conversation
The examples configured no CDK context at all, so every feature flag was inherited from whatever CDK's unconfigured default happened to be — and synth warned that `@aws-cdk/core:defaultCrossStackReferences` had never been chosen, defaulting to "strong". The examples are the pattern users copy, so a default left implicit here becomes a default inherited downstream. Set the flag to "weak", CDK's recommended value. Cross-stack consumers now read the producer's output with `Fn::GetStackOutput` instead of importing a CloudFormation export. Strong references stop a producer removing an export a consumer still reads — protection worth having for a long-lived system, and the wrong trade for stacks CI deploys and destroys on every run. Contrary to the flag's own description, the choice is not confined to cross-region references: in aws-cdk-lib 2.264.0 a same-account, same-region consumer resolves to `Fn::GetStackOutput` under "weak" too, so `ComposureCDK-MultiStackApiStack`'s template changes and its snapshot moves with it. The producer's output loses its `Export`; nothing else in the suite references across stacks. The context is declared twice by necessity: `cdk.json` for the CLI, and `EXAMPLE_CONTEXT` for the tests, which build their own `App`. CDK applies CLI context after `App`'s `context` prop, so a divergence would not fail on its own — it would leave the tests asserting a template CI never deploys. A new test asserts the two stay in sync, that the multi-stack example resolves weakly, and that an app built without the context still warns. Refs #341
Contributor
CoverageOverall line coverage: 99.40% across 24 package(s).
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What & why
Closes the first item of #341.
packages/examples/cdk.jsonconfigured no context at all, so every feature flag was inherited from whatever CDK's unconfigured default happened to be, and synth warned that@aws-cdk/core:defaultCrossStackReferenceshad never been chosen:The examples are the pattern users copy, so a default left implicit here becomes a default inherited downstream. This sets the flag to
"weak"— CDK'srecommendedValuefor it — and gives the examples one place to declare a flag decision.What "weak" changes
Cross-stack consumers read the producer's output with
Fn::GetStackOutputinstead of importing a CloudFormation export. Strong references stop a producer removing an export a consumer still reads — protection worth having for a long-lived system, and the wrong trade for stacks CI deploys and destroys on every run.Contrary to the flag's own description, this is not confined to cross-region references. The flag docs say "same-region references are always strong (
Fn::ImportValue)", but in aws-cdk-lib 2.264.0 the same-account/same-region tail ofresolveValuereads:So
ComposureCDK-MultiStackApiStack's template really does change, and its snapshot moves with it: the producer's output loses itsExport, the consumer swapsFn::ImportValueforFn::GetStackOutput, and the API deployment's logical-id hash shifts with the body. Nothing else in the suite references across stacks, so that is the only snapshot touched.No deployed stacks are affected — the examples are torn down after each smoke-test run, so this starts from a clean slate. An existing system migrating from strong would need the staged
"both"→ deploy everywhere →"weak"path instead, which the README now documents.Why the context is declared twice
cdk.jsonfor the CLI, andEXAMPLE_CONTEXTinsrc/app-context.tsfor the tests, which build their ownApprather than going through the CLI. CDK applies CLI context after anApp'scontextprop, so a divergence would not fail on its own — it would quietly leave the tests asserting a template CI never deploys. A new test asserts the two stay in sync.Every example now defaults its
appparameter toexampleApp()rather than a barenew App(), so a stack synthesised by its own test sees what CI deploys. That is what makes the multi-stack snapshot correct rather than merely updated, and it is where the next flag decision will land without further plumbing.Tests
test/app-context.test.tscovers:cdk.json's context matchesEXAMPLE_CONTEXTexampleApp()applies it, and a caller can override an entryFn::GetStackOutputpresent,Fn::ImportValueabsent)The warning assertion keys on the annotation id (
@aws-cdk/core:crossStackReferencesDefaultStrong) rather than CDK's prose, so a reworded message does not break the build.Not in this PR
The other two items on #341 — pinning a reviewed flag set generally (81 remain unconfigured) and the multi-phase Stack interaction — are unaddressed. The first is worth its own change following the
cdk-floors.jsonshape: a manifest plus a:checkthat fails when CDK adds a flag we have not reviewed. Two of the recommended flags collide with decisions already made here, which is the argument for reviewing them individually rather than bulk-enabling:@aws-cdk/core:validateAgainstDefaultRuleswas deliberately left unset in #339, and@aws-cdk/core:annotationsInValidationReportwould route CDK annotations intovalidation-report.json, whichcheck-validation-report.mjsfails on at any severity.Checklist
npm run verifypasses locallyVerified beyond
verify: the CloudFormation Validate gate from #339 reports no findings on the newFn::GetStackOutputoutput, andcdk synthnow reports 81 unconfigured flags rather than 82, with the warning gone.Generated by Claude Code