Step Functionsステートマシンから別のStep Functionsを呼び出す

公開日時

Step Functions利用時にステートマシン内から別のステートマシンを呼び出したかったので試した。

  • Step Functionsページにチュートリアルとして用意されているHello Worldステートマシンを作成
  • 作成後のHelloWorldステートマシンをコピーしてHelloWorld2ステートマシンを作成 step1
  • 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
    }
}
  • この状態で保存しようとすると、他のステートマシンを呼び出す権限がないとのエラーになる
step2
  • AWS Step Functionsに記載されている権限を StepFunctions-HelloWorld-role-xxxxx IAMロールに追加する
{
    "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
}
step3

この方法を使って複雑なステートマシンを小さいステートマシンに分割しておけばテストと確認がやりやすくなる。

参考


Related #aws

RDSを定期的に停止するLambdaを作る

手動起動は大変なので

AWS SESの受信メールを暗号化してs3に保存しLambdaで読み込む

jsの場合、複合処理を独自実装する必要がある

CloudWatchアラームを一時的に無効化する

AWS CLIで設定する必要がある

Alexaに気温と二酸化炭素濃度を教えてもらう

「Alexa、気温」でセンサー情報を教えてくれるようになった

CloudWatchのカスタムメトリクスを減らす

あまりチェックしないセンサー値の送信を止めた