Skip to content

Commit

Permalink
feat(aws-events-targets): Add Firehose target
Browse files Browse the repository at this point in the history
closes aws#10349
  • Loading branch information
cheruvian committed Sep 16, 2020
1 parent 857acbb commit 78950a1
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 7 deletions.
47 changes: 47 additions & 0 deletions packages/@aws-cdk/aws-events-targets/lib/firehose-stream.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import * as events from '@aws-cdk/aws-events';
import * as iam from '@aws-cdk/aws-iam';
import * as firehose from '@aws-cdk/aws-kinesisfirehose';
import { singletonEventRole } from './util';

/**
* Customize the Firehose Stream Event Target
*/
export interface FirehoseStreamProps {
/**
* The message to send to the stream.
*
* Must be a valid JSON text passed to the target stream.
*
* @default - the entire CloudWatch event
*/
readonly message?: events.RuleTargetInput;
}


/**
* Customize the Firehose Stream Event Target
*/
export class FirehoseStream implements events.IRuleTarget {

constructor(private readonly stream: firehose.CfnDeliveryStream, private readonly props: FirehoseStreamProps = {}) {
}

/**
* Returns a RuleTarget that can be used to trigger this Firehose Stream as a
* result from a CloudWatch event.
*/
public bind(_rule: events.IRule, _id?: string): events.RuleTargetConfig {
const policyStatements = [new iam.PolicyStatement({
actions: ['firehose:PutRecord', 'firehose:PutRecordBatch'],
resources: [this.stream.attrArn],
})];

return {
id: '',
arn: this.stream.attrArn,
role: singletonEventRole(this.stream, policyStatements),
input: this.props.message,
targetResource: this.stream,
};
}
}
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-events-targets/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export * from './ecs-task-properties';
export * from './ecs-task';
export * from './state-machine';
export * from './kinesis-stream';
export * from './firehose-stream';
16 changes: 9 additions & 7 deletions packages/@aws-cdk/aws-events-targets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,39 +77,41 @@
"pkglint": "0.0.0"
},
"dependencies": {
"@aws-cdk/aws-batch": "0.0.0",
"@aws-cdk/aws-codebuild": "0.0.0",
"@aws-cdk/aws-codepipeline": "0.0.0",
"@aws-cdk/aws-ec2": "0.0.0",
"@aws-cdk/aws-ecs": "0.0.0",
"@aws-cdk/aws-events": "0.0.0",
"@aws-cdk/aws-iam": "0.0.0",
"@aws-cdk/aws-kinesis": "0.0.0",
"@aws-cdk/aws-kinesisfirehose": "0.0.0",
"@aws-cdk/aws-lambda": "0.0.0",
"@aws-cdk/aws-sns": "0.0.0",
"@aws-cdk/aws-sns-subscriptions": "0.0.0",
"@aws-cdk/aws-sqs": "0.0.0",
"@aws-cdk/aws-sns-subscriptions": "0.0.0",
"@aws-cdk/aws-stepfunctions": "0.0.0",
"@aws-cdk/aws-batch": "0.0.0",
"@aws-cdk/aws-kinesis": "0.0.0",
"@aws-cdk/core": "0.0.0",
"constructs": "^3.0.4"
},
"homepage": "https://github.com/aws/aws-cdk",
"peerDependencies": {
"@aws-cdk/aws-batch": "0.0.0",
"@aws-cdk/aws-codebuild": "0.0.0",
"@aws-cdk/aws-codepipeline": "0.0.0",
"@aws-cdk/aws-ec2": "0.0.0",
"@aws-cdk/aws-ecs": "0.0.0",
"@aws-cdk/aws-events": "0.0.0",
"@aws-cdk/aws-iam": "0.0.0",
"@aws-cdk/aws-kinesis": "0.0.0",
"@aws-cdk/aws-kinesisfirehose": "0.0.0",
"@aws-cdk/aws-lambda": "0.0.0",
"@aws-cdk/aws-sns": "0.0.0",
"@aws-cdk/aws-sns-subscriptions": "0.0.0",
"@aws-cdk/aws-sqs": "0.0.0",
"@aws-cdk/aws-sns-subscriptions": "0.0.0",
"@aws-cdk/aws-stepfunctions": "0.0.0",
"@aws-cdk/aws-batch": "0.0.0",
"@aws-cdk/core": "0.0.0",
"constructs": "^3.0.4",
"@aws-cdk/aws-kinesis": "0.0.0"
"constructs": "^3.0.4"
},
"engines": {
"node": ">= 10.13.0 <13 || >=13.7.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { expect, haveResource, haveResourceLike } from '@aws-cdk/assert';
import * as events from '@aws-cdk/aws-events';
import * as firehose from '@aws-cdk/aws-kinesisfirehose';
import { Stack } from '@aws-cdk/core';
import * as targets from '../../lib';

describe('FirehoseStream event target', () => {
let stack: Stack;
let stream: firehose.CfnDeliveryStream;
let streamArn: any;

beforeEach(() => {
stack = new Stack();
stream = new firehose.CfnDeliveryStream(stack, 'MyStream');
streamArn = { 'Fn::GetAtt': ['MyStream', 'Arn'] };
});

describe('when added to an event rule as a target', () => {
let rule: events.Rule;

beforeEach(() => {
rule = new events.Rule(stack, 'rule', {
schedule: events.Schedule.expression('rate(1 minute)'),
});
});

describe('with default settings', () => {
beforeEach(() => {
rule.addTarget(new targets.FirehoseStream(stream));
});

test("adds the stream's ARN and role to the targets of the rule", () => {
expect(stack).to(haveResource('AWS::Events::Rule', {
Targets: [
{
Arn: streamArn,
Id: 'Target0',
RoleArn: { 'Fn::GetAtt': ['MyStreamEventsRole5B6CC6AF', 'Arn'] },
},
],
}));
});

test("creates a policy that has PutRecord and PutRecords permissions on the stream's ARN", () => {
expect(stack).to(haveResource('AWS::IAM::Policy', {
PolicyDocument: {
Statement: [
{
Action: ['firehose:PutRecord', 'firehose:PutRecordBatch'],
Effect: 'Allow',
Resource: streamArn,
},
],
Version: '2012-10-17',
},
}));
});
});

describe('with an explicit message', () => {
beforeEach(() => {
rule.addTarget(new targets.FirehoseStream(stream, {
message: events.RuleTargetInput.fromText('fooBar'),
}));
});

test('sets the input', () => {
expect(stack).to(haveResourceLike('AWS::Events::Rule', {
Targets: [
{
Arn: streamArn,
Id: 'Target0',
Input: '"fooBar"',
},
],
}));
});
});
});
});

0 comments on commit 78950a1

Please sign in to comment.