Step Functionsのステータス(State)用語についての説明と使い方についてまとめてみました。

Step Functionsのステータス(State)用語についての説明と使い方についてまとめてみました。

Clock Icon2022.07.22 01:46

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

アジェンダ

  1. Amazon States Language とは
  2. 共通 State フィールド
  3. Task
  4. Choice
  5. Wait
  6. Succeed
  7. Fail
  8. Parallel
  9. Map
  10. Pass

1. Amazon States Language 란

ステートマシンを定義するのに使用されるJSONベースの構造化された言語

2. 共通 State フィールド

定義

  • Type(必須)
    • Task, Choice, Wait などのステータスを定義するときに使用する
  • Next
    • 現在のステータスを終了し、次のステータスに移動する
  • End
    • true なら実行処理が終了する
  • Comment
    • 説明を入力できます
  • InputPath
    • Taskに伝達する入力経路
  • OutputPath
    • 出力に伝達する経路

{
  "Comment": "An example of the Amazon States Language using wait states",
  "StartAt": "FirstState",
  "States": {
    "FirstState": {
      "Type": "Pass",
      "Result": "FirstState",
      "Next": "LambdaFunctionState"
    },
    "LambdaFunctionState": {
      "Type": "Task",
      "Resource": "arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME",
      "End": true
    }
  }
}

3. Task

定義

  • 実行される単一作業単位
  • 直接 Lambda 関数を呼び出して作業
  • 次のフィールドを使用可能
    • Resource(必須)
    • Parameters
    • ResultPath
    • Retry
    • Catch
    • TimeoutSeconds
    • HeartbeatSeconds

{
  "Comment": "An example of the Amazon States Language using a Task state.",
  "StartAt": "TaskState",
  "States": {
    "TaskState": {
      "Type": "Task",
      "Resource": "arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME",
      "End": true
    }
  }
}

4. Choice

定義

  • 分岐処理が可能で、And や Or などの比較演算子が利用可能
  • 次のフィールドを使用可能
    • Choices (必須)
    • Default

{
  "Comment": "An example of the Amazon States Language using a choice state.",
  "StartAt": "ChoiceState",
  "States": {
    "ChoiceState": {
      "Type" : "Choice",
      "Choices": [
        {
          "Variable": "$.foo",
          "NumericEquals": 1,
          "Next": "FirstMatchState"
        },
        {
          "Variable": "$.foo",
          "NumericEquals": 2,
          "Next": "SecondMatchState"
        }
      ],
      "Default": "DefaultState"
    },

    "FirstMatchState": {
      "Type" : "Task",
      "Resource": "arn:aws:lambda:REGION:ACCOUNT_ID:function:OnFirstMatch",
      "Next": "ReultState"
    },

    "SecondMatchState": {
      "Type" : "Task",
      "Resource": "arn:aws:lambda:REGION:ACCOUNT_ID:function:OnSecondMatch",
      "Next": "ReultState"
    },

    "DefaultState": {
      "Type" : "Task",
      "Resource": "arn:aws:lambda:REGION:ACCOUNT_ID:function:OnSecondMatch",
      "Next": "ReultState"
    },

    "ReultState": {
      "Type": "Task",
      "Resource": "arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME",
      "End": true
    }
  }
}

5. Wait

定義

  • 続行を指定された時間遅延させる
  • 相対時間 (状態が開始してからの秒数で指定) と絶対時間 (タイムスタンプで指定) のいずれかを選択可能
  • 次のフィールドを使用可能で、どちらかを指定すべき
    • Seconds
    • Timestamp
    • SecondsPath
    • TimestampPath

{
  "Comment": "An example of the Amazon States Language using wait states",
  "StartAt": "wait_seconds",
  "States": {
    "wait_seconds": {
      "Type": "Wait",
      "Seconds": 10,
      "Next": "wait_timestamp"
    },
    "wait_timestamp": {
      "Type": "Wait",
      "Timestamp": "2015-09-04T01:59:00Z",
      "Next": "wait_timestamp_path"
    },
    "wait_timestamp_path": {
      "Type": "Wait",
      "TimestampPath": "$.expirydate",
      "Next": "wait_seconds_path"
    },
    "wait_seconds_path": {
      "Type": "Wait",
      "SecondsPath": "$.expiryseconds",
      "End": true
    }
  }
}

6. Succeed

定義

  • 実行を正常に中止
  • 処理が終了した状態になるため、 End フィールドや Next フィールドは必要ない

{
    "Comment": "An example of the Amazon States Language using Succeed states",
    "StartAt": "Process1",
    "States": {
        "Process1": {
            "Type": "Task",
            "Resource": "arn:aws:lambda:us-east-1:XXXXXXXXXXXX:function:Process1",
            "Next": "ChoiceStep"
        },
        "ChoiceStep": {
            "Type": "Choice",
            "Choices": [{
                "Variable": "$.processResult",
                "StringEquals": "SuccessProcess",
                "Next": "SuccessProcess"
            }, {
                "Variable": "$.processResult",
                "StringEquals": "Process2",
                "Next": "Process2"
            }]
        },
        "SuccessProcess": {
            "Type": "Succeed"
        },
        "Process2": {
            "Type": "Task",
            "Resource": "arn:aws:lambda:us-east-1:XXXXXXXXXXXX:function:Process2",
            "Catch": [{
              "ErrorEquals": [
                "HandledError"
              ],
              "Next": "FailedProcess"
            }],
            "Next": "Process3"
        },
        "FailedProcess": {
            "Type": "Fail"      
        },
        "Process3": {
            "Type": "Task",
            "Resource": "arn:aws:lambda:us-east-1:XXXXXXXXXXXX:function:Process3",
            "End": true
        }
    }
}

