Currently, I am trying to delete a log group from a lambda function that's created by AWS CDK
Here is what my stack currently looks like:
from aws_cdk import (
core,
aws_lambda
)
from aws_cdk.aws_logs import CfnLogGroup, LogGroup
from datetime import datetime
f = """
import json
from datetime import datetime
def handler(event, context):
print(f'test {datetime.now()}')
return {
'statusCode': 200,
'body': json.dumps(f'test {datetime.now()}')
}
"""
class CdktestStack(core.Stack):
def __init__(self, scope: core.Construct, construct_id: str, **kwargs) -> None:
super().__init__(scope, construct_id, **kwargs)
fn = aws_lambda.Function(self, f"AndyTest-{datetime.now()}",
runtime=aws_lambda.Runtime.PYTHON_3_8,
handler="index.handler",
code=aws_lambda.InlineCode(f)
)
lg = LogGroup.from_log_group_name(self, f"lambda_log_group-{datetime.now()}", fn.log_group.log_group_name)
lg.apply_removal_policy(core.RemovalPolicy.DESTROY)
By default, the lambda log group is retained after CDK destroy has completed.
I have tried to manage the log group by using LogGroups to manage the logs.
I have seen this answer for destroying a log group but it will not as this line:
lg = LogGroup.from_log_group_name(self, f"lambda_log_group-{datetime.now()}", fn.log_group.log_group_name)
returns an ILogGroup and throws this error when I try and call
lg.apply_removal_policy(core.RemovalPolicy.DESTROY)
with this error:
jsii.errors.JSIIError: Cannot apply RemovalPolicy: no child or not a CfnResource. Apply the removal policy on the CfnResource directly.
My objective is to use CDK to manage the log group so that the resource can be deleted on destroy without using boto3.