

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用 Step Functions 開發工作流程
<a name="developing-workflows"></a>

建議您開始在 Step Functions 主控台和 Workflow Studio 視覺化編輯器中建置工作流程。您可以從空白畫布開始，或選擇常見案例的入門範本。

建置您的工作流程需要下列任務：
+ 定義您的工作流程
+ 執行和偵錯您的工作流程
+ 部署您的工作流程

您可以使用 Amazon States Language 定義狀態機器。您可以手動建立 Amazon States Language 定義，但 Workflow Studio 會在教學課程中顯示。使用 Workflow Studio，您可以定義機器定義、視覺化和編輯步驟、執行和偵錯工作流程，以及從 Step Functions 主控台檢視所有結果。

**在 Visual Studio 程式碼中使用 Workflow Studio**  
透過 AWS 工具組，您可以從 VS 程式碼內使用 Workflow Studio，在狀態機器中視覺化、建置甚至測試個別狀態。您可以提供狀態輸入並設定變數、開始測試，然後即可查看資料的轉換方式。您可以調整工作流程並重新測試。完成後，您可以套用變更來更新狀態機器。如需詳細資訊，請參閱《》中的[使用 Workflow Studio](https://docs.aws.amazon.com/toolkit-for-vscode/latest/userguide/stepfunctions-workflowstudio.html) AWS Toolkit for Visual Studio Code。

您也可以從 AWS Command Line Interface () 使用許多 Step Functions 功能AWS CLI。例如，您可以建立狀態機器並列出現有的狀態機器。您可以在 中使用 Step Functions 命令 AWS CLI 來啟動和管理執行、輪詢活動、記錄任務活動訊號等。如需 Step Functions 命令的完整清單、可用引數的說明，以及顯示其使用方式的範例，請參閱 *AWS CLI 命令參考*。[AWS CLI 命令參考](https://docs.aws.amazon.com/cli/latest/reference/)

AWS CLI 命令會密切遵循 Amazon States Language，因此您可以使用 AWS CLI 來了解 Step Functions API 動作。您也可以使用現有的 API 知識來原型程式碼，或從命令列執行 Step Functions 動作。

**驗證狀態機器定義**  
您可以使用 API **驗證**狀態機器，並在建立工作流程之前尋找潛在問題。  
若要進一步了解驗證工作流程，請參閱 Step Functions API 參考中的 [ValidateStateMachineDefinition](https://docs.aws.amazon.com/step-functions/latest/apireference/API_ValidateStateMachineDefinition.html)。

若要開始使用最少的設定，您可以遵循[建立 Lambda 狀態機器](tutorial-creating-lambda-state-machine.md)教學課程，其中說明如何使用呼叫 Lambda 函數的單一步驟來定義工作流程，然後執行工作流程，並檢視結果。

## 定義您的工作流程
<a name="development-define"></a>

開發工作流程的第一步是以 Amazon States Language 定義步驟。根據您的偏好設定和工具，您可以使用 JSON、YAML 或字串化 Amazon States Language (ASL) 定義來定義 Step Functions 狀態機器。

下表顯示依工具支援 ASL 型定義格式。


| AWS 工具 | 支援的格式 (s) | 
| --- | --- | 
| Step Functions 主控台 | JSON | 
| HTTPS 服務 API | 分層 ASL | 
| AWS CLI | 分層 ASL | 
| Step Functions Local | 分層 ASL | 
| AWS Toolkit for Visual Studio Code | JSON、YAML | 
| AWS SAM | JSON、YAML | 
| CloudFormation | JSON、YAML、分層 ASL | 

範本狀態機器定義中的 YAML 單行註解不會轉送至建立的資源定義。如果您需要保留註解，您應該在狀態機器定義中使用 `Comment` 屬性。如需相關資訊，請參閱[狀態機器結構](statemachine-structure.md)。

使用 CloudFormation 和 AWS SAM，您可以將狀態機器定義上傳至 Amazon S3 (JSON 或 YAML 格式），並在範本中提供定義的 Amazon S3 位置。如需詳細資訊，請參閱 [AWS::StepFunctions::StateMachine S3Location](https://docs.aws.amazon.com//AWSCloudFormation/latest/UserGuide/aws-properties-stepfunctions-statemachine-s3location.html) 頁面。

下列範例 CloudFormation 範本示範如何使用不同的輸入格式提供相同的狀態機器定義。

------
#### [ JSON with Definition ]

```
{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Description": "AWS Step Functions sample template.",
  "Resources": {
    "MyStateMachine": {
      "Type": "AWS::StepFunctions::StateMachine",
      "Properties": {
        "RoleArn": {
          "Fn::GetAtt": [ "StateMachineRole", "Arn" ]
        },
        "TracingConfiguration": {
          "Enabled": true
        },
        "Definition": {
          "StartAt": "HelloWorld",
          "States": {
            "HelloWorld": {
              "Type": "Pass",
              "End": true
            }
          }
        }
      }
    },
    "StateMachineRole": {
      "Type": "AWS::IAM::Role",
      "Properties": {
        "AssumeRolePolicyDocument": {
          "Version": "2012-10-17",		 	 	 
          "Statement": [
            {
              "Action": [
                "sts:AssumeRole"
              ],
              "Effect": "Allow",
              "Principal": {
                "Service": [
                  "states.amazonaws.com"
                ]
              }
            }
          ]
        },
        "ManagedPolicyArns": [],
        "Policies": [
          {
            "PolicyName": "StateMachineRolePolicy",
            "PolicyDocument": {
              "Statement": [
                {
                  "Action": [
                    "lambda:InvokeFunction"
                  ],
                  "Resource": "*",
                  "Effect": "Allow"
                }
              ]
            }
          }
        ]
      }
    }
  },
  "Outputs": {
    "StateMachineArn": {
      "Value": {
        "Ref": "MyStateMachine"
      }
    }
  }
}
```

------
#### [ JSON with DefinitionString ]

```
{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Description": "AWS Step Functions sample template.",
  "Resources": {
    "MyStateMachine": {
      "Type": "AWS::StepFunctions::StateMachine",
      "Properties": {
        "RoleArn": {
          "Fn::GetAtt": [ "StateMachineRole", "Arn" ]
        },
        "TracingConfiguration": {
          "Enabled": true
        },
        "DefinitionString": "{\n  \"StartAt\": \"HelloWorld\",\n  \"States\": {\n    \"HelloWorld\": {\n      \"Type\": \"Pass\",\n      \"End\": true\n    }\n  }\n}"
      }
    },
    "StateMachineRole": {
      "Type": "AWS::IAM::Role",
      "Properties": {
        "AssumeRolePolicyDocument": {
          "Version": "2012-10-17",		 	 	 
          "Statement": [
            {
              "Action": [
                "sts:AssumeRole"
              ],
              "Effect": "Allow",
              "Principal": {
                "Service": [
                  "states.amazonaws.com"
                ]
              }
            }
          ]
        },
        "ManagedPolicyArns": [],
        "Policies": [
          {
            "PolicyName": "StateMachineRolePolicy",
            "PolicyDocument": {
              "Statement": [
                {
                  "Action": [
                    "lambda:InvokeFunction"
                  ],
                  "Resource": "*",
                  "Effect": "Allow"
                }
              ]
            }
          }
        ]
      }
    }
  },
  "Outputs": {
    "StateMachineArn": {
      "Value": {
        "Ref": "MyStateMachine"
      }
    }
  }
}
```

------
#### [ YAML with Definition ]

```
AWSTemplateFormatVersion: 2010-09-09
Description: AWS Step Functions sample template.
Resources:
  MyStateMachine:
    Type: 'AWS::StepFunctions::StateMachine'
    Properties:
      RoleArn: !GetAtt
        - StateMachineRole
        - Arn
      TracingConfiguration:
        Enabled: true
      Definition:
        # This is a YAML comment. This will not be preserved in the state machine resource's definition.
        Comment: This is an ASL comment. This will be preserved in the state machine resource's definition.
        StartAt: HelloWorld
        States:
          HelloWorld:
            Type: Pass
            End: true
  StateMachineRole:
    Type: 'AWS::IAM::Role'
    Properties:
      AssumeRolePolicyDocument:
        Version: 2012-10-17
        Statement:
          - Action:
              - 'sts:AssumeRole'
            Effect: Allow
            Principal:
              Service:
                - states.amazonaws.com
      ManagedPolicyArns: []
      Policies:
        - PolicyName: StateMachineRolePolicy
          PolicyDocument:
            Statement:
              - Action:
                  - 'lambda:InvokeFunction'
                Resource: "*"
                Effect: Allow

Outputs:
  StateMachineArn:
    Value:
      Ref: MyStateMachine
```

------
#### [ YAML with DefinitionString ]

```
AWSTemplateFormatVersion: 2010-09-09
Description: AWS Step Functions sample template.
Resources:
  MyStateMachine:
    Type: 'AWS::StepFunctions::StateMachine'
    Properties:
      RoleArn: !GetAtt
        - StateMachineRole
        - Arn
      TracingConfiguration:
        Enabled: true
      DefinitionString: |
        {
            "StartAt": "HelloWorld",
            "States": {
                "HelloWorld": {
                    "Type": "Pass",
                    "End": true
                }
            }
        }
  StateMachineRole:
    Type: 'AWS::IAM::Role'
    Properties:
      AssumeRolePolicyDocument:
        Version: 2012-10-17
        Statement:
          - Action:
              - 'sts:AssumeRole'
            Effect: Allow
            Principal:
              Service:
                - states.amazonaws.com
      ManagedPolicyArns: []
      Policies:
        - PolicyName: StateMachineRolePolicy
          PolicyDocument:
            Statement:
              - Action:
                  - 'lambda:InvokeFunction'
                Resource: "*"
                Effect: Allow

Outputs:
  StateMachineArn:
    Value:
      Ref: MyStateMachinele
```

------

**使用 AWS SDKs 開發工作流程**  
Step Functions 受適用於 Java、.NET、Ruby、PHP、Python (Boto 3)、JavaScript、Go 和 C\+\+ AWS SDKs 支援。這些SDKs提供以多種程式設計語言使用 Step Functions HTTPS API 動作的便利方式。您可以使用這些軟體開發套件程式庫所公開的 API 動作來開發狀態機器、活動或狀態機器啟動者。您也可以使用這些程式庫來存取可見性操作，以開發自己的 Step Functions 監控和報告工具。請參閱目前適用於 Amazon Web Services AWS SDKs 和工具參考文件。 [https://aws.amazon.com/tools/](https://aws.amazon.com/tools/)

**透過 HTTPS 請求開發工作流程**  
Step Functions 提供可透過 HTTPS 請求存取的服務操作。您可以使用這些操作，從您自己的程式庫直接與 Step Functions 通訊。您可以使用服務 API 動作來開發狀態機器、工作者或狀態機器啟動者。您也可以透過 API 動作來存取可見度操作，以開發專屬的監控和報告工具。如需詳細資訊，請參閱 [AWS Step Functions API 參考](https://docs.aws.amazon.com/step-functions/latest/apireference/)。

**使用 AWS Step Functions 資料科學 SDK 開發工作流程**  
資料科學家可以使用 SageMaker AI 和 Step Functions 建立工作流程來處理和發佈機器學習模型。您也可以在 Python 中建立多步驟機器學習工作流程，以大規模協調 AWS 基礎設施。 AWS Step Functions 資料科學 SDK 提供 Python API，可建立和叫用 Step Functions 工作流程。您可以直接在 Python 以及 Jupyter 筆記本中管理和執行這些工作流程。如需詳細資訊，請參閱：[AWS Github 上的 Step Functions 資料科學專案](https://github.com/aws/aws-step-functions-data-science-sdk-python)、[資料科學 SDK 文件](https://aws-step-functions-data-science-sdk.readthedocs.io/)，以及 GitHub [上的 Jupyter 筆記本](https://docs.aws.amazon.com/sagemaker/latest/dg/howitworks-nbexamples.html)和 SageMaker AI 範例。 [SageMaker GitHub](https://github.com/awslabs/amazon-sagemaker-examples/tree/master/step-functions-data-science-sdk)

## 執行和偵錯您的工作流程
<a name="development-run-debug"></a>

您可以透過多種方式啟動工作流程，包括從主控台、從 Amazon EventBridge 和 EventBridge Scheduler 從另一個 Step Functions 狀態機器進行 API 呼叫 （例如，從 Lambda 函數）。執行中的工作流程可以連接到第三方服務、使用 AWS SDKs並在執行時操作資料。有多種工具可以執行和偵錯執行步驟，以及流經狀態機器的資料。下列各節提供執行和偵錯工作流程的其他資源。

若要進一步了解啟動狀態機器執行的方法，請參閱 [在 Step Functions 中啟動狀態機器執行](statemachine-starting.md)。

**選擇要執行工作流程的端點**  
為了減少延遲並將資料存放在符合您需求的位置，Step Functions 會在不同的 AWS 區域中提供端點。Step Functions 中的每個端點都是完全獨立的。狀態機器或活動只存在於其建立所在的區域內。您在某個區域中建立的任何狀態機器和活動都不會與在另一個區域中建立的資料或屬性共用任何資料或屬性。例如，您可以在`STATES-Flows-1`兩個不同的區域中註冊名為 的狀態機器。一個區域中`STATES-Flows-1`的狀態機器不會與另一個區域中`STATES-Flow-1`的狀態機器共用資料或屬性。如需 Step Functions 端點的清單，請參閱 中的[AWS Step Functions 區域和端點](https://docs.aws.amazon.com/general/latest/gr/step-functions.html)*AWS 一般參考*。

**使用 VS 程式碼進行開發**  
透過 AWS 工具組，您可以從 VS 程式碼內使用 Workflow Studio，在狀態機器中視覺化、建置甚至測試個別狀態。您也可以使用 SAM 和 CloudFormation 定義替換。您可以提供狀態輸入並設定變數、開始測試，然後即可查看資料的轉換方式。在狀態定義索引標籤中，您可以調整工作流程並重新測試。完成後，您可以套用變更來更新狀態機器。如需詳細資訊，請參閱《》中的[使用步驟函數](https://docs.aws.amazon.com/toolkit-for-vscode/latest/userguide/bulding-stepfunctions.html)和[使用工作流程 Studio](https://docs.aws.amazon.com/toolkit-for-vscode/latest/userguide/stepfunctions-workflowstudio.html) AWS Toolkit for Visual Studio Code。

## 部署您的工作流程
<a name="development-deploy"></a>

定義和偵錯工作流程之後，您可能想要使用基礎設施做為程式碼架構進行部署。您可以選擇使用各種 IaC 選項來部署狀態機器，包括： AWS Serverless Application Model CloudFormation、 AWS CDK和 Terraform。

**AWS Serverless Application Model**  
您可以使用 AWS Serverless Application Model 搭配 Step Functions 來建置工作流程和部署所需的基礎設施，包括 Lambda 函數、APIs和事件，以建立無伺服器應用程式。您也可以使用 AWS SAM CLI 搭配 AWS Toolkit for Visual Studio Code 做為整合體驗的一部分。  
如需詳細資訊，請參閱[使用 AWS SAM 建置 Step Functions 工作流程](concepts-sam-sfn.md)。

**CloudFormation**  
您可以直接在 CloudFormation 範本中使用狀態機器定義。  
如需詳細資訊，請參閱[使用 在 Step Functions 中 CloudFormation 建立工作流程](tutorial-lambda-state-machine-cloudformation.md)。

**AWS CDK**  
您可以使用 建置標準和快速狀態機器 AWS CDK。  
若要建置標準工作流程，請參閱 [使用 CDK 建立標準工作流程](tutorial-lambda-state-machine-cdk.md)。  
若要建置 Express 工作流程，請參閱 [使用 CDK 建立 Express 工作流程](tutorial-step-functions-rest-api-integration-cdk.md)。

**Terraform**  
HashiCorp 的 [Terraform](https://www.terraform.io/intro/) 是使用基礎設施即程式碼 (IaC) 建置應用程式的架構。使用 Terraform，您可以建立狀態機器並使用功能，例如預覽基礎設施部署和建立可重複使用的範本。Terraform 範本會將程式碼分解為較小的區塊，協助您維護和重複使用程式碼。  
如需詳細資訊，請參閱[使用 Terraform 在 Step Functions 中部署狀態機器](terraform-sfn.md)。