addEventNotification causes "NoSuchBucket" errors during stack deletion regardless of bucket RemovalPolicy
- Dominant language
- TypeScript
- Stars
- 12.9k
- Forks
- 4.6k
- Avg merge
- 2d 3h
- Merged PRs (30d)
- 83
Description
### Describe the bug
When using `bucket.addEventNotification()` to configure S3 bucket notifications, CloudFormation stack deletion fails with a `NoSuchBucket` error. This happens because the custom resource that manages notifications tries to clear them **AFTER** CloudFormation has already processed the bucket deletion (even with `RemovalPolicy.RETAIN`).
This makes it extremely difficult to reliably delete stacks containing S3 buckets with event notifications without resorting to complex workarounds.
This issue makes it impossible to reliably delete stacks containing S3 buckets with event notifications, forcing developers to use complex workarounds or manual cleanup procedures. This is especially problematic in CI/CD environments where automated cleanup is essential.
### Regression Issue
- [ ] Select this option if this issue appears to be a regression.
### Last Known Working CDK Library Version
_No response_
### Expected Behavior
CloudFormation should clear the bucket notification configuration BEFORE trying to "delete" (or mark as deleted in its tracking) the S3 bucket, regardless of the bucket's removal policy.
### Current Behavior
When trying to deleting the stack, the Custom Resource created by the bucket.addEventNotification function can't be deleted with the following error message:
Received response status [FAILED] from custom resource. Message returned: Error: An error occurred (NoSuchBucket) when calling the PutBucketNotificationConfiguration operation: The specified bucket does not exist.
### Reproduction Steps
1. Create an S3 bucket with any `removalPolicy` (`DESTROY`, `RETAIN`, or `SNAPSHOT`)
2. Add an event notification using `bucket.addEventNotification()`
3. Attempt to delete the CloudFormation stack
## Code Example
```
const bucket = new s3.Bucket(this, "CommunityS3", {
accessControl: s3.BucketAccessControl.PRIVATE,
removalPolicy: cdk.RemovalPolicy.RETAIN
versioned: true,
});
// This causes problems during stack deletion
bucket.addEventNotification(
s3.EventType.OBJECT_CREATED,
new s3n.SqsDestination(queue),
{ prefix: "groupvideos/original/" }
);
```
### Possible Solution
_No response_
### Additional Information/Context
_No response_
### AWS CDK Library version (aws-cdk-lib)
2.202.0
### AWS CDK CLI version
2.121.1
### Node.js Version
22
### OS
Ubuntu (WSL)
### Language
TypeScript
### Language Version
_No response_
### Other information
_No response_
Contributor guide
Research direction
Start at the TypeScript implementation behind bucket.addEventNotification and trace the custom resource's CloudFormation deletion handling. Reproduce stack deletion with DESTROY, RETAIN, and SNAPSHOT policies, then inspect or add coverage for notification cleanup when the bucket has already been removed. Done means deletion completes without a NoSuchBucket error for each policy.
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
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100