我可以为在 AWS CDK v2 创建的管道中创建的日志组设置默认保留时间吗?

Can I set the default retention time for log groups created in a AWS CDK v2 created pipeline?

我有一个基于 AWS VDK v2 的应用程序堆栈,它设置了一个具有不同阶段、应用程序等的 CodePipeline,它工作正常。但是此设置会创建一些未设置保留时间的日志组。由于日志组不是由我的 cdk 代码创建的,而是由 cdk 本身在后台创建的,因此我无法明确设置保留时间。

所以我尝试使用 Aspects 来实现。我创建了以下内容:

export class LogGroupRetentionChecker implements IAspect {

    prefix: String

    constructor(prefix: String) {
        this.prefix = prefix;
    }

    visit(node: IConstruct): void {

        console.log(`${(this.prefix)}: checking node ${node.constructor.name}`)

        if (node instanceof CfnResource) {
            console.log(`${this.prefix}: resource type ${node.cfnResourceType}`)
        }

        if (node instanceof CfnLogGroup) {

            console.log(`aspect found log-group ${node.logGroupName}, retention: ${node.retentionInDays}`);

            if (node.retentionInDays === undefined) {
                node.retentionInDays = 60
            }
        }
    }
}

在创建管道的地方,我然后这样做:

Aspects.of(this.pipeline).add(new LogGroupRetentionChecker("pipeline"));

如果再给我一个电话,我会创建一个舞台。对于 pipleine 上的方面,例如显示

pipeline: checking node CodePipeline
pipeline: checking node Pipeline
pipeline: checking node Key
pipeline: checking node CfnKey
pipeline: resource type AWS::KMS::Key
pipeline: checking node Alias
pipeline: checking node CfnAlias
pipeline: resource type AWS::KMS::Alias
pipeline: checking node Bucket
pipeline: checking node CfnBucket
pipeline: resource type AWS::S3::Bucket
pipeline: checking node BucketPolicy
pipeline: checking node CfnBucketPolicy
pipeline: resource type AWS::S3::BucketPolicy
pipeline: checking node Role
pipeline: checking node CfnRole
pipeline: resource type AWS::IAM::Role
pipeline: checking node Policy
pipeline: checking node CfnPolicyConditional
pipeline: resource type AWS::IAM::Policy
pipeline: checking node CfnPipeline
pipeline: resource type AWS::CodePipeline::Pipeline
pipeline: checking node Construct
pipeline: checking node Construct
pipeline: checking node Role
pipeline: checking node CfnRole
pipeline: resource type AWS::IAM::Role
pipeline: checking node Policy
pipeline: checking node CfnPolicyConditional
pipeline: resource type AWS::IAM::Policy
pipeline: checking node Construct

...

因此调用了切面,但是访问的 IConstruct 中的 none 是 CfnLogGroup

我现在的问题是:如何配置这些日志组?如果不是按方面,还有其他方法吗?

对于无法在 CDK/Cfn 中直接访问的日志组,请使用 Lambda-backed Trigger or CustomResource 通过 SDK 调用设置保留策略。 CloudFormation 将调用您的 Lambda 作为管道堆栈部署生命周期的一部分。仅在堆栈创建时将 Trigger/CR 配置为 运行 Lambda。

Lambda 的工作是:

  1. 使用DescribeLogGroups查找日志组。通过设置 logGroupNamePrefix: "/aws/codebuild/MyPipeline".
  2. 过滤
  3. 为每个日志组名称调用 PutRetentionPolicy

编辑:另一种选择

另一种选择是为管道中的每个 CodeBuild 项目创建显式日志组。

许多管道构造,包括 ShellStep,在幕后部署 AWS::CodeBuild::Project 资源。如果我们创建一个 LogGroup 并设置了所需的保留天数,我们可以将其传递给一个项目。

对于间接创建的项目(如底层ShellStep)我们必须首先使用escape hatch syntax to reference the underlying CfnProject constructs. Then we pass our configured LogGroup name to the its logsConfig 属性.

显式构造 codebuild.Project 更容易。它直接在 logging:LoggingOptions 属性中接受 LogGroup