Commit dc5be98
authored
fix(s3): resolve S3 notification removal race condition (#37708)
## Issue
Closes #37667.
## Reason for this change
When removing a bucket notification from a multi-bucket stack, CloudFormation updates the handler's shared IAM policy to revoke the removed bucket's ARN before deleting the `Custom::S3BucketNotifications` resource. This causes the handler Lambda to fail with `AccessDenied` on `s3:PutBucketNotification` during cleanup.
The root cause is that `DependsOn` only controls create and delete ordering, not update ordering. When one of multiple buckets is removed, the shared `DefaultPolicy` is updated (not deleted), so the permission revocation happens before the custom resource cleanup runs.
## Description of changes
This fix replaces the shared `DefaultPolicy` with a dedicated `iam.Policy` per bucket notification. When a notification is removed, its dedicated policy is deleted rather than updated, which means CloudFormation correctly orders the custom resource deletion before the policy deletion via `DependsOn`.
**Files changed:**
- **notifications-resource.ts** - Core fix: creates a per-bucket `HandlerPolicy` via `attachInlinePolicy`
- **Unit tests** - Updated to expect the new `HandlerPolicy` in `DependsOn` arrays
## Describe any new or updated permissions being added
No new permissions added. The same S3 permissions are now granted via per-bucket inline policies instead of a shared `DefaultPolicy`.
## Description of how you validated changes
- All unit tests passing (473/473)
- All S3 notification-related tests passing
- Verified `DependsOn` behavior in test assertions
## Checklist
- [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md)
---
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license1 parent a78ddc5 commit dc5be98
138 files changed
Lines changed: 103487 additions & 53018 deletions
File tree
- packages
- @aws-cdk-testing/framework-integ/test
- aws-lambda-event-sources/test
- integ.s3.imported-bucket.js.snapshot
- integ.s3.js.snapshot
- aws-s3-notifications/test
- integ.notifications.js.snapshot
- lambda/integ.bucket-notifications.js.snapshot
- asset.b5c2a7158e57582e84496b24c0499302cec1fd88624535b3dfb629311b3fdc2a.bundle
- sns
- integ.encrypted-sns-bucket-notifications.js.snapshot
- asset.b5c2a7158e57582e84496b24c0499302cec1fd88624535b3dfb629311b3fdc2a.bundle
- integ.sns-bucket-notifications.js.snapshot
- sqs
- integ.bucket-notifications-sorting.js.snapshot
- integ.bucket-notifications-unmanaged.js.snapshot
- asset.b5c2a7158e57582e84496b24c0499302cec1fd88624535b3dfb629311b3fdc2a.bundle
- integ.bucket-notifications.js.snapshot
- asset.b5c2a7158e57582e84496b24c0499302cec1fd88624535b3dfb629311b3fdc2a.bundle
- aws-s3/test
- integ.bucket-notifications-multi-bucket-policy.js.snapshot
- asset.b5c2a7158e57582e84496b24c0499302cec1fd88624535b3dfb629311b3fdc2a.bundle
- integ.bucket.notifications-scoped-permissions.js.snapshot
- integ.bucket.notifications.js.snapshot
- @aws-cdk/mixins-preview/test/events/aws-s3/integ.events.js.snapshot
- aws-cdk-lib
- aws-s3-notifications/test
- lambda
- aws-s3
- lib/notifications-resource
- test
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 98 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
0 commit comments