Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Aws, Names, Stack, Tags } from 'aws-cdk-lib/core';
import { Arn, ArnFormat, Aws, Names, Stack, Tags, Token } from 'aws-cdk-lib/core';
import { Effect, PolicyDocument, PolicyStatement, ServicePrincipal } from 'aws-cdk-lib/aws-iam';
import * as logs from 'aws-cdk-lib/aws-logs';
import * as s3 from 'aws-cdk-lib/aws-s3';
Expand All @@ -9,6 +9,7 @@ import { ConstructSelector, Mixins } from '../../core';
import * as xray from '../aws-xray/policy';
import { BucketPolicyStatementsMixin } from '../aws-s3/bucket-policy';
import type { CfnKey, IKeyRef } from 'aws-cdk-lib/aws-kms';
import { md5hash } from 'aws-cdk-lib/core/lib/helpers-internal';

/**
* The individual elements of a logs delivery integration.
Expand All @@ -21,7 +22,7 @@ export interface ILogsDeliveryConfig {
/**
* The logs delivery destination.
*/
readonly deliveryDestination: logs.IDeliveryDestinationRef;
readonly deliveryDestination?: logs.IDeliveryDestinationRef;
/**
* The logs delivery
*/
Expand Down Expand Up @@ -474,6 +475,41 @@ export class XRayLogsDelivery implements ILogsDelivery {
}
}

/**
* Delivers vended logs to a CfnDeliveryDestination specified by an arn.
*/
export class DestinationLogsDelivery implements ILogsDelivery {
/**
* Creates a new Destination delivery.
*/
private readonly destinationArn: string;
constructor(destinationArn: string) {
this.destinationArn = destinationArn;
}

/**
* Binds Delivery Destination to a source resource for the purposes of log delivery and creates a delivery source and a connection between the source and the destination.
*/
public bind(scope: IConstruct, logType: string, sourceResourceArn: string): ILogsDeliveryConfig {
const deliverySource = getOrCreateDeliverySource(logType, scope, sourceResourceArn);
const destName = Arn.split(this.destinationArn, ArnFormat.COLON_RESOURCE_NAME).resourceName;
const uniqueName = destName && !Token.isUnresolved(destName) ? destName : `Dest${md5hash(this.destinationArn)}`;
const container = new Construct(scope, deliveryId(uniqueName, logType, scope, deliverySource));

const delivery = new logs.CfnDelivery(container, 'Delivery', {
deliveryDestinationArn: this.destinationArn,
deliverySourceName: deliverySource.deliverySourceRef.deliverySourceName,
});

delivery.node.addDependency(deliverySource);

return {
deliverySource,
delivery,
};
}
}

function deliveryId(destType: string, logType: string, ...scopes: IConstruct[]) {
return `Cdk${destType}${logType.split('_').map(word => word.charAt(0) + word.slice(1).toLowerCase()).join('')}Delivery${scopes.map(s => Names.uniqueId(s)).join('')}`;
}
Expand Down
18 changes: 18 additions & 0 deletions packages/@aws-cdk/mixins-preview/scripts/spec2logs/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,24 @@ class LogsHelper extends ClassType {
}

