

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

# AWS::Serverless::StateMachine
<a name="sam-resource-statemachine"></a>

创建 AWS Step Functions 状态机，您可以使用它来编排 AWS Lambda 函数和其他 AWS 资源，以形成复杂而强大的工作流程。

有关 Step Functions 的更多信息，请参阅[《AWS Step Functions 开发人员指南》](https://docs.aws.amazon.com/step-functions/latest/dg/welcome.html)。

**注意**  
部署到时 AWS CloudFormation， AWS SAM 会将您的 AWS SAM 资源转换为 CloudFormation 资源。有关更多信息，请参阅 [生成的 CloudFormation 资源用于 AWS SAM](sam-specification-generated-resources.md)。

## 语法
<a name="sam-resource-statemachine-syntax"></a>

要在 AWS Serverless Application Model (AWS SAM) 模板中声明此实体，请使用以下语法。

### YAML
<a name="sam-resource-statemachine-syntax.yaml"></a>

```
Type: AWS::Serverless::StateMachine
Properties:
  AutoPublishAlias: String
  UseAliasAsEventTarget: Boolean
  [Definition](#sam-statemachine-definition): Map
  [DefinitionSubstitutions](#sam-statemachine-definitionsubstitutions): Map
  [DefinitionUri](#sam-statemachine-definitionuri): String | [S3Location](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-stepfunctions-statemachine.html#cfn-stepfunctions-statemachine-definitions3location)
  DeploymentPreference: [DeploymentPreference](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-stepfunctions-statemachinealias-deploymentpreference.html)
  [Events](#sam-statemachine-events): EventSource
  [Logging](#sam-statemachine-logging): [LoggingConfiguration](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-stepfunctions-statemachine.html#cfn-stepfunctions-statemachine-loggingconfiguration)
  [Name](#sam-statemachine-name): String
  [PermissionsBoundary](#sam-statemachine-permissionsboundary): String
  [Policies](#sam-statemachine-policies): String | List | Map
  PropagateTags: Boolean
  [RolePath](#sam-statemachine-rolepath): String
  [Role](#sam-statemachine-role): String
  [Tags](#sam-statemachine-tags): Map
  [Tracing](#sam-statemachine-tracing): [TracingConfiguration](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-stepfunctions-statemachine.html#cfn-stepfunctions-statemachine-tracingconfiguration)
  [Type](#sam-statemachine-type): String
```

## Properties
<a name="sam-resource-statemachine-properties"></a>

 `AutoPublishAlias`   <a name="sam-statemachine-autopublishalias"></a>
状态机别名的名称。要详细了解如何使用 Step Functions 状态机别名，请参阅*《AWS Step Functions 开发人员指南》*中的[使用版本与别名功能管理持续部署](https://docs.aws.amazon.com/step-functions/latest/dg/concepts-cd-aliasing-versioning.html)。  
使用 `DeploymentPreference` 为别名配置部署首选项。如果您未指定`DeploymentPreference`，则 AWS SAM 会将流量配置为一次性切换到较新的状态机版本。  
AWS SAM 默认情况下，将版本`UpdateReplacePolicy``Retain`的`DeletionPolicy`和设置为。以前的版本不会被自动删除。  
*类型*：字符串  
*必需*：否  
*CloudFormation 兼容性*：此属性直接传递给`AWS::StepFunctions::StateMachineAlias`资源的` [ Name](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-stepfunctions-statemachinealias.html#cfn-stepfunctions-statemachinealias-name)`属性。

 `UseAliasAsEventTarget`   <a name="sam-statemachine-usealiasaseventtarget"></a>
指示是否将使用 `AutoPublishAlias` 属性创建的别名传递给使用[事件](#sam-statemachine-events)定义的事件源目标。  
指定 `True` 使用别名作为事件的目标。  
*类型*：布尔值  
*必需*：否  
*默认值*：`False`  
*CloudFormation 兼容性*：此属性是独有的 AWS SAM ，没有 CloudFormation 等效属性。

 `Definition`   <a name="sam-statemachine-definition"></a>
状态机定义是一个对象，其中对象的格式与 AWS SAM 模板文件的格式相匹配，例如 JSON 或 YAML。状态机定义遵循 [Amazon States Language](https://docs.aws.amazon.com/step-functions/latest/dg/concepts-amazon-states-language.html)。  
有关内联状态机定义的示例，请参见 [示例](#sam-resource-statemachine--examples)。  
您必须提供 `Definition` 或 `DefinitionUri`。  
*类型*：映射  
*必需*：条件  
*CloudFormation 兼容性*：此属性是独有的 AWS SAM ，没有 CloudFormation 等效属性。

 `DefinitionSubstitutions`   <a name="sam-statemachine-definitionsubstitutions"></a>
在状态机定义中指定占位符变量的映射的 string-to-string地图。这使您能够将运行时获得的值（例如，从内置函数）注入到状态机定义中。  
*类型*：映射  
*必需*：否  
*CloudFormation 兼容性*：此属性类似于`AWS::StepFunctions::StateMachine`资源的`[DefinitionSubstitutions](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-stepfunctions-statemachine.html#cfn-stepfunctions-statemachine-definitionsubstitutions)`属性。如果在内联状态机定义中指定了任何内部函数，则向该属性 AWS SAM 添加条目以将其注入状态机定义中。

 `DefinitionUri`   <a name="sam-statemachine-definitionuri"></a>
Amazon Simple Storage Service (Amazon S3) URI 或以 [Amazon States Language](https://docs.aws.amazon.com/step-functions/latest/dg/concepts-amazon-states-language.html) 编写的状态机定义的本地文件路径。  
如果您提供本地文件路径，则模板必须通过包含 `sam deploy` 或 `sam package` 命令的工作流程才能使定义正确转换。为此，必须使用 AWS SAM CLI 版本 0.52.0 或更高版本。  
您必须提供 `Definition` 或 `DefinitionUri`。  
*类型*：字符串 \$1 [S3Location](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-stepfunctions-statemachine.html#cfn-stepfunctions-statemachine-definitions3location)  
*必需*：条件  
*CloudFormation 兼容性*：此属性直接传递给`AWS::StepFunctions::StateMachine`资源的`[DefinitionS3Location](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-stepfunctions-statemachine.html#cfn-stepfunctions-statemachine-definitions3location)`属性。

 `DeploymentPreference`   <a name="sam-statemachine-deploymentpreference"></a>
启用和配置逐步状态机部署的设置。要详细了解 Step Functions 逐步部署，请参阅*《AWS Step Functions 开发人员指南》*中的[使用版本与别名功能管理持续部署](https://docs.aws.amazon.com/step-functions/latest/dg/concepts-cd-aliasing-versioning.html)。  
在配置此属性之前指定 `AutoPublishAlias`。您的 `DeploymentPreference` 设置将应用于通过 `AutoPublishAlias` 指定的别名。  
指定后`DeploymentPreference`， AWS SAM 会自动生成`StateMachineVersionArn`子属性值。  
*类型*：[DeploymentPreference](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-stepfunctions-statemachinealias-deploymentpreference.html)  
*必需*：否  
*CloudFormation 兼容性*： AWS SAM 生成`StateMachineVersionArn`属性值并将其附加到资源的`[DeploymentPreference](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-stepfunctions-statemachinealias.html#cfn-stepfunctions-statemachinealias-deploymentpreference)`属性，`DeploymentPreference`然后传递`DeploymentPreference`给`AWS::StepFunctions::StateMachineAlias`资源的属性。

 `Events`   <a name="sam-statemachine-events"></a>
指定触发此状态机的事件。事件由一个类型和一组依赖于该类型的属性组成。  
*类型*：[EventSource](sam-property-statemachine-statemachineeventsource.md)  
*必需*：否  
*CloudFormation 兼容性*：此属性是独有的 AWS SAM ，没有 CloudFormation 等效属性。

 `Logging`   <a name="sam-statemachine-logging"></a>
定义记录哪些执行历史事件以及它们的记录位置。  
*类型*：[LoggingConfiguration](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-stepfunctions-statemachine.html#cfn-stepfunctions-statemachine-loggingconfiguration)  
*必需*：否  
*CloudFormation 兼容性*：此属性直接传递给`AWS::StepFunctions::StateMachine`资源的`[LoggingConfiguration](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-stepfunctions-statemachine.html#cfn-stepfunctions-statemachine-loggingconfiguration)`属性。

 `Name`   <a name="sam-statemachine-name"></a>
状态机的名称。  
*类型*：字符串  
*必需*：否  
*CloudFormation 兼容性*：此属性直接传递给`AWS::StepFunctions::StateMachine`资源的`[StateMachineName](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-stepfunctions-statemachine.html#cfn-stepfunctions-statemachine-statemachinename)`属性。

 `PermissionsBoundary`   <a name="sam-statemachine-permissionsboundary"></a>
此状态机执行角色使用的权限边界的 ARN。此属性仅在为您生成角色时有效。  
*类型*：字符串  
*必需*：否  
*CloudFormation 兼容性*：此属性直接传递给`AWS::IAM::Role`资源的`[PermissionsBoundary](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-iam-role.html#cfn-iam-role-permissionsboundary)`属性。

 `Policies`   <a name="sam-statemachine-policies"></a>
此状态机的权限策略。策略将附加到状态机的默认 AWS Identity and Access Management (IAM) 执行角色中。  
此属性接受单个值或值列表。允许的值包括：  
+ [AWS SAM策略模板](serverless-policy-templates.md).
+ [AWS 托管策略](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_managed-vs-inline.html#aws-managed-policies)或[客户管理型策略](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_managed-vs-inline.html#customer-managed-policies)的 ARN。
+ 以下[列表](https://github.com/aws/serverless-application-model/blob/develop/samtranslator/internal/data/aws_managed_policies.json)中 AWS 托管策略的名称。
+ 在 YAML 中格式化为映射的[内联 IAM policy](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_managed-vs-inline.html#inline-policies)。
如果指定 `Role` 属性，则将忽略该属性。
*类型*：字符串 \$1 列表 \$1 映射  
*必需*：否  
*CloudFormation 兼容性*：此属性是独有的 AWS SAM ，没有 CloudFormation 等效属性。

`PropagateTags`  <a name="sam-statemachine-propagatetags"></a>
指明是否将 `Tags` 属性中的标签传递给 [AWS::Serverless::StateMachine](sam-specification-generated-resources-statemachine.md) 生成的资源。指定 `True` 以在生成的资源中传播标签。  
*类型*：布尔值  
*必需*：否  
*默认值*：`False`  
*CloudFormation 兼容性*：此属性是独有的 AWS SAM ，没有 CloudFormation 等效属性。

 `Role`   <a name="sam-statemachine-role"></a>
用作此状态机执行角色的 IAM 角色的 ARN。  
*类型*：字符串  
*必需*：条件  
*CloudFormation 兼容性*：此属性直接传递给`AWS::StepFunctions::StateMachine`资源的`[ RoleArn](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-stepfunctions-statemachine.html#cfn-stepfunctions-statemachine-rolearn)`属性。

 `RolePath`   <a name="sam-statemachine-rolepath"></a>
状态机的 IAM 执行角色的路径。  
生成角色时请使用此属性。当使用 `Role` 属性指定角色时，请勿使用。  
*类型*：字符串  
*必需*：条件  
*CloudFormation 兼容性*：此属性直接传递给`AWS::IAM::Role`资源的`[Path](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-iam-role.html#cfn-iam-role-path)`属性。

 `Tags`   <a name="sam-statemachine-tags"></a>
指定添加到状态机的标签和相应的执行角色的 string-to-string地图。有关标签的有效键和值的信息，请参阅 [https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-stepfunctions-statemachine.html](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-stepfunctions-statemachine.html) 资源的[标签](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-stepfunctions-statemachine.html#cfn-stepfunctions-statemachine-tags)属性。  
*类型*：映射  
*必需*：否  
*CloudFormation 兼容性*：此属性类似于`AWS::StepFunctions::StateMachine`资源的`[Tags](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-stepfunctions-statemachine.html#cfn-stepfunctions-statemachine-tags)`属性。 AWS SAM 自动为该资源以及为其生成的默认角色添加`stateMachine:createdBy:SAM`标签。

 `Tracing`   <a name="sam-statemachine-tracing"></a>
选择是否 AWS X-Ray 为状态机启用。有关使用 X-Ray 和 Step Functions 的更多信息，请参阅*《AWS Step Functions 开发人员指南》*中的 [AWS X-Ray 与 Step Functions](https://docs.aws.amazon.com/step-functions/latest/dg/concepts-xray-tracing.html)。  
*类型*：[TracingConfiguration](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-stepfunctions-statemachine.html#cfn-stepfunctions-statemachine-tracingconfiguration)  
*必需*：否  
*CloudFormation 兼容性*：此属性直接传递给`AWS::StepFunctions::StateMachine`资源的`[TracingConfiguration](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-stepfunctions-statemachine.html#cfn-stepfunctions-statemachine-tracingconfiguration)`属性。

 `Type`   <a name="sam-statemachine-type"></a>
状态机的类型。  
*有效值*：`STANDARD` 或 `EXPRESS`  
*类型*：字符串  
*必需*：否  
*默认值*：`STANDARD`  
*CloudFormation 兼容性*：此属性直接传递给`AWS::StepFunctions::StateMachine`资源的`[StateMachineType](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-stepfunctions-statemachine.html#cfn-stepfunctions-statemachine-statemachinetype)`属性。

## 返回值
<a name="sam-resource-statemachine-return-values"></a>

### Ref
<a name="sam-resource-statemachine-return-values-ref"></a>

在将此资源的逻辑 ID 提供给 Ref 内置函数时，Ref 会返回底层 `AWS::StepFunctions::StateMachine` 资源的 Amazon 资源名称（ARN）。

有关使用 `Ref` 函数的更多信息，请参阅*《AWS CloudFormation 用户指南》*中的 [https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-ref.html](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-ref.html)。

### Fn:: GetAtt
<a name="sam-resource-statemachine-return-values-fn--getatt"></a>

`Fn::GetAtt` 返回一个此类型指定属性的值。以下为可用属性和示例返回值。

有关使用 `Fn::GetAtt` 的更多信息，请参阅*《AWS CloudFormation 用户指南》*中的 [https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-getatt.html](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-getatt.html)。

`Name`  <a name="Name-fn::getatt"></a>
返回状态机的名称，例如 `HelloWorld-StateMachine`。

## 示例
<a name="sam-resource-statemachine--examples"></a>

### 状态机定义文件
<a name="sam-resource-statemachine--examples--state-machine-definition-file"></a>

以下是内联状态机定义的示例，该定义允许 Lambda 函数调用状态机。请注意，此示例要求 `Role` 属性配置适当的策略以允许调用。`my_state_machine.asl.json` 文件必须以 [Amazon States Language](https://docs.aws.amazon.com/step-functions/latest/dg/concepts-amazon-states-language.html) 编写。

在此示例中，这些`DefinitionSubstitution`条目允许状态机包含在 AWS SAM 模板文件中声明的资源。

#### YAML
<a name="sam-resource-statemachine--examples--state-machine-definition-file--yaml"></a>

```
MySampleStateMachine:
  Type: AWS::Serverless::StateMachine
  Properties:
    DefinitionUri: statemachine/my_state_machine.asl.json
    Role: arn:aws:iam::123456123456:role/service-role/my-sample-role
    Tracing:
      Enabled: true
    DefinitionSubstitutions:
      MyFunctionArn: !GetAtt MyFunction.Arn
      MyDDBTable: !Ref TransactionTable
```

### 内联状态机定义
<a name="sam-resource-statemachine--examples--inline-state-machine-definition"></a>

以下是内联状态机定义的示例。

在此示例中， AWS SAM 模板文件是用 YAML 编写的，因此状态机定义也在 YAML 中。要在 JSON 中声明内联状态机定义，请使用 JSON 编写 AWS SAM 模板文件。

#### YAML
<a name="sam-resource-statemachine--examples--inline-state-machine-definition--yaml"></a>

```
MySampleStateMachine:
  Type: AWS::Serverless::StateMachine
  Properties:
    Definition:
      StartAt: MyLambdaState
      States:
        MyLambdaState:
          Type: Task
          Resource: arn:aws:lambda:us-east-1:123456123456:function:my-sample-lambda-app
          End: true
    Role: arn:aws:iam::123456123456:role/service-role/my-sample-role
    Tracing:
      Enabled: true
```