CDK Stack Export Resource not Found

147 Views Asked by At

I'm trying to import values generated and exported from a cdk stack that deploys into eu-west-1 into a stack that needs to be deployed into af-south-1 but I keep getting the error that the export name does not exist:

EU

new CfnOutput(this, 'userPoolArn', {
value: this.userPoolPattern.userPoolArn,})

AF

const userPoolArn = cdk.Fn.importValue('EuWest1Infrastructure:userPoolArn');

Error: AfSouth1Infrastructure | 0/44 | 20:10:55 | ROLLBACK_IN_PROGRESS | AWS::CloudFormation::Stack | AfSouth1Infrastructure No export named EuWest1Infrastructure:userPoolArn found. Rollback requested by user.

I've tried using the value that gets outputted in the console but it returns the same error.

2

There are 2 best solutions below

0
On BEST ANSWER

To create a cross-region cross-stack reference, set the crossRegionReferences Stack prop to true and simply pass the reference around. Here is an example from the docs:

const stack1 = new Stack(app, 'Stack1', {
  env: {
    region: 'us-east-1',
  },
  crossRegionReferences: true,
});
const cert = new acm.Certificate(stack1, 'Cert', {
  domainName: '*.example.com',
  validation: acm.CertificateValidation.fromDns(route53.PublicHostedZone.fromHostedZoneId(stack1, 'Zone', 'Z0329774B51CGXTDQV3X')),
});

const stack2 = new Stack(app, 'Stack2', {
  env: {
    region: 'us-east-2',
  },
  crossRegionReferences: true,
});
new cloudfront.Distribution(stack2, 'Distribution', {
  defaultBehavior: {
    origin: new origins.HttpOrigin('example.com'),
  },
  domainNames: ['dev.example.com'],
  certificate: cert,
});

Here, the cert construct is simply passed around and the necessary cross-region references are created automatically. You can do the same with your UserPool.

This feature is experimental as of CDK 2.114.1

0
On

Problem 1: A CfnOutput doesn't create a cross-stack reference unless you set the exportName prop:

new CfnOutput(this, 'userPoolArn', {
value: this.userPoolPattern.userPoolArn,
exportName: 'UserPoolCrossStackRef', // <--
})

Within a single region you could then consume the value into another stack using cdk.Fn.importValue("UserPoolCrossStackRef").

Problem 2: Cross-stack references don't work across regions:

Docs: You can't create cross-stack references across regions. You can use the intrinsic function Fn::ImportValue to import only values that have been exported within the same region.