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(logs): delete associated log group when stack is deleted #21113

Merged
merged 34 commits into from
Aug 8, 2022

Conversation

YichenQian09
Copy link
Contributor

@YichenQian09 YichenQian09 commented Jul 12, 2022


Motivation: The log group created by LogRetention lingers after the stack is deleted. This commit allows users to choose whether to retain or destroy associated logs of the deleted stack.

Use case: To avoid having hundreds of dead log groups after the stack is deleted.

most salient design aspects:
Added an enum LogDeletionPolicy and set LogDeletionPolicy in the LogRetetion custom resource. Added an API deleteLogGroup and added an event 'Delete' to the handler of LogRetetion to delete the log group.

closes #11549

All Submissions:

Adding new Unconventional Dependencies:

  • This PR adds new unconventional dependencies following the process described here

New Features

  • Have you added the new feature to an integration test?
    • Did you use yarn integ to deploy the infrastructure and generate the snapshot (i.e. yarn integ without --dry-run)?

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

@gitpod-io
Copy link

gitpod-io bot commented Jul 12, 2022

@aws-cdk-automation aws-cdk-automation requested a review from a team July 12, 2022 22:14
@github-actions github-actions bot added the p2 label Jul 12, 2022
@mergify mergify bot dismissed TheRealAmazonKendra’s stale review July 21, 2022 22:53

Pull request has been modified.

Copy link
Contributor

@TheRealAmazonKendra TheRealAmazonKendra 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 your responses. Please see my additional comments below.

@TheRealAmazonKendra
Copy link
Contributor

I realized that I jumped right into this review without also saying thank you for your contribution! This feature will be super helpful so we appreciate you taking it on!

@mergify mergify bot dismissed TheRealAmazonKendra’s stale review July 22, 2022 22:33

Pull request has been modified.

@YichenQian09 YichenQian09 marked this pull request as ready for review July 23, 2022 20:47
@mergify mergify bot dismissed TheRealAmazonKendra’s stale review August 5, 2022 19:25

Pull request has been modified.

Copy link
Contributor

@TheRealAmazonKendra TheRealAmazonKendra left a comment

Choose a reason for hiding this comment

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

Just a few minor things, mostly formatting, but also the arn for long groups wasn't quite right. Neediness that, I think this looks ready to go.

packages/@aws-cdk/aws-logs/README.md Outdated Show resolved Hide resolved
@@ -93,6 +99,8 @@ export class LogRetention extends Construct {
base: retryOptions.base?.toMilliseconds(),
} : undefined,
RetentionInDays: props.retention === RetentionDays.INFINITE ? undefined : props.retention,
//only add RemovalPolicy if it is set to DESTROY
RemovalPolicy: props.removalPolicy && props.removalPolicy === cdk.RemovalPolicy.DESTROY ? props.removalPolicy : undefined,
Copy link
Contributor

Choose a reason for hiding this comment

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

There's no reason here to not just pass props.removalPolicy here.

Suggested change
RemovalPolicy: props.removalPolicy && props.removalPolicy === cdk.RemovalPolicy.DESTROY ? props.removalPolicy : undefined,
RemovalPolicy: props.removalPolicy,

Copy link
Contributor Author

Choose a reason for hiding this comment

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

When I wrote this line I thought only add RemovalPolicy if it is set to DESTROY(not add it if it is undefined or set RETAIN), so it is more reasonable to set it as it is?

Copy link
Contributor

Choose a reason for hiding this comment

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

You're later checking against RemovalPolicy.DESTROY, which kind of does make it so that you don't need RemovalPolicy.RETAIN but there's no reason to add this extra logic here. It's better to just pass the value as input by the user. You're not getting any benefit from the extra logic and it opens up the possibility of more failure points when more logic is introduced.

