Skip to content

fix(s3): avoid access denied when disabling autoDeleteObjects property#37303

Open
Adityakk9031 wants to merge 3 commits intoaws:mainfrom
Adityakk9031:#37257
Open

fix(s3): avoid access denied when disabling autoDeleteObjects property#37303
Adityakk9031 wants to merge 3 commits intoaws:mainfrom
Adityakk9031:#37257

Conversation

@Adityakk9031
Copy link
Copy Markdown

Description:

Fixes #37257

What this PR does / why we need it: When autoDeleteObjects is changed from true to false on an existing bucket, CloudFormation removes the custom Bucket Policy before it fires the Custom Resource deletion event. This causes the Lambda to fail with an AccessDenied error when it attempts to call s3:GetBucketTagging, since its execution role lacks that permission.

This PR fixes the race condition in two complementary ways as suggested in the issue:

Defensive handler fix: Updates

isBucketTaggedForDeletion()
to catch AccessDenied and safely return false, which skips the empty-bucket operation since the policy (and tag) are already confirmed gone.
Architecture fix: Explicitly grants s3:GetBucketTagging directly to the Custom Resource Lambda execution IAM role so it isn't solely dependent on the bucket policy for this call.

Disabling autoDeleteObjects removes the bucket policy before the custom resource deletes the bucket, removing the s3:GetBucketTagging permission and causing an AccessDenied error.

This includes two fixes:
1. The Lambda explicitly catches AccessDenied when checking tags, assuming the bucket is untagged.
2. The custom resource explicitly adds s3:GetBucketTagging to the Lambda execution IAM role.
@github-actions github-actions bot added bug This issue is a bug. effort/medium Medium work item – several days of effort p1 labels Mar 22, 2026
@aws-cdk-automation aws-cdk-automation requested a review from a team March 22, 2026 18:09
@github-actions github-actions bot added the beginning-contributor [Pilot] contributed between 0-2 PRs to the CDK label Mar 22, 2026
Copy link
Copy Markdown
Collaborator

@aws-cdk-automation aws-cdk-automation left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pull request linter fails with the following errors:

❌ Fixes must contain a change to an integration test file and the resulting snapshot.

If you believe this pull request should receive an exemption, please comment and provide a justification. A comment requesting an exemption should contain the text Exemption Request. Additionally, if clarification is needed, add Clarification Request to a comment.

@Adityakk9031 Adityakk9031 changed the title fix(s3): AccessDenied when disabling autoDeleteObjects (#37257) fix(s3): avoid access denied when disabling autoDeleteObjects property Mar 22, 2026
@aws-cdk-automation aws-cdk-automation added the pr/needs-further-review PR requires additional review from our team specialists due to the scope or complexity of changes. label Mar 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

beginning-contributor [Pilot] contributed between 0-2 PRs to the CDK bug This issue is a bug. effort/medium Medium work item – several days of effort p1 pr/needs-further-review PR requires additional review from our team specialists due to the scope or complexity of changes.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

s3.Bucket: AccessDenied when disabling autoDeleteObjects property

2 participants