The error Error: Stack "exampleStack1" cannot consume a cross reference from stack "exampleStack2". Cross stack references are only supported for stacks deployed to the same environment or between nested stacks and their parent stack likely happens if you do the following:
- You try to reference another stack that is in a different AWS region. Cross-Region references only work when both stacks set
crossRegionReferences: true. - You aren't passing
regionandaccountvalues in theenvof theStackProps.
To fix the error in AWS CDK, you need to add the env with region and account parameters to the stack when you create it as shown in the code example below:
new exampleStack1(
app, 'exampleStack1', {
env: {
region: 'eu-west-1',
account: '012345678910',
},
});
}
Frequently Asked Questions
Why does CDK say it cannot consume a cross reference from another stack?
Cross-stack references only work between stacks deployed to the same environment, or between a nested stack and its parent. Referencing a stack in a different account triggers the error, and so does a different Region unless both stacks opt in to cross-Region references.
Does CDK support cross-region references between stacks?
Yes, as an opt-in. Set `crossRegionReferences: true` on both stacks, each with an explicit `env` Region, and CDK passes the values through SSM parameters. Without that setting, a reference to a stack in another Region triggers this error.
How do I set the environment on a CDK stack?
Pass an env object with region and account values in the StackProps when you create the stack.
Can a nested stack reference its parent stack?
Yes. The error message itself lists nested stacks and their parent stack as a supported case for cross-stack references.
Next step
Untangle Cross-Stack References in Your CDK App
Transform your complex CDK codebase into a structured, reusable architecture. Get real-world expertise from someone who's built production CDK at scale.