resources: [cdk.Stack.of(this).formatArn({
service: 'logs',
resource: 'log-group',
resourceName: props.logGroupName+':*',
Copy link
Contributor

@TheRealAmazonKendra TheRealAmazonKendra Aug 6, 2022

Choose a reason for hiding this comment

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

Suggested change
resourceName: props.logGroupName+':*',
resourceName: `${props.logGroupName}:*`,

Copy link
Contributor Author

@YichenQian09 YichenQian09 Aug 8, 2022

Choose a reason for hiding this comment

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

After I removed the ':*', I manually deploy and destroy the stack in integ.log-retention.ts. However, the destroy failed, the error message is: the loggroup is not allowed to delete. After I added the ':*' again, the destroy succeeded. I found the arn of log group is 'arn:aws:logs:us-east-1:account-id:log-group:logRetentionLogGroup:*' from the cloud watch
2b1fbfa6bc14d14ad85faf66195dce0
fd2d01cdbe057bb4fceff4eab18905e

.

Copy link
Contributor

Choose a reason for hiding this comment

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

OK. I was incorrect here, but let's at least us better string formatting. See my edited suggestion.

packages/@aws-cdk/aws-logs/lib/log-retention.ts Outdated Show resolved Hide resolved
@mergify mergify bot dismissed TheRealAmazonKendra’s stale review August 8, 2022 15:45

Pull request has been modified.

Copy link
Contributor

@TheRealAmazonKendra TheRealAmazonKendra left a comment

Choose a reason for hiding this comment

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

@YichenQian09 Just a few more issues/updates from last review. This is very much almost ready to go.

packages/@aws-cdk/aws-logs/test/log-retention.test.ts Outdated Show resolved Hide resolved
@@ -93,6 +99,8 @@ export class LogRetention extends Construct {
base: retryOptions.base?.toMilliseconds(),
} : undefined,
RetentionInDays: props.retention === RetentionDays.INFINITE ? undefined : props.retention,
//only add RemovalPolicy if it is set to DESTROY
RemovalPolicy: props.removalPolicy && props.removalPolicy === cdk.RemovalPolicy.DESTROY ? props.removalPolicy : undefined,
Copy link
Contributor

Choose a reason for hiding this comment

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

You're later checking against RemovalPolicy.DESTROY, which kind of does make it so that you don't need RemovalPolicy.RETAIN but there's no reason to add this extra logic here. It's better to just pass the value as input by the user. You're not getting any benefit from the extra logic and it opens up the possibility of more failure points when more logic is introduced.

resources: [cdk.Stack.of(this).formatArn({
service: 'logs',
resource: 'log-group',
resourceName: props.logGroupName+':*',
Copy link
Contributor

Choose a reason for hiding this comment

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

OK. I was incorrect here, but let's at least us better string formatting. See my edited suggestion.

@mergify mergify bot dismissed TheRealAmazonKendra’s stale review August 8, 2022 17:48

Pull request has been modified.

Co-authored-by: Kendra Neil <[email protected]>
Copy link
Contributor

@TheRealAmazonKendra TheRealAmazonKendra left a comment

Choose a reason for hiding this comment

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

Looks good! Thanks for all your work on this!

@mergify
Copy link
Contributor

mergify bot commented Aug 8, 2022

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

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

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

@mergify mergify bot merged commit 2bdd504 into aws:main Aug 8, 2022
@mergify
Copy link
Contributor

mergify bot commented Aug 8, 2022

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@robertd
Copy link
Contributor

robertd commented Aug 8, 2022

@YichenQian09 This is great PR... Now I just need to track down all the orphaned Log Groups pre-this-PR lol. Looking forward to the next cdk release. 👍

@YichenQian09
Copy link
Contributor Author

YichenQian09 commented Aug 8, 2022

@TheRealAmazonKendra
Thanks for all your reviews, comments and patient help!

jmortlock pushed a commit to jmortlock/aws-cdk that referenced this pull request Aug 8, 2022
)

----
Motivation: The log group created by LogRetention lingers after the stack is deleted. This commit allows users to choose whether to retain or destroy associated logs of the deleted stack.

Use case: To avoid having hundreds of dead log groups after the stack is deleted.

most salient design aspects:
Added an enum LogDeletionPolicy and set LogDeletionPolicy in the LogRetetion custom resource. Added an API deleteLogGroup and added an event 'Delete' to the handler of LogRetetion to delete the log group.

closes [aws#11549](aws#11549)

### All Submissions:

* [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md)

### Adding new Unconventional Dependencies:

* [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-new-unconventional-dependencies)

### New Features

* [x] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)?
	* [x] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)?

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
@tmokmss
Copy link
Contributor

tmokmss commented Aug 9, 2022

Cool! Now I guess we need to add LogRemovalPolicy prop to Lambda function construct just like LogRetention.

josephedward pushed a commit to josephedward/aws-cdk that referenced this pull request Aug 30, 2022
)

----
Motivation: The log group created by LogRetention lingers after the stack is deleted. This commit allows users to choose whether to retain or destroy associated logs of the deleted stack.

Use case: To avoid having hundreds of dead log groups after the stack is deleted.

most salient design aspects:
Added an enum LogDeletionPolicy and set LogDeletionPolicy in the LogRetetion custom resource. Added an API deleteLogGroup and added an event 'Delete' to the handler of LogRetetion to delete the log group.

closes [aws#11549](aws#11549)

### All Submissions:

* [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md)

### Adding new Unconventional Dependencies:

* [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-new-unconventional-dependencies)

### New Features

* [x] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)?
	* [x] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)?

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

stec00 commented Sep 11, 2022

Am I right in my understanding that this PR doesn't actually change anything that can be done in CDK as there isn't a property that can be set on a lambda to change the associated log group's removal policy? So it further requires #21804 to be done in order to achieve the statement of "This commit allows users to choose whether to retain or destroy associated logs of the deleted stack."?

@YichenQian09
Copy link
Contributor Author

Am I right in my understanding that this PR doesn't actually change anything that can be done in CDK as there isn't a property that can be set on a lambda to change the associated log group's removal policy? So it further requires #21804 to be done in order to achieve the statement of "This commit allows users to choose whether to retain or destroy associated logs of the deleted stack."?

This PR allows automatically deleting log groups created by logRetention by setting RemovalPolicy. The logGroup created by the lambda function will not be deleted. For example, if we create a logGroup named logRetentionLogGroup using logRetention and set removalPolicy to “destroy”, the second logGroup in the picture will be deleted, but the first will not. Please reply to this if I misunderstand your question or if you have further questions. Thanks!
image

@stec00
Copy link

stec00 commented Oct 13, 2022

Thanks for your reply @YichenQian09. So as I now understand it, this PR will allow Log Groups that are explictly defined in the CDK script to be deleted as part of a stack delete (assuming removalPolicy="destroy"). But a Log Group that is implicitly created (e.g. due the presence of a Lambda function in the script) will not be deleted - hence why #21804 is still open. Hope that's right - thanks for clarifying!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

(logs): delete associated log group when stack is deleted
8 participants