public build(mixin: LogsMixin) {
const toDest = this.addMethod({
name: 'toDestination',
returnType: mixin.type,
docs: {
summary: 'Send logs to a destination with a specified arn',
},
});

const paramDest = toDest.addParameter({
name: 'destinationArn',
type: Type.STRING,
});

toDest.addBody(stmt.block(
stmt.ret(
mixin.newInstance(expr.str(this.log.logType), new NewExpression(MIXINS_LOGS_DELIVERY.DestLogsDelivery, paramDest)),
),
));
for (const dest of this.log.destinations) {
switch (dest.destinationType) {
case 'S3':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class MixinsLogsDelivery extends ExternalModule {
public readonly LogGroupLogsDelivery = Type.fromName(this, 'LogGroupLogsDelivery');
public readonly FirehoseLogsDelivery = Type.fromName(this, 'FirehoseLogsDelivery');
public readonly XRayLogsDelivery = Type.fromName(this, 'XRayLogsDelivery');
public readonly DestLogsDelivery = Type.fromName(this, 'DestinationLogsDelivery');
public readonly ILogsDelivery = Type.fromName(this, 'ILogsDelivery');
public readonly S3LogsDeliveryPermissionsVersion = $T(Type.fromName(this, 'S3LogsDeliveryPermissionsVersion'));
public readonly S3LogsDestinationProps = Type.fromName(this, 'IS3LogsDestinationProps');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ import * as service from "aws-cdk-lib/aws-some";
* @stability external
*/
export class CfnThingApplicationLogs {
/**
* Send logs to a destination with a specified arn
*/
public toDestination(destinationArn: string): CfnThingLogsMixin {
{
return new CfnThingLogsMixin("APPLICATION_LOGS", new logsDelivery.DestinationLogsDelivery(destinationArn));
}
}

/**
* Send logs to an S3 Bucket
*/
Expand Down Expand Up @@ -56,6 +65,15 @@ export class CfnThingApplicationLogs {
* @stability external
*/
export class CfnThingAccessLogs {
/**
* Send logs to a destination with a specified arn
*/
public toDestination(destinationArn: string): CfnThingLogsMixin {
{
return new CfnThingLogsMixin("ACCESS_LOGS", new logsDelivery.DestinationLogsDelivery(destinationArn));
}
}

/**
* Send logs to an S3 Bucket
*/
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -57,28 +57,25 @@
}
}
},
"EventBusCdkLogGroupInfoLogsDeliveryVendedLogsMixinTestEventBus050202CAVendedLogsMixinTestDeliveryLogGroupD06F335FDest512D2F7B": {
"Type": "AWS::Logs::DeliveryDestination",
"EventBusCDKSourceINFOLOGSVendedLogsMixinTestEventBus050202CA7ECF7D3F": {
"Type": "AWS::Logs::DeliverySource",
"Properties": {
"DeliveryDestinationType": "CWL",
"DestinationResourceArn": {
"LogType": "INFO_LOGS",
"Name": "cdk-infologs-source-VendedLogsMixinTestEventBus050202CA",
"ResourceArn": {
"Fn::GetAtt": [
"DeliveryLogGroup2A53FD53",
"EventBus7B8748AA",
"Arn"
]
},
"Name": "cdk-cwl-info-logs-dest-VendedLogsMixigGroupD06F335FC36F4D1E"
},
"DependsOn": [
"CdkLogGroupLogsDeliveryPolicyResourcePolicy4483E857"
]
}
}
},
"EventBusCdkLogGroupInfoLogsDeliveryVendedLogsMixinTestEventBus050202CAVendedLogsMixinTestDeliveryLogGroupD06F335FDelivery69196BA3": {
"EventBusCdkDest094bf5afe7c2f993f7c3b692fa98e7b3InfoLogsDeliveryVendedLogsMixinTestEventBus050202CAVendedLogsMixinTestEventBusCDKSourceINFOLOGSVendedLogsMixinTestEventBus050202CAEA2661EFDeliveryD0E9AD9A": {
"Type": "AWS::Logs::Delivery",
"Properties": {
"DeliveryDestinationArn": {
"Fn::GetAtt": [
"EventBusCdkLogGroupInfoLogsDeliveryVendedLogsMixinTestEventBus050202CAVendedLogsMixinTestDeliveryLogGroupD06F335FDest512D2F7B",
"DeliveryDestination",
"Arn"
]
},
Expand All @@ -87,23 +84,9 @@
}
},
"DependsOn": [
"EventBusCdkLogGroupInfoLogsDeliveryVendedLogsMixinTestEventBus050202CAVendedLogsMixinTestDeliveryLogGroupD06F335FDest512D2F7B",
"EventBusCDKSourceINFOLOGSVendedLogsMixinTestEventBus050202CA7ECF7D3F"
]
},
"EventBusCDKSourceINFOLOGSVendedLogsMixinTestEventBus050202CA7ECF7D3F": {
"Type": "AWS::Logs::DeliverySource",
"Properties": {
"LogType": "INFO_LOGS",
"Name": "cdk-infologs-source-VendedLogsMixinTestEventBus050202CA",
"ResourceArn": {
"Fn::GetAtt": [
"EventBus7B8748AA",
"Arn"
]
}
}
},
"EventBusCdkS3ErrorLogsDeliveryVendedLogsMixinTestEventBus050202CAVendedLogsMixinTestDeliveryBucketE1BC60E4DestE59F2AF8": {
"Type": "AWS::Logs::DeliveryDestination",
"Properties": {
Expand Down Expand Up @@ -476,6 +459,19 @@
"CustomS3AutoDeleteObjectsCustomResourceProviderRole3B1BD092"
]
},
"DeliveryDestination": {
"Type": "AWS::Logs::DeliveryDestination",
"Properties": {
"DeliveryDestinationType": "CWL",
"DestinationResourceArn": {
"Fn::GetAtt": [
"DeliveryLogGroup2A53FD53",
"Arn"
]
},
"Name": "my-log-group-destination"
}
},
"CdkLogGroupLogsDeliveryPolicyResourcePolicy4483E857": {
"Type": "AWS::Logs::ResourcePolicy",
"Properties": {
Expand Down Expand Up @@ -506,29 +502,6 @@
"Arn"
]
},
":log-stream:*\"},{\"Action\":[\"logs:CreateLogStream\",\"logs:PutLogEvents\"],\"Condition\":{\"StringEquals\":{\"aws:SourceAccount\":\"",
{
"Ref": "AWS::AccountId"
},
"\"},\"ArnLike\":{\"aws:SourceArn\":\"arn:",
{
"Ref": "AWS::Partition"
},
":logs:",
{
"Ref": "AWS::Region"
},
":",
{
"Ref": "AWS::AccountId"
},
":*\"}},\"Effect\":\"Allow\",\"Principal\":{\"Service\":\"delivery.logs.amazonaws.com\"},\"Resource\":\"",
{
"Fn::GetAtt": [
"DeliveryLogGroup2A53FD53",
"Arn"
]
},
":log-stream:*\"}],\"Version\":\"2012-10-17\"}"
]
]
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,16 @@ const bucket = new s3.Bucket(stack, 'DeliveryBucket', {
encryptionKey: key,
});

const deliveryDestination = new logs.CfnDeliveryDestination(stack, 'DeliveryDestination', {
name: 'my-log-group-destination',
destinationResourceArn: logGroup.logGroupArn,
deliveryDestinationType: 'CWL',
});

// Setup error logs delivery to Cloudwatch
eventBus.with(CfnEventBusLogsMixin.ERROR_LOGS.toLogGroup(logGroup));
// Setup info logs delivery to Cloudwatch
eventBus.with(CfnEventBusLogsMixin.INFO_LOGS.toLogGroup(logGroup));
// Setup info logs delivery to Cloudwatch via manually created delivery destination
eventBus.with(CfnEventBusLogsMixin.INFO_LOGS.toDestination(deliveryDestination.attrArn));
// Setup error logs delivery to S3
eventBus.with(CfnEventBusLogsMixin.ERROR_LOGS.toS3(bucket));
// Setup info logs delivery to S3
Expand Down
Loading
Loading