Skip to content

Commit

Permalink
fix(s3): auto-delete fails when bucket has been deleted manually (#16645
Browse files Browse the repository at this point in the history
)

Even though buckets are not supposed to be deleted manually, this change makes the delete operation idempotent and thus more reliable.

Fixes #16619.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
otaviomacedo authored Oct 8, 2021
1 parent c3417f6 commit 7b4fa72
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,14 @@ async function onDelete(bucketName?: string) {
process.stdout.write(`Bucket does not have '${AUTO_DELETE_OBJECTS_TAG}' tag, skipping cleaning.\n`);
return;
}
await emptyBucket(bucketName);
try {
await emptyBucket(bucketName);
} catch (e) {
if (e.code !== 'NoSuchBucket') {
throw e;
}
// Bucket doesn't exist. Ignoring
}
}

/**
Expand Down
17 changes: 17 additions & 0 deletions packages/@aws-cdk/aws-s3/test/auto-delete-objects-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,23 @@ test('delete event where bucket has many objects does recurse appropriately', as
});
});

test('does nothing when the bucket does not exist', async () => {
// GIVEN
mockS3Client.promise.mockRejectedValue({ code: 'NoSuchBucket' });

// WHEN
const event: Partial<AWSLambda.CloudFormationCustomResourceDeleteEvent> = {
RequestType: 'Delete',
ResourceProperties: {
ServiceToken: 'Foo',
BucketName: 'MyBucket',
},
};
await invokeHandler(event);

expect(mockS3Client.deleteObjects).not.toHaveBeenCalled();
});

// helper function to get around TypeScript expecting a complete event object,
// even though our tests only need some of the fields
async function invokeHandler(event: Partial<AWSLambda.CloudFormationCustomResourceEvent>) {
Expand Down

0 comments on commit 7b4fa72

Please sign in to comment.