

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 使用编排资源 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 SAMCLI要检查您拥有的版本，请执行命令 `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#/)显示状态机结构的图形视图，因此您能够直观检查状态机逻辑和监控执行。

借助 AWS Serverless Application Model (AWS SAM) 中的 Step Functions 支持，你可以执行以下操作：
+ 定义状态机，可以直接在 AWS SAM 模板中定义，也可以在单独的文件中定义 
+ 通过 AWS SAM 策略模板、内联策略或托管策略创建状态机执行角色 
+ 使用 API Gateway 或 Amazon EventBridge 事件、在 AWS SAM 模板内按计划或 APIs 直接调用触发状态机执行
+ 使用可用的 [AWS SAM 策略模板](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-policy-templates.html)创建常见的 Step Functions 开发模式。

## 示例
<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 开发者指南》 AWS SAM*中的 “[使用创建 Step Functions 状态机](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)