diff --git a/packages/@aws-cdk/aws-pipes-targets-alpha/README.md b/packages/@aws-cdk/aws-pipes-targets-alpha/README.md index 868e011170c67..f9b5e1994983d 100644 --- a/packages/@aws-cdk/aws-pipes-targets-alpha/README.md +++ b/packages/@aws-cdk/aws-pipes-targets-alpha/README.md @@ -32,6 +32,7 @@ The following targets are supported: 1. `targets.SqsTarget`: [Send event source to a Queue](#amazon-sqs) 2. `targets.SfnStateMachine`: [Invoke a State Machine from an event source](#aws-step-functions-state-machine) 3. `targets.LambdaFunction`: [Send event source to a Lambda Function](#aws-lambda-function) +3. `targets.CloudWatchLogsTarget`: [Send event source to a CloudWatch Logs log group](#amazon-cloudwatch-logs-log-group) ### Amazon SQS diff --git a/packages/@aws-cdk/aws-pipes-targets-alpha/lib/cloudwatch-logs.ts b/packages/@aws-cdk/aws-pipes-targets-alpha/lib/cloudwatch-logs.ts index ee17f834cc6f0..8a5a0b49b216f 100644 --- a/packages/@aws-cdk/aws-pipes-targets-alpha/lib/cloudwatch-logs.ts +++ b/packages/@aws-cdk/aws-pipes-targets-alpha/lib/cloudwatch-logs.ts @@ -1,6 +1,7 @@ import { IInputTransformation, IPipe, ITarget, TargetConfig } from '@aws-cdk/aws-pipes-alpha'; import { IRole } from 'aws-cdk-lib/aws-iam'; import { ILogGroup } from 'aws-cdk-lib/aws-logs'; +import { Token } from 'aws-cdk-lib'; /** * CloudWatch Logs target properties. @@ -75,7 +76,7 @@ export class CloudWatchLogsTarget implements ITarget { } function validateLogStreamName({ logStreamName }: CloudWatchLogsTargetParameters) { - if (logStreamName !== undefined) { + if (logStreamName !== undefined && !Token.isUnresolved(logStreamName)) { if (logStreamName.length < 1 || logStreamName.length > 256) { throw new Error(`Log stream name must be between 1 and 256 characters, received ${logStreamName.length}`); } @@ -83,7 +84,7 @@ function validateLogStreamName({ logStreamName }: CloudWatchLogsTargetParameters } function validateTimestamp({ timestamp }: CloudWatchLogsTargetParameters) { - if (timestamp !== undefined) { + if (timestamp !== undefined && !Token.isUnresolved(timestamp)) { if (timestamp.length < 1 || timestamp.length > 256) { throw new Error(`Timestamp must be between 1 and 256 characters, received ${timestamp.length}`); } diff --git a/packages/@aws-cdk/aws-pipes-targets-alpha/test/cloudwatch-logs.test.ts b/packages/@aws-cdk/aws-pipes-targets-alpha/test/cloudwatch-logs.test.ts index 2e523919fca47..32ee94153617b 100644 --- a/packages/@aws-cdk/aws-pipes-targets-alpha/test/cloudwatch-logs.test.ts +++ b/packages/@aws-cdk/aws-pipes-targets-alpha/test/cloudwatch-logs.test.ts @@ -1,6 +1,6 @@ import { InputTransformation, Pipe } from '@aws-cdk/aws-pipes-alpha'; -import { App, Stack } from 'aws-cdk-lib'; +import { App, Lazy, Stack } from 'aws-cdk-lib'; import { Template } from 'aws-cdk-lib/assertions'; import { LogGroup } from 'aws-cdk-lib/aws-logs'; import { TestSource } from './test-classes'; @@ -143,6 +143,38 @@ describe('CloudWatch Logs source parameters validation', () => { }).toThrow('Log stream name must be between 1 and 256 characters, received 257'); }); + test('validateLogStreamName works with a token', () => { + // GIVEN + const app = new App(); + const stack = new Stack(app, 'TestStack'); + const logGroup = new LogGroup(stack, 'MyLogGroup', {}); + const logStreamName = Lazy.string({ produce: () => 'log-stream-name' }); + const target = new CloudWatchLogsTarget(logGroup, {logStreamName}); + + new Pipe(stack, 'MyPipe', { + source: new TestSource(), + target, + }); + + // ACT + const template = Template.fromStack(stack); + + // ASSERT + template.hasResourceProperties('AWS::Pipes::Pipe', { + Target: { + 'Fn::GetAtt': [ + 'MyLogGroup5C0DAD85', + 'Arn', + ], + }, + TargetParameters: { + CloudWatchLogsParameters: { + LogStreamName: 'log-stream-name', + }, + }, + }); + }); + test('Timestamp must be >= 1 character', () => { // GIVEN const app = new App(); @@ -170,4 +202,36 @@ describe('CloudWatch Logs source parameters validation', () => { }); }).toThrow('Timestamp must be between 1 and 256 characters, received 257'); }); + + test('validateTimestamp works with a token', () => { + // GIVEN + const app = new App(); + const stack = new Stack(app, 'TestStack'); + const logGroup = new LogGroup(stack, 'MyLogGroup', {}); + const timestamp = Lazy.string({ produce: () => 'timestamp' }); + const target = new CloudWatchLogsTarget(logGroup, {timestamp}); + + new Pipe(stack, 'MyPipe', { + source: new TestSource(), + target, + }); + + // ACT + const template = Template.fromStack(stack); + + // ASSERT + template.hasResourceProperties('AWS::Pipes::Pipe', { + Target: { + 'Fn::GetAtt': [ + 'MyLogGroup5C0DAD85', + 'Arn', + ], + }, + TargetParameters: { + CloudWatchLogsParameters: { + Timestamp: 'timestamp', + }, + }, + }); + }); });