Skip to content

Commit

Permalink
fix(core): cross-stack reference error doesn't include violation (#23987
Browse files Browse the repository at this point in the history
)

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*
  • Loading branch information
rix0rrr authored Feb 7, 2023
1 parent 252f052 commit c7ad66f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
13 changes: 10 additions & 3 deletions packages/@aws-cdk/core/lib/private/refs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ function resolveValue(consumer: Stack, reference: CfnReference): IResolvable {
// unsupported: stacks are not in the same account
if (producerAccount !== consumerAccount) {
throw new Error(
`Stack "${consumer.node.path}" cannot consume a cross reference from stack "${producer.node.path}". ` +
`Stack "${consumer.node.path}" cannot reference ${renderReference(reference)} in stack "${producer.node.path}". ` +
'Cross stack references are only supported for stacks deployed to the same account or between nested stacks and their parent stack');
}


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

/**
* Return a human readable version of this reference
*/
function renderReference(ref: CfnReference) {
return `{${ref.target.node.path}[${ref.displayName}]}`;
}

/**
* Finds all the CloudFormation references in a construct tree.
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/core/test/stack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1698,7 +1698,7 @@ describe('stack', () => {

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

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

0 comments on commit c7ad66f

Please sign in to comment.