-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(pipes-targets): add EventBridge (#30654)
Add EventBridge event bus as a Pipes target.
- Loading branch information
Showing
16 changed files
with
34,187 additions
and
5 deletions.
There are no files selected for viewing
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
132 changes: 132 additions & 0 deletions
132
packages/@aws-cdk/aws-pipes-targets-alpha/lib/event-bridge.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,132 @@ | ||
import { IInputTransformation, IPipe, ITarget, TargetConfig } from '@aws-cdk/aws-pipes-alpha'; | ||
import { Token } from 'aws-cdk-lib'; | ||
import { IEventBus } from 'aws-cdk-lib/aws-events'; | ||
import { IRole } from 'aws-cdk-lib/aws-iam'; | ||
|
||
/** | ||
* EventBridge target properties. | ||
*/ | ||
export interface EventBridgeTargetParameters { | ||
/** | ||
* The input transformation to apply to the message before sending it to the target. | ||
* | ||
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-pipes-pipe-pipetargetparameters.html#cfn-pipes-pipe-pipetargetparameters-inputtemplate | ||
* @default - none | ||
*/ | ||
readonly inputTransformation?: IInputTransformation; | ||
|
||
/** | ||
* A free-form string used to decide what fields to expect in the event detail. | ||
* | ||
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-pipes-pipe-pipetargeteventbridgeeventbusparameters.html#cfn-pipes-pipe-pipetargeteventbridgeeventbusparameters-detailtype | ||
* @default - none | ||
*/ | ||
readonly detailType?: string; | ||
|
||
/** | ||
* The URL subdomain of the endpoint. | ||
* | ||
* @example | ||
* // if the URL for the endpoint is https://abcde.veo.endpoints.event.amazonaws.com | ||
* 'abcde.veo' | ||
* | ||
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-pipes-pipe-pipetargeteventbridgeeventbusparameters.html#cfn-pipes-pipe-pipetargeteventbridgeeventbusparameters-endpointid | ||
* @default - none | ||
*/ | ||
readonly endpointId?: string; | ||
|
||
/** | ||
* AWS resources, identified by Amazon Resource Name (ARN), which the event primarily concerns. | ||
* | ||
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-pipes-pipe-pipetargeteventbridgeeventbusparameters.html#cfn-pipes-pipe-pipetargeteventbridgeeventbusparameters-resources | ||
* @default - none | ||
*/ | ||
readonly resources?: string[]; | ||
|
||
/** | ||
* The source of the event. | ||
* | ||
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-pipes-pipe-pipetargeteventbridgeeventbusparameters.html#cfn-pipes-pipe-pipetargeteventbridgeeventbusparameters-source | ||
* @default - none | ||
*/ | ||
readonly source?: string; | ||
|
||
/** | ||
* The time stamp of the event, per RFC3339. | ||
* | ||
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-pipes-pipe-pipetargeteventbridgeeventbusparameters.html#cfn-pipes-pipe-pipetargeteventbridgeeventbusparameters-time | ||
* @default - the time stamp of the PutEvents call | ||
*/ | ||
readonly time?: string; | ||
} | ||
|
||
/** | ||
* An EventBridge Pipes target that sends messages to an EventBridge event bus. | ||
*/ | ||
export class EventBridgeTarget implements ITarget { | ||
private eventBus: IEventBus; | ||
private eventBridgeParameters?: EventBridgeTargetParameters; | ||
public readonly targetArn: string; | ||
|
||
constructor(eventBus: IEventBus, parameters?: EventBridgeTargetParameters) { | ||
this.eventBus = eventBus; | ||
this.targetArn = eventBus.eventBusArn; | ||
if (parameters) { | ||
this.eventBridgeParameters = parameters; | ||
for (const validate of [validateDetailType, validateEndpointId, validateSource, validateTime]) { | ||
validate(parameters); | ||
} | ||
} | ||
} | ||
|
||
grantPush(grantee: IRole): void { | ||
this.eventBus.grantPutEventsTo(grantee); | ||
} | ||
|
||
bind(pipe: IPipe): TargetConfig { | ||
if (!this.eventBridgeParameters) { | ||
return { | ||
targetParameters: {}, | ||
}; | ||
} | ||
|
||
return { | ||
targetParameters: { | ||
inputTemplate: this.eventBridgeParameters.inputTransformation?.bind(pipe).inputTemplate, | ||
eventBridgeEventBusParameters: this.eventBridgeParameters, | ||
}, | ||
}; | ||
} | ||
} | ||
|
||
function validateDetailType({ detailType }: EventBridgeTargetParameters) { | ||
if (detailType !== undefined && !Token.isUnresolved(detailType)) { | ||
if (detailType.length < 1 || detailType.length > 128) { | ||
throw new Error(`Detail type must be between 1 and 128 characters, received ${detailType.length}`); | ||
} | ||
} | ||
} | ||
|
||
function validateEndpointId({ endpointId }: EventBridgeTargetParameters) { | ||
if (endpointId !== undefined && !Token.isUnresolved(endpointId)) { | ||
if (endpointId.length < 1 || endpointId.length > 50) { | ||
throw new Error(`Endpoint id must be between 1 and 50 characters, received ${endpointId.length}`); | ||
} | ||
} | ||
} | ||
|
||
function validateSource({ source }: EventBridgeTargetParameters) { | ||
if (source !== undefined && !Token.isUnresolved(source)) { | ||
if (source.length < 1 || source.length > 256) { | ||
throw new Error(`Source must be between 1 and 256 characters, received ${source.length}`); | ||
} | ||
} | ||
} | ||
|
||
function validateTime({ time }: EventBridgeTargetParameters) { | ||
if (time !== undefined && !Token.isUnresolved(time)) { | ||
if (time.length < 1 || time.length > 256) { | ||
throw new Error(`Time must be between 1 and 256 characters, received ${time.length}`); | ||
} | ||
} | ||
} |
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
54 changes: 54 additions & 0 deletions
54
packages/@aws-cdk/aws-pipes-targets-alpha/test/__snapshots__/event-bridge.test.ts.snap
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,54 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`EventBridge should grant pipe role push access 1`] = ` | ||
{ | ||
"MyPipeRoleCBC8E9AB": { | ||
"Properties": { | ||
"AssumeRolePolicyDocument": { | ||
"Statement": [ | ||
{ | ||
"Action": "sts:AssumeRole", | ||
"Effect": "Allow", | ||
"Principal": { | ||
"Service": "pipes.amazonaws.com", | ||
}, | ||
}, | ||
], | ||
"Version": "2012-10-17", | ||
}, | ||
}, | ||
"Type": "AWS::IAM::Role", | ||
}, | ||
} | ||
`; | ||
|
||
exports[`EventBridge should grant pipe role push access 2`] = ` | ||
{ | ||
"MyPipeRoleDefaultPolicy31387C20": { | ||
"Properties": { | ||
"PolicyDocument": { | ||
"Statement": [ | ||
{ | ||
"Action": "events:PutEvents", | ||
"Effect": "Allow", | ||
"Resource": { | ||
"Fn::GetAtt": [ | ||
"MyEventBus251E60F8", | ||
"Arn", | ||
], | ||
}, | ||
}, | ||
], | ||
"Version": "2012-10-17", | ||
}, | ||
"PolicyName": "MyPipeRoleDefaultPolicy31387C20", | ||
"Roles": [ | ||
{ | ||
"Ref": "MyPipeRoleCBC8E9AB", | ||
}, | ||
], | ||
}, | ||
"Type": "AWS::IAM::Policy", | ||
}, | ||
} | ||
`; |
Oops, something went wrong.