aws-cdk: (core): unused cross-region exports can't be deleted

Describe the bug

If a stack maintains multiple cross-region exports, and some subset of cross-region exports are being imported by other stacks, attempting to remove any cross-region export value that is not being used (imported) by any deployed stack:

❯ cdk diff [...] --exclusively
[...]
[~] Custom::CrossRegionExportWriter ExportsWriteruswest2[...]
ExportsWriteruswest2[...]
 └─ [~] WriterProps
     └─ [~] .exports:
         └─ [-] Removed: ./cdk/exports/unused-value/anotherstackuseast1Ref[...]certificate[...]

Fails with the following error on cdk deploy:

ExportsWriteruswest2[...]/Resource/Default (ExportsWriteruswest2[...]) Received response status [FAILED] from custom resource. Message returned: Error: Exports cannot be updated: 

    at throwIfAnyInUse (/var/task/index.js:4:10)

Other issues don’t describe this bug:

Expected Behavior

CDK should fail to deploy with the error above only if the cdk diff shows an export being removed that is currently imported by another stack.

Current Behavior

CDK fails to deploy with the error above even when cdk diff shows only exports being removed that are not currently imported by another stack.

Reproduction Steps

  1. Create two cross-region exports, exported from stack A into another stack B in a different region.
  2. Remove one of the two imports from stack B and deploy stack B exclusively. Observe that one import is removed from stack B’s ["CrossRegionExportReader"]["ReaderProps"] template in the CloudFormation console.
  3. Attempt to deploy stack A. Observe one of the two exports being removed in the diff. Observe that on the deploy, the ExportWriter fails to update and the deploy fails with the error above.

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.94.0 (build 987c329)

Framework Version

No response

Node.js Version

v18.17.1

OS

macOS

Language

Python

Language Version

Python (3.10.4)

Other information

No response

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 16
  • Comments: 19 (5 by maintainers)

Most upvoted comments

This is giving us headaches as well. Any update would be immensely helpful. Thanks!

thanks @shahin

@khushail I made a reproduction repo please let me know if you have any questions

https://github.com/renschler/cross-region-delete

@TheRealAmazonKendra I made a reproduction repo here; if anyone has issues reproducing let me know and I can help explain!

ps as a warning to others, don’t delete those unnecessary SSM paramters, it will create more issues! I’m now having to re-add them manually to SSM in every region where I deleted them. then after manually re-adding you’ll hit a “last modified date does not match with the last modified date of the retrieved parameters” error during deploy, the only way to fix was to run cdk synth and then manually update the stacks in the aws console using the generated json template in cdk.out/