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

(custom-resources): CustomResourceProvider should destroy log group when stack deleted #26553

Open
2 tasks
hakanson opened this issue Jul 28, 2023 · 5 comments
Open
2 tasks
Labels
@aws-cdk/custom-resources Related to AWS CDK Custom Resources feature-request A feature should be added or improved. p3

Comments

@hakanson
Copy link

Describe the feature

Delete Log Groups created by auto-created a CustomResourceProvider when stack is destroyed. These are sometimes an "implementation detail" of constructs like s3.Bucket, so a CDK user may not be aware they need to clean them up.

Use Case

My CDK created an s3.Bucket with autoDeleteObjects: true, and when I destroy the stack, I noticed there was still a CloudWatch Log Group not cleaned up.

[/aws/lambda/XXXXXXXXXXXStack-CustomS3AutoDeleteObjectsCustomRe-xxxxxxxxxxxx

This appears to be a result of the auto-created Log Group for the CustomResourceProvider Lambda. This leaves cruft in the AWS account, which the user didn't intent to create.

Proposed Solution

after the CustomResourceProvider 'AWS::Lambda::Function' is created, also create the "AWS::Logs::LogGroup" and set RemovalPolicy to destroy

https://github.com/aws/aws-cdk/blob/main/packages/aws-cdk-lib/core/lib/custom-resource-provider/custom-resource-provider.ts#L307

Other Information

No response

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

CDK version used

2.88.0 (build 5d497f9)

Environment details (OS name and version, etc.)

macOS 12.6.6

@hakanson hakanson added feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged. labels Jul 28, 2023
@github-actions github-actions bot added the @aws-cdk/custom-resources Related to AWS CDK Custom Resources label Jul 28, 2023
@peterwoodworth
Copy link
Contributor

Can you use LogRetention as described here? #26538 (comment)

@peterwoodworth peterwoodworth added response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. and removed needs-triage This issue or PR still needs to be triaged. labels Jul 28, 2023
@hakanson
Copy link
Author

Since I don't create the custom resource, but the s3.Bucket does, I'm not sure how to find the CustomS3AutoDeleteObjectsCustomResourceProviderHandlerXXXXXXXX node (?) to change LogRetention. Any tips n that. I do see the aws:cdk:path is deterministic.

   "Metadata": {
    "aws:cdk:path": "MyStack/Custom::S3AutoDeleteObjectsCustomResourceProvider/Handler",

@github-actions github-actions bot removed the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Jul 29, 2023
@peterwoodworth
Copy link
Contributor

peterwoodworth commented Aug 3, 2023

You can view the construct path in cdk.out/tree.json

Alternative to this, you can try creating an AwsCustomResource which makes the delete log group API call on stack deletion

@peterwoodworth peterwoodworth added the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Aug 3, 2023
@hakanson
Copy link
Author

hakanson commented Aug 3, 2023

I don't know what to do with that last comment. I found the "Custom::S3AutoDeleteObjectsCustomResourceProvider" in the tree.json but the s3.bucket creates it. Is there an CDK api to find this node by id or by path, then navigate from the children to the handler? This should probably be handled by s3.bucket`, but I'm looking for an escape hatch to fix what was created. How would creating my own custom resource help?

          "Custom::S3AutoDeleteObjectsCustomResourceProvider": {
            "id": "Custom::S3AutoDeleteObjectsCustomResourceProvider",
            "path": "MyStack/Custom::S3AutoDeleteObjectsCustomResourceProvider",
            "children": {
              "Handler": {
                "id": "Handler",
                "path": "MyStack/Custom::S3AutoDeleteObjectsCustomResourceProvider/Handler",
                "constructInfo": {
                  "fqn": "aws-cdk-lib.CfnResource",
                  "version": "2.88.0"
                }
              }

@github-actions github-actions bot removed the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Aug 4, 2023
@amid0
Copy link

amid0 commented Aug 28, 2023

Faced with the same issue.
For lambda function for now resolved with manually creating LogGroup for each lambda.
But for library custom resources (such as S3 auto delete or Bucket deployment) - still an issue.
There is an idea of how to solve this - its customer resource on stack delete will go through all lambda functions in stack -> construct log group name for each -> delete if exists.
And this customer resource probably should be executed last one on stack destroy.
For now, I do not know if it is possible to tell this custom resource to be executed last on destroy and if all function names (that potentially already deleted, at the moment custom resource execution? function names available)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/custom-resources Related to AWS CDK Custom Resources feature-request A feature should be added or improved. p3
Projects
None yet
Development

No branches or pull requests

4 participants