-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
(lambda): Add property for log removal policy of Lambda function log groups #21804
Comments
Thanks for submitting this, we accept contributions! Check out our contributing guide if you're interested - there's a low chance the team will be able to address this soon but we'd be happy to review a PR 🙂 I think we would want to be able to set this new property even if |
Hi thanks! I agree with that so I'll submit a PR implementing 2nd option. |
For our use case, we need a method akin to |
I want to share with you my working solution to delete LogGroup of CustomResources during stack deletion. and thanks to @greensmith for the idea. Issue is related on CustomResource Constructor I guess. It creates CloudWatch log group for wrapper lambda. Even if I have defined a log group for the lambda explicitly (/aws/lambda/${lambdaFn.functionName}), this CustomResource Constructor overwrites my log group setting. (#8815) Workaround is:
declare const onEvent: lambda.Function;
onEvent.addToRolePolicy(new iam.PolicyStatement({
actions:['logs:CreateLogGroup'],
resources:['*'],
effect: iam.Effect.DENY
}));
new logs.LogGroup(this, 'log-group', {
logGroupName: `/aws/lambda/${onEvent.functionName}`,
removalPolicy: RemovalPolicy.DESTROY,
retention: logs.RetentionDays.ONE_DAY,
}) In this way, Lambda Wrapper will not be able to create/overwrite the same log group, but you can keep writing logs in the log group that you have defined as long as your stack is alive. This log group will be but destroyed with your stack deletion together. You can check out my repository to check whole code as an example. https://github.com/deloittepark/aws-serverless-golang/tree/main/cognito-react-runtime-config |
There is no mechanism in place to clean up CloudWatch Log Groups. While the logs itself can be set to expire, the Log Groups are kept forever. There isn't an elegant way to delete log groups within CDK on stack teardown currently (this may change in the future - see aws/aws-cdk#21804). This is a temporary workaround until a better solution is found. Note: A --no-paginate option is added to limit the number of queries (~50). This prevents rate limiting as well as stops a potentially endless number of results being returned by AWS (which would result in a very long-running workflow). This means only a maximum number of 50 log groups are removed each workflow run.
There is no mechanism in place to clean up CloudWatch Log Groups. While the logs itself can be set to expire, the Log Groups are kept forever. There isn't an elegant way to delete log groups within CDK on stack teardown currently (this may change in the future - see aws/aws-cdk#21804). This is a temporary workaround until a better solution is found. Note: A --no-paginate option is added to limit the number of queries (~50). This prevents rate limiting as well as stops a potentially endless number of results being returned by AWS (which would result in a very long-running workflow). This means only a maximum number of 50 log groups are removed each workflow run. Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
Describe the feature
#21113 introduced
removalPolicy
forLogRetention
custom resource to allow us to delete log groups inside a stack. However, because we don't have a corresponding property in Lambda Function construct, it seems we still cannot remove a log group for a Lambda function automatically when we delete a stack. (Sorry if I'm missing something)Use Case
Automatically remove log groups for lambda functions inside a stack when we delete it.
Proposed Solution
1st idea:
Add a property e.g.
logRetentionRemovalPolicy?
here:aws-cdk/packages/@aws-cdk/aws-lambda/lib/function.ts
Line 294 in 478b996
The property will be only valid when
logRetention
is set. There might be a better API for this but at least it should work and won't introduce any breaking change :(2nd Idea (which might have better DX):
Add a property like
autoDeleteLog?: boolean
.If users specify this, we internally create a logRetention with length of
logRetention
property orRetentionDays.INFINITY
if not specified, and setlogRetention.RemovalPolicy
todestroy
. By this we only have to setautoDeleteLog: true
when we just want to delete a log groups on removal of the function.Other Information
An aspect like below will not work either:
because we still need to configure IAM policy to allow the lambda to delete the log group, which is set here.
aws-cdk/packages/@aws-cdk/aws-logs/lib/log-retention.ts
Lines 162 to 183 in 478b996
It results in the bellow error:
Acknowledgements
CDK version used
2.39.0
Environment details (OS name and version, etc.)
macOS
The text was updated successfully, but these errors were encountered: