|
| 1 | +import { IInputTransformation, IPipe, ITarget, TargetConfig } from '@aws-cdk/aws-pipes-alpha'; |
| 2 | +import { Token } from 'aws-cdk-lib'; |
| 3 | +import { IRole } from 'aws-cdk-lib/aws-iam'; |
| 4 | +import { IStream } from 'aws-cdk-lib/aws-kinesis'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Kinesis target properties. |
| 8 | + */ |
| 9 | +export interface KinesisTargetParameters { |
| 10 | + /** |
| 11 | + * The input transformation to apply to the message before sending it to the target. |
| 12 | + * |
| 13 | + * @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-pipes-pipe-pipetargetparameters.html#cfn-pipes-pipe-pipetargetparameters-inputtemplate |
| 14 | + * @default - none |
| 15 | + */ |
| 16 | + readonly inputTransformation?: IInputTransformation; |
| 17 | + |
| 18 | + /** |
| 19 | + * Determines which shard in the stream the data record is assigned to. |
| 20 | + * |
| 21 | + * @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-pipes-pipe-pipetargetkinesisstreamparameters.html#cfn-pipes-pipe-pipetargetkinesisstreamparameters-partitionkey |
| 22 | + */ |
| 23 | + readonly partitionKey: string; |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * An EventBridge Pipes target that sends messages to a Kinesis stream. |
| 28 | + */ |
| 29 | +export class KinesisTarget implements ITarget { |
| 30 | + private stream: IStream; |
| 31 | + private streamParameters: KinesisTargetParameters; |
| 32 | + public readonly targetArn: string; |
| 33 | + |
| 34 | + constructor(stream: IStream, parameters: KinesisTargetParameters) { |
| 35 | + this.stream = stream; |
| 36 | + this.targetArn = stream.streamArn; |
| 37 | + this.streamParameters = parameters; |
| 38 | + |
| 39 | + validatePartitionKey(parameters.partitionKey); |
| 40 | + } |
| 41 | + |
| 42 | + grantPush(grantee: IRole): void { |
| 43 | + this.stream.grantWrite(grantee); |
| 44 | + } |
| 45 | + |
| 46 | + bind(pipe: IPipe): TargetConfig { |
| 47 | + return { |
| 48 | + targetParameters: { |
| 49 | + inputTemplate: this.streamParameters.inputTransformation?.bind(pipe).inputTemplate, |
| 50 | + kinesisStreamParameters: this.streamParameters, |
| 51 | + }, |
| 52 | + }; |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +function validatePartitionKey(pk: string) { |
| 57 | + if (!Token.isUnresolved(pk) && pk.length > 256) { |
| 58 | + throw new Error(`Partition key must be less than or equal to 256 characters, received ${pk.length}`); |
| 59 | + } |
| 60 | +} |
0 commit comments