Skip to content
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

feat(s3): add autoDeleteObjectsLogGroup to pass log group to custom resource lambda #30333

Conversation

samson-keung
Copy link
Contributor

Issue # (if applicable)

Closes #24815.

Reason for this change

To allow log group customization on the custom resource lambda created for the autoDeleteObjects feature.

Description of changes

There are 2 changes to implement the autoDeleteObjectsLogGroup prop:

  1. CustomResourceProviderBase is updated to accept an optional logGroupName prop. If it is defined, it will be passed to the AWS::Lambda::Function resource definition (namely, setting the LoggingConfig prop).
  2. s3.Bucket is updated to accept an optional autoDeleteObjectsLogGroup prop. The idea is to make the API experience similar to the logGroup prop on lambda.Function.

Description of how you validated changes

Unit tests have been added for both the s3.Bucket class and core.CustomResourceProvider class

Checklist


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@aws-cdk-automation aws-cdk-automation requested a review from a team May 24, 2024 21:35
@github-actions github-actions bot added effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. p1 labels May 24, 2024
@mergify mergify bot added the contribution/core This is a PR that came from AWS. label May 24, 2024
Copy link
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 has failed. See the aws-cdk-automation comment below for failure reasons. 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.

@aws-cdk-automation aws-cdk-automation dismissed their stale review May 25, 2024 01:36

✅ Updated pull request passes all PRLinter validations. Dismissing previous PRLinter review.

@samson-keung samson-keung force-pushed the customize-bucket-auto-delete-objects-lambda-logging branch from 0efae66 to e554487 Compare May 25, 2024 02:07
@samson-keung samson-keung marked this pull request as ready for review May 25, 2024 02:46
@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: 5a201cb
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@aws-cdk-automation aws-cdk-automation added the pr/needs-maintainer-review This PR needs a review from a Core Team Member label May 25, 2024
Copy link
Contributor

@aaythapa aaythapa left a comment

Choose a reason for hiding this comment

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

thanks for making this change!

I see from this #24815 (comment) comment from the linked issue that someone made a similar prototype and they faced this error.

An unexpected behavior was that the provider lambda was re-creating the log group after I got it to successfully delete itself on stack deletion, but the new log group came back with infinite duration (argh!). This is because the default role for the lambda has permission to create a log group (which was occurring when it issued a final log stream on delete). Fortunately policyStatements was already wired up so I just explicitly denied CreateLogGroup for the Lambda role.

Wondering if you faced something similar (I see similarities between the prototype and your change so just checking)

Comment on lines +1493 to +1495
*/
readonly autoDeleteObjectsLogGroup?: logs.ILogGroup;

Copy link
Contributor

Choose a reason for hiding this comment

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

what happens if autoDeleteObjectsLogGroup is set but autoDeleteObjects isn't set to true?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is ignored.

```ts
import { ILogGroup } from 'aws-cdk-lib/aws-logs';

declare const logGroup: ILogGroup;
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: I think its more valuable if you actually define a log group here. The log group from the integ test should do.

Suggested change
declare const logGroup: ILogGroup;
const logGroup = new logs.LogGroup(this, 'LogGroup', {
logGroupName: 'AutoDeleteObjectsLambdaLogs',
retention: logs.RetentionDays.THREE_DAYS,
}),

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My first revision was defining the log group explicitly. But I found the Lambda REAME that uses this style. But I agree defining the log group is more valuable to the reader. Will update

Comment on lines +3478 to +3485
Template.fromStack(stack).hasResourceProperties('AWS::Lambda::Function', {
LoggingConfig: {
LogGroup: {
'Ref': 'LogGroup106AAD846',
},
},
});
});
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: should we also be checking if the log group resource was created?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea. Will update

@samson-keung
Copy link
Contributor Author

samson-keung commented May 27, 2024

Wondering if you faced something similar (I see similarities between the prototype and you're change so just checking)

Let me try reproducing the issue and get back to you. From your call out, I realized the integ test is not generating expected template, namely, the LoggingConfig on the lambda isn't set.

@samson-keung samson-keung marked this pull request as draft May 27, 2024 16:38
@aws-cdk-automation aws-cdk-automation removed the pr/needs-maintainer-review This PR needs a review from a Core Team Member label May 27, 2024
@samson-keung
Copy link
Contributor Author

This PR has a problem with the integ test. Looking at the snapshot, it is apparent that the log group is created but it is not referenced anywhere in the template. The expected template should have the custom resource lambda referencing the log group like so:

  "CustomS3AutoDeleteObjectsCustomResourceProviderHandler9D90184F": {
   "Type": "AWS::Lambda::Function",
   "Properties": {
    // ...
    "LoggingConfig": {
     "LogGroup": {
      "Ref": "LogGroupF5B46931"
     }
    }
   }
  }

Upon further investigation, I have learned that the autoDeleteObjects prop creates a stack wide singleton provider lambda. Therefore, with this PR, the following happens:

const myLogGroupA = new LogGroup(...)
const bucket = new s3.Bucket(this, 'BucketA', {
  autoDeleteObjects: true, 
  autoDeleteObjectsLogGroup: myLogGroupA
});

// The above creates a singleton AutoDeleteObjectsProvider lambda and set the LoggingConfig on the lambda to use "myLogGroupA"

const myLogGroupB = new LogGroup(...)
const bucketThatWillBeRemoved = new s3.Bucket(this, 'BucketB', {
  autoDeleteObjects: true,
  autoDeleteObjectsLogGroup: myLogGroupB
});

// This second bucket DOES NOT create a new AutoDeleteObjectsProvider lambda. But rather it will retrieve and re-use the existing one. Hence, "myLogGroupB" is ignored

With the above, the approach in this PR creates a poor UX as user must remember to set the autoDeleteObjectsLogGroup on the very first bucket with autoDeleteObjects: true. And all subsequent buckets with autoDeleteObjectsLogGroup will be ignored quietly. Due to this, I am abandoning this approach and closing this PR. Please refer to the issue for further updates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contribution/core This is a PR that came from AWS. effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. p1
Projects
None yet
Development

Successfully merging this pull request may close these issues.

(aws-s3): retention on S3 autoDeleteObjects lambda CloudWatch log group is Never expire
3 participants