|
| 1 | +import * as events from '@aws-cdk/aws-events'; |
| 2 | +import * as iam from '@aws-cdk/aws-iam'; |
| 3 | +import * as kinesis from '@aws-cdk/aws-kinesis'; |
| 4 | +import { singletonEventRole } from './util'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Customize the Kinesis Stream Event Target |
| 8 | + */ |
| 9 | +export interface KinesisStreamProps { |
| 10 | + /** |
| 11 | + * Partition Key Path for records sent to this stream |
| 12 | + * |
| 13 | + * @default - eventId as the partition key |
| 14 | + */ |
| 15 | + readonly partitionKeyPath?: string; |
| 16 | + |
| 17 | + /** |
| 18 | + * The message to send to the stream. |
| 19 | + * |
| 20 | + * Must be a valid JSON text passed to the target stream. |
| 21 | + * |
| 22 | + * @default - the entire CloudWatch event |
| 23 | + */ |
| 24 | + readonly message?: events.RuleTargetInput; |
| 25 | + |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * Use a Kinesis Stream as a target for AWS CloudWatch event rules. |
| 30 | + * |
| 31 | + * @example |
| 32 | + * |
| 33 | + * // put to a Kinesis stream every time code is committed |
| 34 | + * // to a CodeCommit repository |
| 35 | + * repository.onCommit(new targets.KinesisStream(stream)); |
| 36 | + * |
| 37 | + */ |
| 38 | +export class KinesisStream implements events.IRuleTarget { |
| 39 | + |
| 40 | + constructor(private readonly stream: kinesis.IStream, private readonly props: KinesisStreamProps = {}) { |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Returns a RuleTarget that can be used to trigger this Kinesis Stream as a |
| 45 | + * result from a CloudWatch event. |
| 46 | + */ |
| 47 | + public bind(_rule: events.IRule, _id?: string): events.RuleTargetConfig { |
| 48 | + const policyStatements = [new iam.PolicyStatement({ |
| 49 | + actions: ['kinesis:PutRecord', 'kinesis:PutRecords'], |
| 50 | + resources: [this.stream.streamArn], |
| 51 | + })]; |
| 52 | + |
| 53 | + return { |
| 54 | + id: '', |
| 55 | + arn: this.stream.streamArn, |
| 56 | + role: singletonEventRole(this.stream, policyStatements), |
| 57 | + input: this.props.message, |
| 58 | + targetResource: this.stream, |
| 59 | + kinesisParameters: this.props.partitionKeyPath ? { partitionKeyPath: this.props.partitionKeyPath } : undefined, |
| 60 | + }; |
| 61 | + } |
| 62 | + |
| 63 | +} |
0 commit comments