Skip to content

Commit

Permalink
feat(pipes-targets): add step function target
Browse files Browse the repository at this point in the history
Co-authored-by: RaphaelManke <RaphaelManke@users.noreply.github.com>
  • Loading branch information
WtfJoke and RaphaelManke committed Apr 28, 2024
1 parent 247aa35 commit 141dbf5
Show file tree
Hide file tree
Showing 16 changed files with 33,972 additions and 1 deletion.
46 changes: 45 additions & 1 deletion packages/@aws-cdk/aws-pipes-targets-alpha/README.md
Expand Up @@ -27,6 +27,11 @@ For more details see the service documentation:

Pipe targets are the end point of a EventBridge Pipe.

The following targets are supported:

1. `targets.SqsTarget`: [Send event source to a Queue](#amazon-sqs)
2. `targets.StepFunctionTarget`: [Start a Step function from an event source](#aws-step-function)

### Amazon SQS

A SQS message queue can be used as a target for a pipe. Messages will be pushed to the queue.
Expand All @@ -43,7 +48,7 @@ const pipe = new pipes.Pipe(this, 'Pipe', {
});
```

The target configuration can be transformed:
The target input can be transformed:

```ts
declare const sourceQueue: sqs.Queue;
Expand All @@ -63,3 +68,42 @@ const pipe = new pipes.Pipe(this, 'Pipe', {
target: pipeTarget
});
```

### AWS Step Functions

A Step Function can be used as a target for a pipe. Step Functions will be invoked with the (enriched/filtered) source payload.

```ts
declare const sourceQueue: sqs.Queue;
declare const targetStateMachine: sfn.IStateMachine;

const pipeTarget = new targets.StepFunctionTarget(targetStateMachine,
{
invocationType: targets.StepFunctionInvocationType.FIRE_AND_FORGET,
}
);

const pipe = new pipes.Pipe(this, 'Pipe', {
source: new SomeSource(sourceQueue),
target: pipeTarget
});
```

The target input can be transformed:

```ts
declare const sourceQueue: sqs.Queue;
declare const targetStateMachine: sfn.IStateMachine;

const pipeTarget = new targets.StepFunctionTarget(targetStateMachine,
{
inputTransformation: pipes.InputTransformation.fromObject({ body: '<$.body>' }),
invocationType: targets.StepFunctionInvocationType.FIRE_AND_FORGET,
}
);

const pipe = new pipes.Pipe(this, 'Pipe', {
source: new SomeSource(sourceQueue),
target: pipeTarget
});
```
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-pipes-targets-alpha/lib/index.ts
@@ -1 +1,2 @@
export * from './sqs';
export * from './stepfunctions';
83 changes: 83 additions & 0 deletions packages/@aws-cdk/aws-pipes-targets-alpha/lib/stepfunctions.ts
@@ -0,0 +1,83 @@
import { IInputTransformation, IPipe, ITarget, TargetConfig } from '@aws-cdk/aws-pipes-alpha';
import { IRole } from 'aws-cdk-lib/aws-iam';
import * as sfn from 'aws-cdk-lib/aws-stepfunctions';
import { StateMachine, StateMachineType } from 'aws-cdk-lib/aws-stepfunctions';

/**
* StateMachine StepFunction target properties.
*/
export interface StateMachineTargetParameters {
/**
* 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;

/**
* Specify whether to invoke the Step Functions state machine synchronously (`REQUEST_RESPONSE`) or asynchronously (`FIRE_AND_FORGET`).
*
* @see http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-pipes-pipe-pipetargetsqsqueueparameters.html#cfn-pipes-pipe-pipetargetsqsqueueparameters-messagededuplicationid
*/
readonly invocationType: StepFunctionInvocationType;
}

/**
* InvocationType for invoking StepFunction.
* @see https://docs.aws.amazon.com/eventbridge/latest/pipes-reference/API_PipeTargetStateMachineParameters.html
*/
export enum StepFunctionInvocationType {
/**
* Invoke StepFunction asynchronously (`StartExecution`). See https://docs.aws.amazon.com/step-functions/latest/apireference/API_StartExecution.html for more details.
*/
FIRE_AND_FORGET = 'FIRE_AND_FORGET',

/**
* Invoke StepFunction synchronously (`StartSyncExecution`) and wait for the execution to complete. See https://docs.aws.amazon.com/step-functions/latest/apireference/API_StartSyncExecution.html for more details.
*/
REQUEST_RESPONSE = 'REQUEST_RESPONSE',
}

/**
* An EventBridge Pipes target that sends messages to a StepFunction.
*/
export class StepFunctionTarget implements ITarget {
public readonly targetArn: string;

private readonly stateMachine: sfn.IStateMachine;
private readonly invocationType: StepFunctionInvocationType;
private readonly inputTemplate?: IInputTransformation;

constructor(stateMachine: sfn.IStateMachine, parameters: StateMachineTargetParameters) {
this.stateMachine = stateMachine;
this.targetArn = stateMachine.stateMachineArn;
this.invocationType = parameters.invocationType;
this.inputTemplate = parameters.inputTransformation;

if (this.stateMachine instanceof StateMachine
&& this.stateMachine.stateMachineType === StateMachineType.STANDARD
&& this.invocationType === StepFunctionInvocationType.REQUEST_RESPONSE) {
throw new Error('Standard Workflows do not support REQUEST_RESPONSE invocation type');
}
}

grantPush(grantee: IRole): void {
if (this.invocationType === StepFunctionInvocationType.FIRE_AND_FORGET) {
this.stateMachine.grantStartExecution(grantee);
} else {
this.stateMachine.grantStartSyncExecution(grantee);
}
}

bind(pipe: IPipe): TargetConfig {
return {
targetParameters: {
inputTemplate: this.inputTemplate?.bind(pipe).inputTemplate,
stepFunctionStateMachineParameters: {
invocationType: this.invocationType,
},
},
};
}
}
@@ -1,6 +1,7 @@
// Fixture with packages imported, but nothing else
import * as cdk from 'aws-cdk-lib';
import * as sqs from 'aws-cdk-lib/aws-sqs';
import * as sfn from 'aws-cdk-lib/aws-stepfunctions';
import { Construct } from 'constructs';
import * as pipes from '@aws-cdk/aws-pipes-alpha';
import * as targets from '@aws-cdk/aws-pipes-targets-alpha';
Expand Down
@@ -0,0 +1,95 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`step-function should grant pipe role push access (StartAsyncExecution) with invocation type FIRE_AND_FORGET 1`] = `
{
"MySfnPipeRoleF1D0F697": {
"Properties": {
"AssumeRolePolicyDocument": {
"Statement": [
{
"Action": "sts:AssumeRole",
"Effect": "Allow",
"Principal": {
"Service": "pipes.amazonaws.com",
},
},
],
"Version": "2012-10-17",
},
},
"Type": "AWS::IAM::Role",
},
"MyStateMachineRoleD59FFEBC": {
"Properties": {
"AssumeRolePolicyDocument": {
"Statement": [
{
"Action": "sts:AssumeRole",
"Effect": "Allow",
"Principal": {
"Service": {
"Fn::FindInMap": [
"ServiceprincipalMap",
{
"Ref": "AWS::Region",
},
"states",
],
},
},
},
],
"Version": "2012-10-17",
},
},
"Type": "AWS::IAM::Role",
},
}
`;

exports[`step-function should grant pipe role push access (StartSyncExecution) with invocation type REQUEST-RESPONSE 1`] = `
{
"MySfnPipeRoleF1D0F697": {
"Properties": {
"AssumeRolePolicyDocument": {
"Statement": [
{
"Action": "sts:AssumeRole",
"Effect": "Allow",
"Principal": {
"Service": "pipes.amazonaws.com",
},
},
],
"Version": "2012-10-17",
},
},
"Type": "AWS::IAM::Role",
},
"MyStateMachineRoleD59FFEBC": {
"Properties": {
"AssumeRolePolicyDocument": {
"Statement": [
{
"Action": "sts:AssumeRole",
"Effect": "Allow",
"Principal": {
"Service": {
"Fn::FindInMap": [
"ServiceprincipalMap",
{
"Ref": "AWS::Region",
},
"states",
],
},
},
},
],
"Version": "2012-10-17",
},
},
"Type": "AWS::IAM::Role",
},
}
`;

0 comments on commit 141dbf5

Please sign in to comment.