|
| 1 | +import * as iam from '@aws-cdk/aws-iam'; |
| 2 | +import * as sfn from '@aws-cdk/aws-stepfunctions'; |
| 3 | +import { Construct, Stack } from '@aws-cdk/core'; |
| 4 | +import { integrationResourceArn, validatePatternSupported } from '../private/task-utils'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Properties for StartExecution |
| 8 | + */ |
| 9 | +export interface StepFunctionsStartExecutionProps extends sfn.TaskStateBaseProps { |
| 10 | + /** |
| 11 | + * The Step Functions state machine to start the execution on. |
| 12 | + */ |
| 13 | + readonly stateMachine: sfn.IStateMachine; |
| 14 | + |
| 15 | + /** |
| 16 | + * The JSON input for the execution, same as that of StartExecution. |
| 17 | + * |
| 18 | + * @see https://docs.aws.amazon.com/step-functions/latest/apireference/API_StartExecution.html |
| 19 | + * |
| 20 | + * @default - The state input (JSON path '$') |
| 21 | + */ |
| 22 | + readonly input?: sfn.TaskInput; |
| 23 | + |
| 24 | + /** |
| 25 | + * The name of the execution, same as that of StartExecution. |
| 26 | + * |
| 27 | + * @see https://docs.aws.amazon.com/step-functions/latest/apireference/API_StartExecution.html |
| 28 | + * |
| 29 | + * @default - None |
| 30 | + */ |
| 31 | + readonly name?: string; |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * A Step Functions Task to call StartExecution on another state machine. |
| 36 | + * |
| 37 | + * It supports three service integration patterns: FIRE_AND_FORGET, SYNC and WAIT_FOR_TASK_TOKEN. |
| 38 | + */ |
| 39 | +export class StepFunctionsStartExecution extends sfn.TaskStateBase { |
| 40 | + private static readonly SUPPORTED_INTEGRATION_PATTERNS = [ |
| 41 | + sfn.IntegrationPattern.REQUEST_RESPONSE, |
| 42 | + sfn.IntegrationPattern.RUN_JOB, |
| 43 | + sfn.IntegrationPattern.WAIT_FOR_TASK_TOKEN, |
| 44 | + ]; |
| 45 | + |
| 46 | + protected readonly taskMetrics?: sfn.TaskMetricsConfig; |
| 47 | + protected readonly taskPolicies?: iam.PolicyStatement[]; |
| 48 | + |
| 49 | + private readonly integrationPattern: sfn.IntegrationPattern; |
| 50 | + |
| 51 | + constructor(scope: Construct, id: string, private readonly props: StepFunctionsStartExecutionProps) { |
| 52 | + super(scope, id, props); |
| 53 | + |
| 54 | + this.integrationPattern = props.integrationPattern || sfn.IntegrationPattern.REQUEST_RESPONSE; |
| 55 | + validatePatternSupported(this.integrationPattern, StepFunctionsStartExecution.SUPPORTED_INTEGRATION_PATTERNS); |
| 56 | + |
| 57 | + if (this.integrationPattern === sfn.IntegrationPattern.WAIT_FOR_TASK_TOKEN && !sfn.FieldUtils.containsTaskToken(props.input)) { |
| 58 | + throw new Error('Task Token is required in `input` for callback. Use Context.taskToken to set the token.'); |
| 59 | + } |
| 60 | + |
| 61 | + this.taskPolicies = this.createScopedAccessPolicy(); |
| 62 | + } |
| 63 | + |
| 64 | + protected renderTask(): any { |
| 65 | + // suffix of ':2' indicates that the output of the nested state machine should be JSON |
| 66 | + // suffix is only applicable when waiting for a nested state machine to complete (RUN_JOB) |
| 67 | + // https://docs.aws.amazon.com/step-functions/latest/dg/connect-stepfunctions.html |
| 68 | + const suffix = this.integrationPattern === sfn.IntegrationPattern.RUN_JOB ? ':2' : ''; |
| 69 | + return { |
| 70 | + Resource: `${integrationResourceArn('states', 'startExecution', this.integrationPattern)}${suffix}`, |
| 71 | + Parameters: sfn.FieldUtils.renderObject({ |
| 72 | + Input: this.props.input ? this.props.input.value : sfn.TaskInput.fromDataAt('$').value, |
| 73 | + StateMachineArn: this.props.stateMachine.stateMachineArn, |
| 74 | + Name: this.props.name, |
| 75 | + }), |
| 76 | + }; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * As StateMachineArn is extracted automatically from the state machine object included in the constructor, |
| 81 | + * |
| 82 | + * the scoped access policy should be generated accordingly. |
| 83 | + * |
| 84 | + * This means the action of StartExecution should be restricted on the given state machine, instead of being granted to all the resources (*). |
| 85 | + */ |
| 86 | + private createScopedAccessPolicy(): iam.PolicyStatement[] { |
| 87 | + const stack = Stack.of(this); |
| 88 | + |
| 89 | + const policyStatements = [ |
| 90 | + new iam.PolicyStatement({ |
| 91 | + actions: ['states:StartExecution'], |
| 92 | + resources: [this.props.stateMachine.stateMachineArn], |
| 93 | + }), |
| 94 | + ]; |
| 95 | + |
| 96 | + // Step Functions use Cloud Watch managed rules to deal with synchronous tasks. |
| 97 | + if (this.integrationPattern === sfn.IntegrationPattern.RUN_JOB) { |
| 98 | + policyStatements.push( |
| 99 | + new iam.PolicyStatement({ |
| 100 | + actions: ['states:DescribeExecution', 'states:StopExecution'], |
| 101 | + // https://docs.aws.amazon.com/step-functions/latest/dg/concept-create-iam-advanced.html#concept-create-iam-advanced-execution |
| 102 | + resources: [ |
| 103 | + stack.formatArn({ |
| 104 | + service: 'states', |
| 105 | + resource: 'execution', |
| 106 | + sep: ':', |
| 107 | + resourceName: `${stack.parseArn(this.props.stateMachine.stateMachineArn, ':').resourceName}*`, |
| 108 | + }), |
| 109 | + ], |
| 110 | + }), |
| 111 | + ); |
| 112 | + |
| 113 | + policyStatements.push( |
| 114 | + new iam.PolicyStatement({ |
| 115 | + actions: ['events:PutTargets', 'events:PutRule', 'events:DescribeRule'], |
| 116 | + resources: [ |
| 117 | + stack.formatArn({ |
| 118 | + service: 'events', |
| 119 | + resource: 'rule', |
| 120 | + resourceName: 'StepFunctionsGetEventsForStepFunctionsExecutionRule', |
| 121 | + }), |
| 122 | + ], |
| 123 | + }), |
| 124 | + ); |
| 125 | + } |
| 126 | + |
| 127 | + return policyStatements; |
| 128 | + } |
| 129 | +} |
0 commit comments