-
Notifications
You must be signed in to change notification settings - Fork 4.3k
fix(mixins-preview): improving delivery source and delivery destination creation #36314
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
Changes from 13 commits
f5410c5
4710d26
d53bc3a
f4ca026
37c8ce7
0a2c32a
22379b0
6ad2e5c
f635ca7
77a716f
c112511
a197315
3f1f28d
e20299b
b90cdd9
db6a0ad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,13 +32,13 @@ export interface ILogsDeliveryConfig { | |
| */ | ||
| export interface ILogsDelivery { | ||
| /** | ||
| * Binds the destination to a delivery source and creates a delivery connection between the source and destination. | ||
| * Binds the log delivery to a source resource and creates a delivery connection between the source and destination. | ||
| * @param scope - The construct scope | ||
| * @param deliverySource - The delivery source reference | ||
| * @param logType - The type of logs that the delivery source will produce | ||
| * @param sourceResourceArn - The Arn of the source resource | ||
| * @returns The delivery reference | ||
| */ | ||
| bind(scope: IConstruct, deliverySource: logs.IDeliverySourceRef, sourceResourceArn: string): ILogsDeliveryConfig; | ||
| bind(scope: IConstruct, logType: string, sourceResourceArn: string): ILogsDeliveryConfig; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -84,28 +84,29 @@ export class S3LogsDelivery implements ILogsDelivery { | |
| } | ||
|
|
||
| /** | ||
| * Binds the S3 destination to a delivery source and creates a delivery connection between them. | ||
| * Binds S3 Bucket to a source resource for the purposes of log delivery and creates a delivery source, a delivery destination, and a connection between them. | ||
| */ | ||
| public bind(scope: IConstruct, deliverySource: logs.IDeliverySourceRef, _sourceResourceArn: string): ILogsDeliveryConfig { | ||
| const container = new Construct(scope, deliveryId('S3', scope, this.bucket)); | ||
| public bind(scope: IConstruct, logType: string, sourceResourceArn: string): ILogsDeliveryConfig { | ||
| const container = new Construct(scope, deliveryId('S3'.concat(logType.split('_').map(word => word.charAt(0) + word.slice(1).toLowerCase()).join('')), scope, this.bucket)); | ||
|
|
||
| const bucketPolicy = this.getOrCreateBucketPolicy(container); | ||
| this.grantLogsDelivery(bucketPolicy); | ||
|
|
||
| const deliverySource = getOrCreateDeliverySource(logType, scope, sourceResourceArn); | ||
| const deliverySourceRef = deliverySource.deliverySourceRef; | ||
|
|
||
| const deliveryDestination = new logs.CfnDeliveryDestination(container, 'Dest', { | ||
| destinationResourceArn: this.bucket.bucketRef.bucketArn, | ||
| name: deliveryDestName('s3', container), | ||
| name: deliveryDestName('s3'.concat(`-${logType.split('_').map(word => word.toLowerCase()).join('-')}`), container), | ||
|
||
| deliveryDestinationType: 'S3', | ||
| }); | ||
|
|
||
| const delivery = new logs.CfnDelivery(container, 'Delivery', { | ||
| deliveryDestinationArn: deliveryDestination.attrArn, | ||
| deliverySourceName: deliverySource.deliverySourceRef.deliverySourceName, | ||
| deliverySourceName: deliverySourceRef.deliverySourceName, | ||
| }); | ||
|
|
||
| delivery.node.addDependency(bucketPolicy); | ||
| deliveryDestination.node.addDependency(bucketPolicy); | ||
| deliverySource.node.addDependency(bucketPolicy); | ||
|
|
||
| delivery.node.addDependency(deliverySource); | ||
| delivery.node.addDependency(deliveryDestination); | ||
|
|
@@ -197,18 +198,20 @@ export class FirehoseLogsDelivery implements ILogsDelivery { | |
| } | ||
|
|
||
| /** | ||
| * Binds the Firehose destination to a delivery source and creates a delivery connection between them. | ||
| * Binds Firehose Delivery Stream to a source resource for the purposes of log delivery and creates a delivery source, a delivery destination, and a connection between them. | ||
| */ | ||
| public bind(scope: IConstruct, deliverySource: logs.IDeliverySourceRef, _sourceResourceArn: string): ILogsDeliveryConfig { | ||
| const container = new Construct(scope, deliveryId('Firehose', scope, this.deliveryStream)); | ||
| public bind(scope: IConstruct, logType: string, sourceResourceArn: string): ILogsDeliveryConfig { | ||
| const container = new Construct(scope, deliveryId('Firehose'.concat(logType.split('_').map(word => word.charAt(0) + word.slice(1).toLowerCase()).join('')), scope, this.deliveryStream)); | ||
|
|
||
| // Firehose uses a service-linked role to deliver logs | ||
| // This tag marks the destination stream as an allowed destination for the service-linked role | ||
| Tags.of(this.deliveryStream).add('LogDeliveryEnabled', 'true'); | ||
|
|
||
| const deliverySource = getOrCreateDeliverySource(logType, scope, sourceResourceArn); | ||
|
|
||
| const deliveryDestination = new logs.CfnDeliveryDestination(container, 'Dest', { | ||
| destinationResourceArn: this.deliveryStream.deliveryStreamRef.deliveryStreamArn, | ||
| name: deliveryDestName('fh', container), | ||
| name: deliveryDestName('fh'.concat(`-${logType.split('_').map(word => word.toLowerCase()).join('-')}`), container), | ||
| deliveryDestinationType: 'FH', | ||
| }); | ||
|
|
||
|
|
@@ -243,30 +246,31 @@ export class LogGroupLogsDelivery implements ILogsDelivery { | |
| } | ||
|
|
||
| /** | ||
| * Binds the log group destination to a delivery source and creates a delivery connection between them. | ||
| * Binds Log Group to a source resource for the purposes of log delivery and creates a delivery source, a delivery destination, and a connection between them. | ||
| */ | ||
| public bind(scope: IConstruct, deliverySource: logs.IDeliverySourceRef, _sourceResourceArn: string): ILogsDeliveryConfig { | ||
| const container = new Construct(scope, deliveryId('LogGroup', scope, this.logGroup)); | ||
| public bind(scope: IConstruct, logType: string, sourceResourceArn: string): ILogsDeliveryConfig { | ||
| const container = new Construct(scope, deliveryId('LogGroup'.concat(logType.split('_').map(word => word.charAt(0) + word.slice(1).toLowerCase()).join('')), scope, this.logGroup)); | ||
|
|
||
| const deliverySource = getOrCreateDeliverySource(logType, scope, sourceResourceArn); | ||
| const deliverySourceRef = deliverySource.deliverySourceRef; | ||
|
|
||
| const logGroupPolicy = this.getOrCreateLogsResourcePolicy(container); | ||
| this.grantLogsDelivery(logGroupPolicy); | ||
|
|
||
| const deliveryDestination = new logs.CfnDeliveryDestination(container, 'Dest', { | ||
| destinationResourceArn: this.logGroup.logGroupRef.logGroupArn, | ||
| name: deliveryDestName('cwl', container), | ||
| name: deliveryDestName('cwl'.concat(`-${logType.split('_').map(word => word.toLowerCase()).join('-')}`), container), | ||
| deliveryDestinationType: 'CWL', | ||
| }); | ||
|
|
||
| const delivery = new logs.CfnDelivery(container, 'Delivery', { | ||
| deliveryDestinationArn: deliveryDestination.deliveryDestinationRef.deliveryDestinationArn, | ||
| deliverySourceName: deliverySource.deliverySourceRef.deliverySourceName, | ||
| deliverySourceName: deliverySourceRef.deliverySourceName, | ||
| }); | ||
|
|
||
| delivery.node.addDependency(logGroupPolicy); | ||
| delivery.node.addDependency(deliverySource); | ||
| delivery.node.addDependency(deliveryDestination); | ||
|
|
||
| deliverySource.node.addDependency(logGroupPolicy); | ||
| deliveryDestination.node.addDependency(logGroupPolicy); | ||
|
|
||
| return { | ||
|
|
@@ -327,16 +331,17 @@ export class XRayLogsDelivery implements ILogsDelivery { | |
| constructor() {} | ||
|
|
||
| /** | ||
| * Binds the X-Ray destination to a delivery source and creates a delivery connection between them. | ||
| * Binds X-Ray Destination to a source resource for the purposes of log delivery and creates a delivery source, a delivery destination, and a connection between them. | ||
| */ | ||
| public bind(scope: IConstruct, deliverySource: logs.IDeliverySourceRef, sourceResourceArn: string): ILogsDeliveryConfig { | ||
| const container = new Construct(scope, deliveryId('XRay', scope, deliverySource)); | ||
| public bind(scope: IConstruct, logType: string, sourceResourceArn: string): ILogsDeliveryConfig { | ||
| const deliverySource = getOrCreateDeliverySource(logType, scope, sourceResourceArn); | ||
| const container = new Construct(scope, deliveryId('XRay'.concat(logType.split('_').map(word => word.charAt(0) + word.slice(1).toLowerCase()).join('')), scope, deliverySource)); | ||
|
|
||
| const xrayResourcePolicy = this.getOrCreateResourcePolicy(container); | ||
| this.grantLogsDelivery(xrayResourcePolicy, sourceResourceArn); | ||
|
|
||
| const deliveryDestination = new logs.CfnDeliveryDestination(container, 'Dest', { | ||
| name: deliveryDestName('xray', container), | ||
| name: deliveryDestName('xray'.concat(`-${logType.split('_').map(word => word.toLowerCase()).join('-')}`), container), | ||
| deliveryDestinationType: 'XRAY', | ||
| }); | ||
|
|
||
|
|
@@ -345,9 +350,7 @@ export class XRayLogsDelivery implements ILogsDelivery { | |
| deliverySourceName: deliverySource.deliverySourceRef.deliverySourceName, | ||
| }); | ||
|
|
||
| delivery.node.addDependency(xrayResourcePolicy); | ||
| deliveryDestination.node.addDependency(xrayResourcePolicy); | ||
| deliverySource.node.addDependency(xrayResourcePolicy); | ||
|
|
||
| delivery.node.addDependency(deliverySource); | ||
| delivery.node.addDependency(deliveryDestination); | ||
|
|
@@ -411,3 +414,20 @@ function deliveryDestName(type: string, scope: IConstruct) { | |
| const prefix = `cdk-${type}-dest-`; | ||
| return `${prefix}${Names.uniqueResourceName(scope, { maxLength: 60 - prefix.length })}`; | ||
| } | ||
|
|
||
| function getOrCreateDeliverySource(logType: string, resource: IConstruct, sourceArn: string) { | ||
| // the delivery source should always be a child of the Construct passed in by resource | ||
|
||
| const sourceId = `CDKSource${logType}${Names.uniqueId(resource)}`; | ||
| const sourceResource = resource.node.tryFindChild(sourceId) as logs.CfnDeliverySource; | ||
|
|
||
| if (!sourceResource) { | ||
| const prefix = `cdk-${logType.split('_').map(word => word.toLowerCase()).join('')}-source-`; | ||
| const newSource = new logs.CfnDeliverySource(resource, sourceId, { | ||
| name: `${prefix}${Names.uniqueResourceName(resource, { maxLength: 60 - prefix.length })}`, | ||
| logType, | ||
| resourceArn: sourceArn, | ||
| }); | ||
| return newSource; | ||
| } | ||
| return sourceResource; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add this to the
deliveryIdfunction instead, less repeat.