Skip to content

Commit c7ad66f

Browse files
authored
fix(core): cross-stack reference error doesn't include violation (#23987)
When users are using cross-stack references that are not allowed, we just tell them the stacks that are involved in the reference, but not the reference itself. For example: ``` Stack "ExampleStack" cannot consume a cross reference from stack "cross-region-stack-1111111111:us-east-2". Cross stack references are only supported for stacks deployed to the same environment or between nested stacks and their parent stack. ``` This makes it very hard to debug what's going on, and why the reference is there in the first place. Render the reference as well so that it's easier to figure out why this is happening. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 252f052 commit c7ad66f

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

Diff for: packages/@aws-cdk/core/lib/private/refs.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,15 @@ function resolveValue(consumer: Stack, reference: CfnReference): IResolvable {
6161
// unsupported: stacks are not in the same account
6262
if (producerAccount !== consumerAccount) {
6363
throw new Error(
64-
`Stack "${consumer.node.path}" cannot consume a cross reference from stack "${producer.node.path}". ` +
64+
`Stack "${consumer.node.path}" cannot reference ${renderReference(reference)} in stack "${producer.node.path}". ` +
6565
'Cross stack references are only supported for stacks deployed to the same account or between nested stacks and their parent stack');
6666
}
6767

6868

6969
// Stacks are in the same account, but different regions
7070
if (producerRegion !== consumerRegion && !consumer._crossRegionReferences) {
7171
throw new Error(
72-
`Stack "${consumer.node.path}" cannot consume a cross reference from stack "${producer.node.path}". ` +
72+
`Stack "${consumer.node.path}" cannot reference ${renderReference(reference)} in stack "${producer.node.path}". ` +
7373
'Cross stack references are only supported for stacks deployed to the same environment or between nested stacks and their parent stack. ' +
7474
'Set crossRegionReferences=true to enable cross region references');
7575
}
@@ -113,7 +113,7 @@ function resolveValue(consumer: Stack, reference: CfnReference): IResolvable {
113113
if (producerRegion !== consumerRegion && consumer._crossRegionReferences) {
114114
if (producerRegion === cxapi.UNKNOWN_REGION || consumerRegion === cxapi.UNKNOWN_REGION) {
115115
throw new Error(
116-
`Stack "${consumer.node.path}" cannot consume a cross reference from stack "${producer.node.path}". ` +
116+
`Stack "${consumer.node.path}" cannot reference ${renderReference(reference)} in stack "${producer.node.path}". ` +
117117
'Cross stack/region references are only supported for stacks with an explicit region defined. ');
118118
}
119119
consumer.addDependency(producer,
@@ -133,6 +133,13 @@ function resolveValue(consumer: Stack, reference: CfnReference): IResolvable {
133133
return createImportValue(reference);
134134
}
135135

136+
/**
137+
* Return a human readable version of this reference
138+
*/
139+
function renderReference(ref: CfnReference) {
140+
return `{${ref.target.node.path}[${ref.displayName}]}`;
141+
}
142+
136143
/**
137144
* Finds all the CloudFormation references in a construct tree.
138145
*/

Diff for: packages/@aws-cdk/core/test/stack.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1698,7 +1698,7 @@ describe('stack', () => {
16981698

16991699
expect(() => {
17001700
app.synth();
1701-
}).toThrow(/Stack "Stack2" cannot consume a cross reference from stack "Stack1"/);
1701+
}).toThrow(/Stack "Stack2" cannot reference [^ ]+ in stack "Stack1"/);
17021702
});
17031703

17041704
test('urlSuffix does not imply a stack dependency', () => {

0 commit comments

Comments
 (0)