7. Fail

定義

  • エラーにより実行を中止
  • エラーにより処理が終了した状態になるため、 End フィールドや Next フィールドは必要ない
  • 次のフィールドを使用可能
    • Cause
    • Error

{
    "Comment": "An example of the Amazon States Language using Succeed states",
    "StartAt": "Process1",
    "States": {
        "Process1": {
            "Type": "Task",
            "Resource": "arn:aws:lambda:us-east-1:XXXXXXXXXXXX:function:Process1",
            "Next": "ChoiceStep"
        },
        "ChoiceStep": {
            "Type": "Choice",
            "Choices": [{
                "Variable": "$.processResult",
                "StringEquals": "SuccessProcess",
                "Next": "SuccessProcess"
            }, {
                "Variable": "$.processResult",
                "StringEquals": "Process2",
                "Next": "Process2"
            }]
        },
        "SuccessProcess": {
            "Type": "Succeed"
        },
        "Process2": {
            "Type": "Task",
            "Resource": "arn:aws:lambda:us-east-1:XXXXXXXXXXXX:function:Process2",
            "Catch": [{
              "ErrorEquals": [
                "HandledError"
              ],
              "Next": "FailedProcess"
            }],
            "Next": "Process3"
        },
        "FailedProcess": {
            "Type": "Fail"      
        },
        "Process3": {
            "Type": "Task",
            "Resource": "arn:aws:lambda:us-east-1:XXXXXXXXXXXX:function:Process3",
            "End": true
        }
    }
}

8. Parallel

定義

  • 並列処理が可能
  • 次のフィールドを使用可能
    • Branches (必須)
    • ResultPath
    • Retry
    • Catch

{
  "Comment": "An example of the Amazon States Language using a parallel state to execute two branches at the same time.",
  "StartAt": "Parallel",
  "States": {
    "Parallel": {
      "Type": "Parallel",
      "Next": "Final State",
      "Branches": [
        {
          "StartAt": "Wait 10s",
          "States": {
            "Wait 10s": {
              "Type": "Wait",
              "Seconds": 10,
              "End": true
            }
          }
        },
        {
          "StartAt": "Pass State",
          "States": {
            "Pass State": {
              "Type": "Pass",
              "Next": "Wait 5s"
            },
            "Wait 5s": {
              "Type": "Wait",
              "Seconds": 5,
              "End": true
            }
          }
        }
      ]
    },
    "Final State": {
      "Type": "Pass",
      "End": true
    }
  }
}

9. Map

定義

  • 動的並列処理が可能
  • 入力配列の各要素に対して同じ段階で実行可能
  • 次のフィールドを使用可能
    • Iterator (必須)
    • ItemsPath
    • MaxConcurrency
    • ResultPath
    • Retry
    • Catch

{
    "Comment": "An example of the Amazon States Language using Map states",
    "StartAt": "MapState",
    "States": {
      "MapState": {
        "Type": "Map",
        "End": true,
        "Iterator": {
          "StartAt": "Customer",
          "States": {
            "Customer": {
              "Type": "Parallel",
              "End": true,
              "Branches": [
                {
                  "StartAt": "CustomerA",
                  "States": {
                    "CustomerA": {
                      "Type": "Task",
                      "Resource": "arn:aws:lambda:us-east-1:XXXXXXXXXXXX:function:CustomerA",
                      "End": true
                    }
                  }
                },
                {
                  "StartAt": "CustomerB",
                  "States": {
                    "CustomerB": {
                      "Type": "Task",
                      "Resource": "arn:aws:lambda:us-east-1:XXXXXXXXXXXX:function:CustomerB",
                      "End": true
                    }
                  }
                }
              ]
            }
          }
        }
      }
    }
  }

10. Pass

定義

  • ワークフローを構築、デバッグする際に使用
  • 入力を処理せずに出力に伝達
  • 次のフィールドを使用可能
    • Result
    • ResultPath
    • Parameters

{
  "Comment": "An example of the Amazon States Language using a pass state.",
  "StartAt": "ChoiceState",
  "States": {
    "ChoiceState": {
      "Type" : "Choice",
      "Choices": [
        {
          "Variable": "$.foo",
          "NumericEquals": 1,
          "Next": "TaskState"
        },
        {
          "Variable": "$.foo",
          "NumericEquals": 2,
          "Next": "PassState"
        }
      ]
    },
    "TaskState": {
      "Type" : "Task",
      "Resource": "arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME",
      "Next": "ReultState"
    },
    "PassState": {
      "Type" : "Pass",
      "Result": "just pass",
      "Next": "ReultState"
    },
    "ReultState": {
      "Type": "Task",
      "Resource": "arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME",
      "End": true
    }
  }
}

この記事をシェアする

facebook logohatena logotwitter logo

© Classmethod, Inc. All rights reserved.