Skip to content

feat(lambda): function log removal policy #34723

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

Merged
merged 5 commits into from
Jun 25, 2025

Conversation

phuhung273
Copy link
Contributor

Issue # (if applicable)

Closes #34669

Reason for this change

Support configuring function log removal policy

Description of changes

Expose log group removal policy to function props

Describe any new or updated permissions being added

Description of how you validated changes

Unit + Integ

Checklist


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@aws-cdk-automation aws-cdk-automation requested a review from a team June 15, 2025 06:53
@github-actions github-actions bot added the star-contributor [Pilot] contributed between 25-49 PRs to the CDK label Jun 15, 2025
@aws-cdk-automation aws-cdk-automation added the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label Jun 15, 2025
Copy link
Contributor

@go-to-k go-to-k left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please change the PR title to feat(lambda): ...? Because this PR is to add a new property so I believe that's a feat. In that case, you can request to exempt README. (Or we could just make a few changes to the code example in the README.)

Comment on lines 1144 to 1161
removalPolicy: props.logRemovalPolicy,
});
this._logGroup = logs.LogGroup.fromLogGroupArn(this, 'LogGroup', logRetention.logGroupArn);
this._logRetention = logRetention;
} else if (!props.logGroup && (FeatureFlags.of(this).isEnabled(USE_CDK_MANAGED_LAMBDA_LOGGROUP))) { // logRetention:f/undef, logGroup:f/undef, FF.isEnabled()
this._logGroup = new logs.LogGroup(this, 'LogGroup', {
logGroupName: `/aws/lambda/${this.functionName}`,
removalPolicy: props.logRemovalPolicy,
});
}
Copy link
Contributor

@go-to-k go-to-k Jun 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that a log group is being generated within the getter. I thought to store logRemovalPolicy in a private instance variable and to pass it to the following properties too.

https://github.com/aws/aws-cdk/blob/v2.201.0/packages/aws-cdk-lib/aws-lambda/lib/function.ts#L1401-L1404

  private _logRemovalPolicy?: RemovalPolicy;
  // ...
  // ...
  this._logRemovalPolicy = props.logRemovalPolicy;
  // ...
  // ...
  public get logGroup(): logs.ILogGroup {
    if (!this._logGroup) {
      const logRetention = new logs.LogRetention(this, 'LogRetention', {
        logGroupName: `/aws/lambda/${this.functionName}`,
        retention: logs.RetentionDays.INFINITE,
        removalPolicy: this._logRemovalPolicy,
      });
      this._logGroup = logs.LogGroup.fromLogGroupArn(this, `${this.node.id}-LogGroup`, logRetention.logGroupArn);
    }
    return this._logGroup;
  }

However, other properties such as logRetentionRole and logRetentionRetryOptions are not passed to them in the getter. But logRetentionRole and logRetentionRetryOptions are only applied when props.logRetention is specified, as indicated by their prefixes.

    if (props.logRetention) {
      if (props.logGroup) {
        throw new ValidationError('CDK does not support setting logRetention and logGroup', this);
      }
      const logRetention = new logs.LogRetention(this, 'LogRetention', {
        logGroupName: `/aws/lambda/${this.functionName}`,
        retention: props.logRetention,
        role: props.logRetentionRole,
        logRetentionRetryOptions: props.logRetentionRetryOptions as logs.LogRetentionRetryOptions,
      });
      this._logGroup = logs.LogGroup.fromLogGroupArn(this, 'LogGroup', logRetention.logGroupArn);

On the other hand, the creation of a log group using this getter is performed when props.logRetention is NOT specified (otherwise this._logGroup will be true). In other words, unlike those properties, it seems natural that logRemovalPolicy is also passed in the getter.

How about it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much for pointing this out. I totally missed this.

Added to this getter and include in tests.

@aws-cdk-automation aws-cdk-automation removed the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label Jun 16, 2025
@phuhung273 phuhung273 changed the title chore(lambda): function log removal policy feat(lambda): function log removal policy Jun 16, 2025
@github-actions github-actions bot added bug This issue is a bug. effort/medium Medium work item – several days of effort p2 labels Jun 16, 2025
Copy link
Collaborator

@aws-cdk-automation aws-cdk-automation left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(This review is outdated)

@phuhung273 phuhung273 force-pushed the lambda-log-removal-policy branch from 7b61c0b to 76aa322 Compare June 18, 2025 14:14
@aws-cdk-automation aws-cdk-automation dismissed their stale review June 18, 2025 14:59

✅ Updated pull request passes all PRLinter validations. Dismissing previous PRLinter review.

Copy link
Contributor

@go-to-k go-to-k left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the changes. LGTM.

@aws-cdk-automation aws-cdk-automation added the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Jun 19, 2025
@phuhung273
Copy link
Contributor Author

Thanks for your review @go-to-k

@matboros matboros self-assigned this Jun 20, 2025
@aws-cdk-automation aws-cdk-automation removed the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Jun 23, 2025
@phuhung273 phuhung273 force-pushed the lambda-log-removal-policy branch from cdf695a to 778d245 Compare June 24, 2025 12:19
@mergify mergify bot dismissed matboros’s stale review June 24, 2025 12:19

Pull request has been modified.

@phuhung273 phuhung273 force-pushed the lambda-log-removal-policy branch from 778d245 to e41cda1 Compare June 24, 2025 12:21
@aws-cdk-automation aws-cdk-automation added the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Jun 24, 2025
@aws-cdk-automation aws-cdk-automation removed the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Jun 24, 2025
@mergify mergify bot dismissed matboros’s stale review June 24, 2025 16:32

Pull request has been modified.

@aws-cdk-automation aws-cdk-automation added the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Jun 24, 2025
@phuhung273 phuhung273 requested a review from matboros June 25, 2025 05:20
Copy link
Contributor

mergify bot commented Jun 25, 2025

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: 02fbb10
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

Copy link
Contributor

mergify bot commented Jun 25, 2025

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@mergify mergify bot merged commit 0388483 into aws:main Jun 25, 2025
17 checks passed
Copy link
Contributor

Comments on closed issues and PRs are hard for our team to see.
If you need help, please open a new issue that references this one.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 25, 2025
@phuhung273 phuhung273 deleted the lambda-log-removal-policy branch June 25, 2025 11:49
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug This issue is a bug. effort/medium Medium work item – several days of effort p2 pr/needs-maintainer-review This PR needs a review from a Core Team Member star-contributor [Pilot] contributed between 25-49 PRs to the CDK
Projects
None yet
Development

Successfully merging this pull request may close these issues.

aws-lambda: Cannot set removal policy on auto-created log group
4 participants