(lambda): Add property for log removal policy of Lambda function log groups
- Dominant language
- TypeScript
- Stars
- 12.9k
- Forks
- 4.6k
- Avg merge
- 2d 3h
- Merged PRs (30d)
- 83
Description
### Describe the feature
#21113 introduced `removalPolicy` for `LogRetention` 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](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_lambda.Function.html#logretention), 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:
https://github.com/aws/aws-cdk/blob/478b9967f4a5814d36472ecee5fc7157e3c2ad74/packages/%40aws-cdk/aws-lambda/lib/function.ts#L294
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 or `RetentionDays.INFINITY` if not specified, and set `logRetention.RemovalPolicy` to `destroy`. By this we only have to set `autoDeleteLog: 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:
```ts
class SetLogGroupRemovalPolicy implements IAspect {
public visit(node: CfnResource): void {
if (node.cfnResourceType == 'Custom::LogRetention') {
node.addOverride('Properties.RemovalPolicy', 'destroy');
}
}
}
```
because we still need to configure IAM policy to allow the lambda to delete the log group, which is set here.
https://github.com/aws/aws-cdk/blob/478b9967f4a5814d36472ecee5fc7157e3c2ad74/packages/%40aws-cdk/aws-logs/lib/log-retention.ts#L162-L183
It results in the bellow error:
```
10:22:50 PM | DELETE_FAILED | Custom::LogRetention | HandlerLogRetention34184093
Received response status [FAILED] from custom resource. Message returned: User: arn:aws:sts::1:assumed-role/Stack-LogRetentionaae0aa3c
5b4d4f87b02d85b2-1XVR8ES6GII3X/Stack-LogRetentionaae0aa3c5b4d4f87b02d85b2-QJvljA9zM3Gp is not authorized to perform: logs:DeleteLogGroup on resou
rce: arn:aws:logs:ap-northeast-1:123456789012:log-group:/aws/lambda/Stack-Handler886CB40B-WSAN0fvgsbiN:log-stream: because no identity-based poli
cy allows the logs:DeleteLogGroup action (RequestId: 70428621-b81e-4e56-a8f3-8c2a788b9e13)
```
### Acknowledgements
- [X] I may be able to implement this feature request
- [ ] This feature might incur a breaking change
### CDK version used
2.39.0
### Environment details (OS name and version, etc.)
macOS
Contributor guide
Research direction
Start with packages/@aws-cdk/aws-lambda/lib/function.ts and trace the LogRetention behavior into packages/@aws-cdk/aws-logs/lib/log-retention.ts, especially its IAM policy setup. No test path is named; completion should establish a settled Function API that removes its Lambda log group on stack deletion while retaining the required deletion permission.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- aws, typescript
- Domain
- cloud, infrastructure
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100