Step Functionsステートマシンから別のStep Functionsを呼び出す
Step Functions利用時にステートマシン内から別のステートマシンを呼び出したかったので試した。
- Step Functionsページにチュートリアルとして用意されているHello Worldステートマシンを作成
- 作成後のHelloWorldステートマシンをコピーしてHelloWorld2ステートマシンを作成
 
- HelloWorldステートマシン定義を編集して、Parallel State実行後にHelloWorld2ステートマシンを呼び出すように
{
    // ...
    "Parallel State": {
      "Comment": "A Parallel state can be used to create parallel branches of execution in your state machine.",
      "Type": "Parallel",
      "Next": "Call Hello World",
      "Branches": [
        {
          "StartAt": "Hello",
          "States": {
            "Hello": {
              "Type": "Pass",
              "End": true
            }
          }
        },
        {
          "StartAt": "World",
          "States": {
            "World": {
              "Type": "Pass",
              "End": true
            }
          }
        }
      ]
    },
    "Call Hello World": {
      "Type": "Task",
      "Resource": "arn:aws:states:::states:startExecution.sync:2",
      "Parameters": {
        "StateMachineArn": "arn:aws:states:[region]:[account]:stateMachine:HelloWorld2",
        "Input": {
          "IsHelloWorldExample": true
        }
      },
      "Next": "Success"
    },
    "Success": {
      "Type": "Pass",
      "End": true
    }
}- この状態で保存しようとすると、他のステートマシンを呼び出す権限がないとのエラーになる

- AWS Step Functionsに記載されている権限を StepFunctions-HelloWorld-role-xxxxxIAMロールに追加する
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "states:StartExecution"
            ],
            "Resource": [
                "arn:aws:states:[region]:[account]:stateMachine:HelloWorld2"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "states:DescribeExecution",
                "states:StopExecution"
            ],
            "Resource": "*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "events:PutTargets",
                "events:PutRule",
                "events:DescribeRule"
            ],
            "Resource": [
                "arn:aws:events:[region]:[account]:rule/StepFunctionsGetEventsForStepFunctionsExecutionRule"
            ]
        }
    ]
}- 権限付与後に↓のInputでHelloWorldステートマシンを実行すると、Parallel State実行後にHelloWorld2ステートマシンが実行される
{
    "IsHelloWorldExample": true
}
この方法を使って複雑なステートマシンを小さいステートマシンに分割しておけばテストと確認がやりやすくなる。
