Search code examples
typescriptaws-cdk

AWS CDK prevent creation of extra LogRetention Function


I am using AWS CDK with typescript to create a lambda function. I am attempting to add a subscription filter to that lambda function to push logs to a kinesis stream.

Whenever i include the subscription filter it creates a new lambda function "LogRetention" which i dont want.

   const MyLFN = new NodejsFunction(this, "MyLFN", {...}) // We do not set any log* props here

   const FnbKinesisSF = new logs.SubscriptionFilter(this, "FnbKinesisSF", {
        logGroup: MyLFN.logGroup,
        destination: new destinations.KinesisDestination(FnbKinesis, {
            role: FnbKinesisRole,
        }),
        filterPattern: logs.FilterPattern.allEvents(),
    });

Is there a way to prevent the creation of the LogRetention Lambda function.

FYI: The LogRetention lambda does not show up in the CDK diff.


Solution

  • The solution is to create the LogGroup first before creating the Lambda Function. Then in your lambda function you reference an existing log group.

        const MyLfnLg = new logs.LogGroup(this, "MyLfnLg", {
            logGroupName: "/aws/lambda/ex-scheduler",
            retention: logs.RetentionDays.ONE_MONTH,
            removalPolicy: cdk.RemovalPolicy.DESTROY,
        });
    
        const MyLfn = new NodejsFunction(this, "RoboloxSchedulerLfn", {
            functionName: "ex-scheduler",
            logGroup: MyLfnLg,
        });
    
        MyLfnLg.addSubscriptionFilter("FnbKinesis-Subscription", {
            destination: new destinations.KinesisDestination(FnbKinesis, {
                role: FnbKinesisRole,
            }),
            filterPattern: logs.FilterPattern.allEvents(),
        });
    

    This prevents the creation of the uncessary "LogRetention" Lambda function