forked from aws/aws-cdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(aws-events-targets): Add Firehose target
closes aws#10349
- Loading branch information
Showing
4 changed files
with
137 additions
and
7 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
packages/@aws-cdk/aws-events-targets/lib/firehose-stream.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
packages/@aws-cdk/aws-events-targets/test/firehose/firehose-stream.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"', | ||
}, | ||
], | ||
})); | ||
}); | ||
}); | ||
}); | ||
}); |