aws / aws/aws-cdk

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

Open
#27,902 20 comments 19 reactions 0 assignees View on GitHub
@aws-cdk/core bug effort/medium p1
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

Description

### 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:
- several closed issues reference cross-region exports (https://github.com/aws/aws-cdk/issues/24464, https://github.com/aws/aws-cdk/issues/25114), but these concern creating exports not deleting them
- many closed issues reference removing cross-stack exports (https://github.com/aws/aws-cdk/issues/7602), but cross-stack exports that are not also cross-region don't use the ExportsWriter lambda and can't produce the failure mode
- the [Stack.export_value](https://docs.aws.amazon.com/cdk/api/v2/python/aws_cdk/Stack.html#aws_cdk.Stack.export_value) function does not export values across regions, only across stacks, so it can't be used to create a "dummy" cross-region export for this ExportsWriter lambda

### 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_

Contributor guide

Open the contributing guide

Research direction

Start with the ExportsWriter custom resource and the CrossRegionExportReader resource described in the reproduction, then run the two-stack cross-region export scenario with `cdk diff` and exclusive deploys. Trace how removed exports are checked against active imports; done means deployment rejects only exports still imported by another stack, while unused removals succeed.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, typescript
Domain
cloud, infrastructure
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.