Step Functions 의 상태(State) 용어에 대한 설명과 사용법에 대해 정리해봤습니다.

2022.02.24

この記事は公開されてから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
    }
  }
}