Skip to content

Commit

Permalink
feat(scheduler-targets-alpha): KinesisDataFirehosePutRecord Target (a…
Browse files Browse the repository at this point in the history
…ws#27842)

This PR adds KinesisDataFirehosePutRecord as a target for an EventBridge scheduler.

Closes aws#27450.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
go-to-k authored and chenjane-dev committed Dec 5, 2023
1 parent 9482952 commit 562ce9b
Show file tree
Hide file tree
Showing 14 changed files with 36,292 additions and 0 deletions.
24 changes: 24 additions & 0 deletions packages/@aws-cdk/aws-scheduler-targets-alpha/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ The following targets are supported:
6. `targets.EventBridgePutEvents`: [Put Events on EventBridge](#send-events-to-an-eventbridge-event-bus)
7. `targets.InspectorStartAssessmentRun`: [Start an Amazon Inspector assessment run](#start-an-amazon-inspector-assessment-run)
8. `targets.KinesisStreamPutRecord`: [Put a record to an Amazon Kinesis Data Streams](#put-a-record-to-an-amazon-kinesis-data-streams)
9. `targets.KinesisDataFirehosePutRecord`: [Put a record to a Kinesis Data Firehose](#put-a-record-to-a-kinesis-data-firehose)

## Invoke a Lambda function

Expand Down Expand Up @@ -246,3 +247,26 @@ new Schedule(this, 'Schedule', {
}),
});
```

## Put a record to a Kinesis Data Firehose

Use the `KinesisDataFirehosePutRecord` target to put a record to a Kinesis Data Firehose delivery stream.

The code snippet below creates an event rule with a delivery stream as a target
called every hour by Event Bridge Scheduler with a custom payload.

```ts
import * as firehose from 'aws-cdk-lib/aws-kinesisfirehose';
declare const deliveryStream: firehose.CfnDeliveryStream;

const payload = {
Data: "record",
};

new Schedule(this, 'Schedule', {
schedule: ScheduleExpression.rate(Duration.minutes(60)),
target: new targets.KinesisDataFirehosePutRecord(deliveryStream, {
input: ScheduleTargetInput.fromObject(payload),
}),
});
```
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-scheduler-targets-alpha/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from './codebuild-start-build';
export * from './event-bridge-put-events';
export * from './inspector-start-assessment-run';
export * from './kinesis-data-firehose-put-record';
export * from './kinesis-stream-put-record';
export * from './lambda-invoke';
export * from './sns-publish';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { ISchedule, IScheduleTarget } from '@aws-cdk/aws-scheduler-alpha';
import { Names } from 'aws-cdk-lib';
import { IRole, PolicyStatement } from 'aws-cdk-lib/aws-iam';
import { CfnDeliveryStream } from 'aws-cdk-lib/aws-kinesisfirehose';
import { ScheduleTargetBase, ScheduleTargetBaseProps } from './target';
import { sameEnvDimension } from './util';

/**
* Use an Amazon Kinesis Data Firehose as a target for AWS EventBridge Scheduler.
*/
export class KinesisDataFirehosePutRecord extends ScheduleTargetBase implements IScheduleTarget {
constructor(
private readonly deliveryStream: CfnDeliveryStream,
private readonly props: ScheduleTargetBaseProps = {},
) {
super(props, deliveryStream.attrArn);
}

protected addTargetActionToRole(schedule: ISchedule, role: IRole): void {
if (!sameEnvDimension(this.deliveryStream.stack.region, schedule.env.region)) {
throw new Error(`Cannot assign the Firehose delivery stream in region ${this.deliveryStream.stack.region} to the schedule ${Names.nodeUniqueId(schedule.node)} in region ${schedule.env.region}. Both the schedule and the Firehose delivery stream must be in the same region.`);
}

if (!sameEnvDimension(this.deliveryStream.stack.account, schedule.env.account)) {
throw new Error(`Cannot assign the Firehose delivery stream in account ${this.deliveryStream.stack.account} to the schedule ${Names.nodeUniqueId(schedule.node)} in account ${schedule.env.region}. Both the schedule and the Firehose delivery stream must be in the same account.`);
}

if (this.props.role && !sameEnvDimension(this.props.role.env.account, this.deliveryStream.stack.account)) {
throw new Error(`Cannot grant permission to execution role in account ${this.props.role.env.account} to invoke target ${Names.nodeUniqueId(this.deliveryStream.node)} in account ${this.deliveryStream.stack.account}. Both the target and the execution role must be in the same account.`);
}

role.addToPrincipalPolicy(new PolicyStatement({
actions: ['firehose:PutRecord'],
resources: [this.deliveryStream.attrArn],
}));
}
}
Loading

0 comments on commit 562ce9b

Please sign in to comment.