Start a Workflow within a Workflow (Step Functions, Lambda) - AWS Step Functions

Start a Workflow within a Workflow (Step Functions, Lambda)

This sample project demonstrates how to use an AWS Step Functions state machine to launch other state machine executions. See Start Workflow Executions from a Task State.

Create the State Machine and Provision Resources

  1. Open the Step Functions console and choose Create a state machine.

  2. Choose Sample Projects, and then choose Start a Workflow Within a Workflow.

    The state machine Code and Visual Workflow are displayed.

    
          Start a workflow.
  3. Choose Next.

    The Deploy resources page is displayed, listing the resources that will be created. For this sample project, the resources include:

    • An additional Step Functions state machine

    • A Lambda function for callback

  4. Choose Deploy Resources.

    Note

    It can take up to 10 minutes for these resources and related IAM permissions to be created. While the Deploy resources page is displayed, you can open the Stack ID link to see which resources are being provisioned.

Start a New Execution

  1. On the New execution page, enter an execution name (optional), and then choose Start Execution.

  2. (Optional) To help identify your execution, you can specify an ID for it in the Enter an execution name box. If you don't enter an ID, Step Functions generates a unique ID automatically.

    Note

    Step Functions allows you to create state machine, execution, and activity names that contain non-ASCII characters. These non-ASCII names don't work with Amazon CloudWatch. To ensure that you can track CloudWatch metrics, choose a name that uses only ASCII characters.

  3. Optionally, you can go to the newly created state machine on the Step Functions Dashboard, and then choose New execution.

  4. When an execution is complete, you can select states on the Visual workflow and browse the Input and Output under Step details.

Example State Machine Code

The state machine in this sample project integrates another state machine and AWS Lambda by passing parameters directly to those resources.

Browse through this example state machine to see how Step Functions calls the StartExecution API action for the other state machine. It launches two instances of the other state machine in parallel: one using the Run a Job pattern and one using the Wait for a Callback with the Task Token pattern.

For more information about how AWS Step Functions can control other AWS services, see Service Integrations with AWS Step Functions .

{ "Comment": "An example of combining workflows using a Step Functions StartExecution task state with various integration patterns.", "StartAt": "Start new workflow and continue", "States": { "Start new workflow and continue": { "Comment": "Start an execution of another Step Functions state machine and continue", "Type": "Task", "Resource": "arn:aws:states:::states:startExecution", "Parameters": { "StateMachineArn": "arn:aws:states:us-east-1:123456789012:stateMachine:NestingPatternAnotherStateMachine-HZ9gtgspmdun", "Input": { "NeedCallback": false, "AWS_STEP_FUNCTIONS_STARTED_BY_EXECUTION_ID.$": "$$.Execution.Id" } }, "Next": "Start in parallel" }, "Start in parallel": { "Comment": "Start two executions of the same state machine in parallel", "Type": "Parallel", "End": true, "Branches": [ { "StartAt": "Start new workflow and wait for completion", "States": { "Start new workflow and wait for completion": { "Comment": "Start an execution of the same 'NestingPatternAnotherStateMachine' and wait for its completion", "Type": "Task", "Resource": "arn:aws:states:::states:startExecution.sync", "Parameters": { "StateMachineArn": "arn:aws:states:us-east-1:123456789012:stateMachine:NestingPatternAnotherStateMachine-HZ9gtgspmdun", "Input": { "NeedCallback": false, "AWS_STEP_FUNCTIONS_STARTED_BY_EXECUTION_ID.$": "$$.Execution.Id" } }, "OutputPath": "$.Output", "End": true } } }, { "StartAt": "Start new workflow and wait for callback", "States": { "Start new workflow and wait for callback": { "Comment": "Start an execution and wait for it to call back with a task token", "Type": "Task", "Resource": "arn:aws:states:::states:startExecution.waitForTaskToken", "Parameters": { "StateMachineArn": "arn:aws:states:us-east-1:123456789012:stateMachine:NestingPatternAnotherStateMachine-HZ9gtgspmdun", "Input": { "NeedCallback": true, "AWS_STEP_FUNCTIONS_STARTED_BY_EXECUTION_ID.$": "$$.Execution.Id", "TaskToken.$": "$$.Task.Token" } }, "End": true } } } ] } } }

For information about how to configure IAM when using Step Functions with other AWS services, see IAM Policies for Integrated Services.