(stepfunctions-tasks): StartSyncExecution
- Dominant language
- TypeScript
- Stars
- 12.9k
- Forks
- 4.6k
- Avg merge
- 2d 3h
- Merged PRs (30d)
- 83
Description
### Describe the feature
A class for a step function task which contains the cloudformation to start a synchronous excecution of another step function.
### Use Case
When you are using express step functions (because they are cheaper for short executions) and you need a result from the step function, you can use the StartSyncExecution workflow described here: https://docs.aws.amazon.com/step-functions/latest/apireference/API_StartSyncExecution.html.
### Proposed Solution
I copied the existent version of the StartExecution and removed the parts which I did not think was relevant (I likely do not have enough experience within CDK to be the least responsible for this, so it should likely be viewed as a proof of concept and nothing else).
```import * as iam from 'aws-cdk-lib/aws-iam';
import * as sfn from 'aws-cdk-lib/aws-stepfunctions';
import { Construct } from 'constructs';
/**
* Properties for StartExecution
*/
export interface StepFunctionsStartSyncExecutionProps extends sfn.TaskStateBaseProps {
/**
* The Step Functions state machine to start the execution on.
*/
readonly stateMachine: sfn.IStateMachine;
/**
* The JSON input for the execution, same as that of StartExecution.
*
* @see https://docs.aws.amazon.com/step-functions/latest/apireference/API_StartExecution.html
*
* @default - The state input (JSON path '$')
*/
readonly input?: sfn.TaskInput;
/**
* The name of the execution, same as that of StartExecution.
*
* @see https://docs.aws.amazon.com/step-functions/latest/apireference/API_StartExecution.html
*
* @default - None
*/
readonly name?: string;
/**
* Pass the execution ID from the context object to the execution input.
* This allows the Step Functions UI to link child executions from parent executions, making it easier to trace execution flow across state machines.
*
* If you set this property to `true`, the `input` property must be an object (provided by `sfn.TaskInput.fromObject`) or omitted entirely.
*
* @see https://docs.aws.amazon.com/step-functions/latest/dg/concepts-nested-workflows.html#nested-execution-startid
*
* @default - false
*/
readonly associateWithParent?: boolean;
}
/**
* A Step Functions Task to call StartSyncExecution on another state machine.
*
*/
export class StepFunctionsStartSyncExecution extends sfn.TaskStateBase {
protected readonly taskMetrics?: sfn.TaskMetricsConfig;
protected readonly taskPolicies?: iam.PolicyStatement[];
constructor(scope: Construct, id: string, private readonly props: StepFunctionsStartSyncExecutionProps) {
super(scope, id, props);
this.taskPolicies = this.createScopedAccessPolicy();
}
/**
* @internal
*/
protected _renderTask(): any {
// suffix of ':2' indicates that the output of the nested state machine should be JSON
// suffix is only applicable when waiting for a nested state machine to complete (RUN_JOB)
// https://docs.aws.amazon.com/step-functions/latest/dg/connect-stepfunctions.html
let input: any;
if (this.props.associateWithParent) {
const associateWithParentEntry = {
AWS_STEP_FUNCTIONS_STARTED_BY_EXECUTION_ID: sfn.JsonPath.stringAt('$$.Execution.Id'),
};
input = this.props.input ? { ...this.props.input.value, ...associateWithParentEntry } : associateWithParentEntry;
} else {
input = this.props.input ? this.props.input.value: sfn.TaskInput.fromJsonPathAt('$').value;
}
return {
Resource: "arn:aws:states:::aws-sdk:sfn:startSyncExecution",
Parameters: sfn.FieldUtils.renderObject({
Input: input,
StateMachineArn: this.props.stateMachine.stateMachineArn,
Name: this.props.name,
}),
};
}
/**
* As StateMachineArn is extracted automatically from the state machine object included in the constructor,
*
* the scoped access policy should be generated accordingly.
*
* This means the action of StartExecution should be restricted on the given state machine, instead of being granted to all the resources (*).
*/
private createScopedAccessPolicy(): iam.PolicyStatement[] {
const policyStatements = [
new iam.PolicyStatement({
actions: ['states:StartSyncExecution'],
resources: [this.props.stateMachine.stateMachineArn],
}),
];
return policyStatements;
}
}
```
### Other Information
_No response_
### Acknowledgements
- [X] I may be able to implement this feature request
- [ ] This feature might incur a breaking change
### CDK version used
2.19.0
### Environment details (OS name and version, etc.)
Windows 11
Contributor guide
Research direction
Start by comparing the existing StartExecution implementation with the proposed StepFunctionsStartSyncExecution class and the AWS StartSyncExecution API documentation linked in the issue. Review how the construct should render its task and scoped IAM policy, then verify the synthesized CloudFormation and permissions against the requested synchronous execution behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- aws, typescript
- Domain
- cloud, infrastructure
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100