

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

# 使用 協調 AWS SAM 資源 AWS Step Functions
<a name="serverless-step-functions-in-sam"></a>

您可以使用 [AWS Step Functions](https://docs.aws.amazon.com/step-functions/latest/dg/) 來協調 AWS Lambda 函數和其他 AWS 資源，以形成複雜且強大的工作流程。Step Functions 可告知應用程式何時和在何種條件下使用您的 AWS 資源，例如 AWS Lambda 函數。這可簡化形成複雜且強大工作流程的程序。使用 [AWS::Serverless::StateMachine](sam-resource-statemachine.md)，您可以定義工作流程中的個別步驟、將每個步驟中的資源建立關聯，然後將這些步驟排序在一起。您也可以在需要轉換和條件的地方新增轉換和條件。這可簡化建立複雜且強大工作流程的程序。

**注意**  
若要管理包含 Step Functions 狀態機器的 AWS SAM 範本，您必須使用 0.52.0 版或更新版本 AWS SAM CLI。若要檢查您擁有的版本，請執行命令 `sam --version`。

Step Functions 是以[任務](https://docs.aws.amazon.com/step-functions/latest/dg/amazon-states-language-task-state.html)和[狀態機器](https://docs.aws.amazon.com/step-functions/latest/dg/concepts-states.html)的概念為基礎。您可以使用 JSON 型 [Amazon States Language](https://docs.aws.amazon.com/step-functions/latest/dg/concepts-amazon-states-language.html) 定義狀態機器。[Step Functions 主控台](https://console.aws.amazon.com/states/home?region=us-east-1#/)會顯示狀態機器結構的圖形檢視，讓您可以以視覺化方式檢查狀態機器的邏輯並監控執行。

透過 Step Functions 支援 in AWS Serverless Application Model (AWS SAM)，您可以執行下列動作：
+ 直接在 AWS SAM 範本內或在個別檔案中定義狀態機器 
+ 透過 AWS SAM 政策範本、內嵌政策或受管政策建立狀態機器執行角色 
+ 使用 API Gateway 或 Amazon EventBridge 事件、依照 AWS SAM 範本中的排程，或直接呼叫 APIs 來觸發狀態機器執行
+ 針對常見的 Step Functions 開發模式使用可用的[AWS SAM 政策範本](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-policy-templates.html)。

## 範例
<a name="serverless-step-functions-in-sam-example"></a>

下列 AWS SAM 範本檔案的範例程式碼片段會在定義檔案中定義 Step Functions 狀態機器。請注意，`my_state_machine.asl.json`檔案必須以 [Amazon States Language](https://docs.aws.amazon.com/step-functions/latest/dg/concepts-amazon-states-language.html) 撰寫。

```
AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Description: Sample SAM template with Step Functions State Machine

Resources:
  MyStateMachine:
    Type: AWS::Serverless::StateMachine
    Properties:
      DefinitionUri: statemachine/my_state_machine.asl.json
      ...
```

若要下載包含 Step Functions 狀態機器的範例 AWS SAM 應用程式，請參閱《 *AWS Step Functions 開發人員指南*》中的[使用 建立 Step Functions 狀態機器 AWS SAM](https://docs.aws.amazon.com/step-functions/latest/dg/tutorial-state-machine-using-sam.html)。

## 其他資訊
<a name="serverless-step-functions-in-sam-more-information"></a>

若要進一步了解 Step Functions 並搭配 使用 AWS SAM，請參閱以下內容：
+ [AWS Step Functions 的運作方式](https://docs.aws.amazon.com/step-functions/latest/dg/how-step-functions-works.html)
+ [AWS Step Functions 而且 AWS Serverless Application Model](https://docs.aws.amazon.com/step-functions/latest/dg/concepts-sam-sfn.html)
+ [教學課程：使用 建立 Step Functions 狀態機器 AWS SAM](https://docs.aws.amazon.com/step-functions/latest/dg/tutorial-state-machine-using-sam.html)
+ [AWS SAM 規格：AWS::Serverless::StateMachine](sam-resource-statemachine.md)