Skip to content

Commit

Permalink
allow setAutoDeleteObjectsLogGroup to be called before bucket creation
Browse files Browse the repository at this point in the history
  • Loading branch information
samson-keung committed Jun 11, 2024
1 parent 25d6fb2 commit f47255a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
7 changes: 1 addition & 6 deletions packages/aws-cdk-lib/aws-s3/lib/bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1898,12 +1898,7 @@ export class Bucket extends BucketBase {
* @param logGroup the log group to use on the lambda.
*/
public static setAutoDeleteObjectsLogGroup(scope: Construct, logGroup: logs.ILogGroup): void {
const provider = AutoDeleteObjectsProvider.getProvider(scope, AUTO_DELETE_OBJECTS_RESOURCE_TYPE);

if (provider === undefined) {
throw new Error('Requires at least one bucket with \'autoDeleteObjects: true\'. None is found.');
}
provider.configureLambdaLogGroup(logGroup.logGroupName);
AutoDeleteObjectsProvider.useLogGroup(scope, AUTO_DELETE_OBJECTS_RESOURCE_TYPE, logGroup.logGroupName);
}

public readonly bucketArn: string;
Expand Down
37 changes: 26 additions & 11 deletions packages/aws-cdk-lib/aws-s3/test/bucket.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3494,22 +3494,14 @@ describe('bucket', () => {
})).toThrow(/Cannot use \'autoDeleteObjects\' property on a bucket without setting removal policy to \'DESTROY\'/);
});

test('setAutoDeleteObjectsLogGroup throws if no bucket has autoDeleteObjects: true in stack', () => {
const stack = new cdk.Stack();

new s3.Bucket(stack, 'MyBucket', { });

expect(() => s3.Bucket.setAutoDeleteObjectsLogGroup(stack, new logs.LogGroup(stack, 'MyLogGroup', {})))
.toThrow(/Requires at least one bucket with 'autoDeleteObjects: true'. None is found./);
});

test('setAutoDeleteObjectsLogGroup to update AutoDeleteObjectsProvider LoggingConfig', () => {
test('setAutoDeleteObjectsLogGroup to update AutoDeleteObjectsProvider LoggingConfig after Bucket creation', () => {
const stack = new cdk.Stack();

new s3.Bucket(stack, 'MyBucket', {
removalPolicy: cdk.RemovalPolicy.DESTROY,
autoDeleteObjects: true,
});

s3.Bucket.setAutoDeleteObjectsLogGroup(stack, new logs.LogGroup(stack, 'FirstLogGroup', {
logGroupName: 'MyFirstLogGroup',
}));
Expand All @@ -3526,16 +3518,39 @@ describe('bucket', () => {
});
});

test('setAutoDeleteObjectsLogGroup multiple times should take the latest Log Group', () => {
test('setAutoDeleteObjectsLogGroup before Bucket creation', () => {
const stack = new cdk.Stack();

s3.Bucket.setAutoDeleteObjectsLogGroup(stack, new logs.LogGroup(stack, 'FirstLogGroup', {
logGroupName: 'MyFirstLogGroup',
}));
new s3.Bucket(stack, 'MyBucket', {
removalPolicy: cdk.RemovalPolicy.DESTROY,
autoDeleteObjects: true,
});

Template.fromStack(stack).hasResourceProperties('AWS::Logs::LogGroup', {
LogGroupName: 'MyFirstLogGroup',
});
Template.fromStack(stack).hasResourceProperties('AWS::Lambda::Function', {
LoggingConfig: {
LogGroup: {
'Ref': 'FirstLogGroupFF5C2AA0',
},
},
});
});

test('setAutoDeleteObjectsLogGroup multiple times should take the latest Log Group', () => {
const stack = new cdk.Stack();

s3.Bucket.setAutoDeleteObjectsLogGroup(stack, new logs.LogGroup(stack, 'FirstLogGroup', {
logGroupName: 'MyFirstLogGroup',
}));
new s3.Bucket(stack, 'MyBucket', {
removalPolicy: cdk.RemovalPolicy.DESTROY,
autoDeleteObjects: true,
});
s3.Bucket.setAutoDeleteObjectsLogGroup(stack, new logs.LogGroup(stack, 'SecondLogGroup', {
logGroupName: 'MySecondLogGroup',
}));
Expand Down

0 comments on commit f47255a

Please sign in to comment.