-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
custom-resource-handlers/aws-s3/auto-delete-objects-handler: Object locked objects prevent cdk destroy #32147
Comments
Easily reproducible using provided code (assuming that TerminationProtection is not enabled for stack). Running
Per Retention Modes section at Locking objects with Object Lock:
Also, the error from custom resource handler is IAmazonS3 s3Client = new AmazonS3Client();
string bucketName = "<<some-bucket>>";
var listVersionsRequest = new ListVersionsRequest
{
BucketName = bucketName
};
ListVersionsResponse listVersionsResponse;
// Iterate through the objects in the bucket and delete them.
do
{
// List all the versions of all the objects in the bucket.
listVersionsResponse = s3Client.ListVersions(listVersionsRequest);
if (listVersionsResponse.Versions.Count == 0)
{
// If the bucket has no objects break the loop.
break;
}
var keyVersionList = new List<KeyVersion>(listVersionsResponse.Versions.Count);
for (int index = 0; index < listVersionsResponse.Versions.Count; index++)
{
keyVersionList.Add(new KeyVersion
{
Key = listVersionsResponse.Versions[index].Key,
VersionId = listVersionsResponse.Versions[index].VersionId
});
}
try
{
// Delete the current set of objects.
var deleteObjectsResponse = s3Client.DeleteObjects(new DeleteObjectsRequest
{
BucketName = bucketName,
Objects = keyVersionList,
BypassGovernanceRetention = true
});
}
catch
{
}
// Set the markers to get next set of objects from the bucket.
listVersionsRequest.KeyMarker = listVersionsResponse.NextKeyMarker;
listVersionsRequest.VersionIdMarker = listVersionsResponse.NextVersionIdMarker;
}
// Continue listing objects and deleting them until the bucket is empty.
while (listVersionsResponse.IsTruncated); |
Describe the bug
When an s3 bucket is created with object lock governance and the s3 bucket is set for removal cdk destroy fails. This happens even when the role has permissions to bypassgovernancelock
This is because in the auto-delete-objects-handler the s3 delete objects is called without
BypassGovernanceRetention
Current:
await s3.deleteObjects({ Bucket: bucketName, Delete: { Objects: records } });
Regression Issue
Last Known Working CDK Version
No response
Expected Behavior
I expect for the cdk destroy to remove the governed objects provided the role has the necessary permissions
Current Behavior
The cdk destroy fails and the stack reverts back to its previous state
Reproduction Steps
Add some objects, then run cdk destroy
Possible Solution
await s3.deleteObjects({ Bucket: bucketName, Delete: { Objects: records }, BypassGovernanceRetention: true });
The above will work for governed objects however will fail for non object locked objects. For this reason a combination of the two will need to be used
Additional Information/Context
No response
CDK CLI Version
2.160.0 (build 7a8ae02)
Framework Version
No response
Node.js Version
v21.7.1
OS
MacOS 14.7.1
Language
TypeScript
Language Version
No response
Other information
No response
The text was updated successfully, but these errors were encountered: