From 64131d1310eea02b6ab1f585349742c266c1242d Mon Sep 17 00:00:00 2001 From: Madeline Kusters Date: Fri, 16 Jul 2021 17:13:41 -0700 Subject: [PATCH] leverage construct tree to store LogGroup and prevent creating it multiple times --- .../@aws-cdk/aws-kinesisfirehose/lib/destination.ts | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/packages/@aws-cdk/aws-kinesisfirehose/lib/destination.ts b/packages/@aws-cdk/aws-kinesisfirehose/lib/destination.ts index 0fd54e88ebe61..47f87c1d3a0c2 100644 --- a/packages/@aws-cdk/aws-kinesisfirehose/lib/destination.ts +++ b/packages/@aws-cdk/aws-kinesisfirehose/lib/destination.ts @@ -1,6 +1,6 @@ import * as iam from '@aws-cdk/aws-iam'; import * as logs from '@aws-cdk/aws-logs'; -import { Construct } from 'constructs'; +import { Construct, Node } from 'constructs'; import { IDeliveryStream } from './delivery-stream'; import { CfnDeliveryStream } from './kinesisfirehose.generated'; @@ -66,7 +66,6 @@ export interface DestinationProps { * Abstract base class that destination types can extend to benefit from methods that create generic configuration. */ export abstract class DestinationBase implements IDestination { - private logGroup?: logs.ILogGroup; constructor(protected readonly props: DestinationProps = {}) { } @@ -81,12 +80,12 @@ export abstract class DestinationBase implements IDestination { throw new Error('logging cannot be set to false when logGroup is provided'); } if (this.props.logging !== false || this.props.logGroup) { - this.logGroup = this.logGroup ?? this.props.logGroup ?? new logs.LogGroup(scope, 'LogGroup'); - this.logGroup.grantWrite(deliveryStream); + const logGroup = Node.of(scope).tryFindChild('LogGroup') as logs.ILogGroup ?? this.props.logGroup ?? new logs.LogGroup(scope, 'LogGroup'); + logGroup.grantWrite(deliveryStream); return { enabled: true, - logGroupName: this.logGroup.logGroupName, - logStreamName: this.logGroup.addStream(streamId).logStreamName, + logGroupName: logGroup.logGroupName, + logStreamName: logGroup.addStream(streamId).logStreamName, }; } return undefined;