-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(s3): option to auto delete objects upon bucket removal (#12090)
Use the custom resource provider from core to delete objects in the bucket. A bucket policy gives the correct permissions to the provider's Lambda function role. Credits to @Chriscbr for starting the work on this. Closes #3297 Closes #9751 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
- Loading branch information
Showing
8 changed files
with
776 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
packages/@aws-cdk/aws-s3/lib/auto-delete-objects-handler/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
import { S3 } from 'aws-sdk'; | ||
|
||
const s3 = new S3(); | ||
|
||
export async function handler(event: AWSLambda.CloudFormationCustomResourceEvent) { | ||
switch (event.RequestType) { | ||
case 'Create': | ||
case 'Update': | ||
return; | ||
case 'Delete': | ||
return onDelete(event); | ||
} | ||
} | ||
|
||
/** | ||
* Recursively delete all items in the bucket | ||
* | ||
* @param bucketName the bucket name | ||
*/ | ||
async function emptyBucket(bucketName: string) { | ||
const listedObjects = await s3.listObjectVersions({ Bucket: bucketName }).promise(); | ||
const contents = [...listedObjects.Versions ?? [], ...listedObjects.DeleteMarkers ?? []]; | ||
if (contents.length === 0) { | ||
return; | ||
}; | ||
|
||
const records = contents.map((record: any) => ({ Key: record.Key, VersionId: record.VersionId })); | ||
await s3.deleteObjects({ Bucket: bucketName, Delete: { Objects: records } }).promise(); | ||
|
||
if (listedObjects?.IsTruncated) { | ||
await emptyBucket(bucketName); | ||
} | ||
} | ||
|
||
async function onDelete(deleteEvent: AWSLambda.CloudFormationCustomResourceDeleteEvent) { | ||
const bucketName = deleteEvent.ResourceProperties?.BucketName; | ||
if (!bucketName) { | ||
throw new Error('No BucketName was provided.'); | ||
} | ||
await emptyBucket(bucketName); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
168 changes: 168 additions & 0 deletions
168
packages/@aws-cdk/aws-s3/test/auto-delete-objects-handler.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
const mockS3Client = { | ||
listObjectVersions: jest.fn().mockReturnThis(), | ||
deleteObjects: jest.fn().mockReturnThis(), | ||
promise: jest.fn(), | ||
}; | ||
|
||
import { handler } from '../lib/auto-delete-objects-handler'; | ||
|
||
jest.mock('aws-sdk', () => { | ||
return { S3: jest.fn(() => mockS3Client) }; | ||
}); | ||
|
||
beforeEach(() => { | ||
mockS3Client.listObjectVersions.mockReturnThis(); | ||
mockS3Client.deleteObjects.mockReturnThis(); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
test('does nothing on create event', async () => { | ||
// GIVEN | ||
const event: Partial<AWSLambda.CloudFormationCustomResourceCreateEvent> = { | ||
RequestType: 'Create', | ||
ResourceProperties: { | ||
ServiceToken: 'Foo', | ||
BucketName: 'MyBucket', | ||
}, | ||
}; | ||
|
||
// WHEN | ||
await invokeHandler(event); | ||
|
||
// THEN | ||
expect(mockS3Client.listObjectVersions).toHaveBeenCalledTimes(0); | ||
expect(mockS3Client.deleteObjects).toHaveBeenCalledTimes(0); | ||
}); | ||
|
||
test('does nothing on update event', async () => { | ||
// GIVEN | ||
const event: Partial<AWSLambda.CloudFormationCustomResourceUpdateEvent> = { | ||
RequestType: 'Update', | ||
ResourceProperties: { | ||
ServiceToken: 'Foo', | ||
BucketName: 'MyBucket', | ||
}, | ||
}; | ||
|
||
// WHEN | ||
await invokeHandler(event); | ||
|
||
// THEN | ||
expect(mockS3Client.listObjectVersions).toHaveBeenCalledTimes(0); | ||
expect(mockS3Client.deleteObjects).toHaveBeenCalledTimes(0); | ||
}); | ||
|
||
test('deletes no objects on delete event when bucket has no objects', async () => { | ||
// GIVEN | ||
mockS3Client.promise.mockResolvedValue({ Versions: [] }); // listObjectVersions() call | ||
|
||
// WHEN | ||
const event: Partial<AWSLambda.CloudFormationCustomResourceDeleteEvent> = { | ||
RequestType: 'Delete', | ||
ResourceProperties: { | ||
ServiceToken: 'Foo', | ||
BucketName: 'MyBucket', | ||
}, | ||
}; | ||
await invokeHandler(event); | ||
|
||
// THEN | ||
expect(mockS3Client.listObjectVersions).toHaveBeenCalledTimes(1); | ||
expect(mockS3Client.listObjectVersions).toHaveBeenCalledWith({ Bucket: 'MyBucket' }); | ||
expect(mockS3Client.deleteObjects).toHaveBeenCalledTimes(0); | ||
}); | ||
|
||
test('deletes all objects on delete event', async () => { | ||
// GIVEN | ||
mockS3Client.promise.mockResolvedValue({ // listObjectVersions() call | ||
Versions: [ | ||
{ Key: 'Key1', VersionId: 'VersionId1' }, | ||
{ Key: 'Key2', VersionId: 'VersionId2' }, | ||
], | ||
}); | ||
|
||
// WHEN | ||
const event: Partial<AWSLambda.CloudFormationCustomResourceDeleteEvent> = { | ||
RequestType: 'Delete', | ||
ResourceProperties: { | ||
ServiceToken: 'Foo', | ||
BucketName: 'MyBucket', | ||
}, | ||
}; | ||
await invokeHandler(event); | ||
|
||
// THEN | ||
expect(mockS3Client.listObjectVersions).toHaveBeenCalledTimes(1); | ||
expect(mockS3Client.listObjectVersions).toHaveBeenCalledWith({ Bucket: 'MyBucket' }); | ||
expect(mockS3Client.deleteObjects).toHaveBeenCalledTimes(1); | ||
expect(mockS3Client.deleteObjects).toHaveBeenCalledWith({ | ||
Bucket: 'MyBucket', | ||
Delete: { | ||
Objects: [ | ||
{ Key: 'Key1', VersionId: 'VersionId1' }, | ||
{ Key: 'Key2', VersionId: 'VersionId2' }, | ||
], | ||
}, | ||
}); | ||
}); | ||
|
||
test('delete event where bucket has many objects does recurse appropriately', async () => { | ||
// GIVEN | ||
mockS3Client.promise // listObjectVersions() call | ||
.mockResolvedValueOnce({ | ||
Versions: [ | ||
{ Key: 'Key1', VersionId: 'VersionId1' }, | ||
{ Key: 'Key2', VersionId: 'VersionId2' }, | ||
], | ||
IsTruncated: true, | ||
}) | ||
.mockResolvedValueOnce(undefined) // deleteObjects() call | ||
.mockResolvedValueOnce({ // listObjectVersions() call | ||
Versions: [ | ||
{ Key: 'Key3', VersionId: 'VersionId3' }, | ||
{ Key: 'Key4', VersionId: 'VersionId4' }, | ||
], | ||
}); | ||
|
||
// WHEN | ||
const event: Partial<AWSLambda.CloudFormationCustomResourceDeleteEvent> = { | ||
RequestType: 'Delete', | ||
ResourceProperties: { | ||
ServiceToken: 'Foo', | ||
BucketName: 'MyBucket', | ||
}, | ||
}; | ||
await invokeHandler(event); | ||
|
||
// THEN | ||
expect(mockS3Client.listObjectVersions).toHaveBeenCalledTimes(2); | ||
expect(mockS3Client.listObjectVersions).toHaveBeenCalledWith({ Bucket: 'MyBucket' }); | ||
expect(mockS3Client.deleteObjects).toHaveBeenCalledTimes(2); | ||
expect(mockS3Client.deleteObjects).toHaveBeenNthCalledWith(1, { | ||
Bucket: 'MyBucket', | ||
Delete: { | ||
Objects: [ | ||
{ Key: 'Key1', VersionId: 'VersionId1' }, | ||
{ Key: 'Key2', VersionId: 'VersionId2' }, | ||
], | ||
}, | ||
}); | ||
expect(mockS3Client.deleteObjects).toHaveBeenNthCalledWith(2, { | ||
Bucket: 'MyBucket', | ||
Delete: { | ||
Objects: [ | ||
{ Key: 'Key3', VersionId: 'VersionId3' }, | ||
{ Key: 'Key4', VersionId: 'VersionId4' }, | ||
], | ||
}, | ||
}); | ||
}); | ||
|
||
// 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>) { | ||
return handler(event as AWSLambda.CloudFormationCustomResourceEvent); | ||
} |
Oops, something went wrong.