Skip to content

aws-lambda: Cannot set removal policy on auto-created log group #34669

@DFEvans

Description

@DFEvans

Describe the bug

After creating an AWS Lambda function with a log retention period, CDK will auto-generate a LogGroup. When trying to set the removal policy on that auto-generated log group via apply_removal_policy, I receive an error referring to no child or not a CfnResource which appears to be an internal CDK issue.

Regression Issue

  • Select this option if this issue appears to be a regression.

Last Known Working CDK Library Version

No response

Expected Behavior

I am able to set the log group removal policy

Current Behavior

CDK fails the operation with:

jsii.errors.JavaScriptError:
  @jsii/kernel.RuntimeError: Error: Cannot apply RemovalPolicy: no child or not a CfnResource. Apply the removal policy on the CfnResource directly.
      at Kernel._Kernel_ensureSync (/tmp/tmpfvt728k1/lib/program.js:927:23)
      at Kernel.invoke (/tmp/tmpfvt728k1/lib/program.js:294:102)
      at KernelHost.processRequest (/tmp/tmpfvt728k1/lib/program.js:15467:36)
      at KernelHost.run (/tmp/tmpfvt728k1/lib/program.js:15427:22)
      at Immediate._onImmediate (/tmp/tmpfvt728k1/lib/program.js:15428:46)
      at process.processImmediate (node:internal/timers:505:21)

Reproduction Steps

import aws_cdk
import aws_cdk.aws_lambda
import aws_cdk.aws_logs
app = aws_cdk.App()
stack = aws_cdk.Stack(app, "stack")
f = aws_cdk.aws_lambda.Function(
	stack,
	"function",
	code=aws_cdk.aws_lambda.Code.from_inline("def handler(event, context): pass"),
	runtime=aws_cdk.aws_lambda.Runtime.PYTHON_3_12,
	log_retention=aws_cdk.aws_logs.RetentionDays.ONE_DAY,
	handler="index.handler",
)
f.log_group.apply_removal_policy(aws_cdk.RemovalPolicy.DESTROY)

Possible Solution

No response

Additional Information/Context

No response

AWS CDK Library version (aws-cdk-lib)

2.200.1

AWS CDK CLI version

2.1018.0

Node.js Version

23.11.0

OS

Ubuntu under WSL

Language

Python

Language Version

3.10.12

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    @aws-cdk/aws-lambdaRelated to AWS LambdabugThis issue is a bug.effort/mediumMedium work item – several days of effortp2

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions