Search code examples
amazon-web-servicesaws-cloudformationaws-cdkclouddevelopmentkit

How can you pass variables between cdk stacks without getting cross reference error?


I need to pass INamespace to CloudMapOptions in order that the ECS tasks register to the AWS CloudMap, I get following error. I can't decouple them with CfnOutput, because I need the namespace in the ECS cloudMapOptions:

   Stack "users" cannot consume a cross reference from stack "servicediscovery". Cross stack references are only supported for stacks deployed to the same environment or between nested stacks and their parent stack.

I tried this : https://bobbyhadz.com/blog/aws-cdk-share-vpc-between-stacks, but without any success. How can I pass a variable of type INamespace between stacks?


Solution

  • This error likely happens if you do one of the following:

    1. You try to create a reference between stacks which are defined to live in 2 different regions

    Note that such cross region references are not supported

    1. You supply different StackProps as 3rd parameter to Stack constructor

    In order to resolve that please make sure to pass the same env value in StackProps e.g.

    class MyStack extends Stack {
      constructor(scope: Construct){
        super(scope, "MyStack", {
          env: { 
            account: '123456789', 
            region: 'eu-central-1'
          }
        })
      }
    }