

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

# 示例：Step Functions 中的版本与别名功能部署
<a name="example-alias-version-deployment"></a>

下面的金丝雀部署技术示例展示了如何使用 AWS Command Line Interface部署新的状态机版本。在此示例中，您创建的别名将 20% 的执行流量路由到新版本。然后将剩余的 80% 路由到早期版本。要部署新的状态机[版本](concepts-state-machine-version.md)并使用[别名](concepts-state-machine-alias.md)转移执行流量，请完成以下步骤：

1. 

**从当前状态机修订版中发布一个版本。**  
使用 AWS CLI 中的 **publish-state-machine-version** 命令，从名为 `myStateMachine:` 的状态机的当前版本中发布一个版本：

   ```
   aws stepfunctions publish-state-machine-version --state-machine-arn arn:aws:states:region:account-id:stateMachine:myStateMachine
   ```

   响应会返回发布版本的 `stateMachineVersionArn`。例如 `arn:aws:states:region:account-id:stateMachine:myStateMachine:1`。

1. 

**创建指向状态机版本的别名。**  
使用 **create-state-machine-alias** 命令创建指向 `myStateMachine` 版本 1 的别名，名为 `PROD`：

   ```
   aws stepfunctions create-state-machine-alias --name PROD --routing-configuration "[{\"stateMachineVersionArn\":\"arn:aws:states:region:account-id:stateMachine:myStateMachine:1\",\"weight\":100}]"
   ```

1. 

**验证由别名启动的执行是否使用正确的已发布版本。**  
通过在 **start-execution** 命令中提供别名 **PROD** 的 ARN 来启动 `myStateMachine` 的新执行：

   ```
   aws stepfunctions start-execution 
     --state-machine-arn arn:aws:states:region:account-id:stateMachineAlias:myStateMachine:PROD
     --input "{}"
   ```

   如果您在[StartExecution](https://docs.aws.amazon.com/step-functions/latest/apireference/API_StartExecution.html)请求中提供了状态机 ARN，则它会使用最新的[revision](concepts-cd-aliasing-versioning.md#statemachinerev)状态机而不是您的别名中指定的版本来开始执行。

1. 

**更新状态机定义并发布新版本。**  
更新 `myStateMachine` 并发布其新版本。为此，请使用 **update-state-machine** 命令的可选 `publish` 参数：

   ```
   aws stepfunctions update-state-machine
       --state-machine-arn arn:aws:states:region:account-id:stateMachine:myStateMachine
       --definition $UPDATED_STATE_MACHINE_DEFINITION
       --publish
   ```

   响应会返回新版本的 `stateMachineVersionArn`。例如 `arn:aws:states:region:account-id:stateMachine:myStateMachine:2`。

1. 

**更新别名以指向两个版本并设置别名的[路由配置](concepts-state-machine-alias.md#alias-routing-config)。**  
使用 **update-state-machine-alias** 命令更新别名 `PROD` 的路由配置。配置别名，使 80% 的执行流量流向版本 1，其余 20% 流向版本 2：

   ```
   aws stepfunctions update-state-machine-alias --state-machine-alias-arn arn:aws:states:region:account-id:stateMachineAlias:myStateMachine:PROD --routing-configuration "[{\"stateMachineVersionArn\":\"arn:aws:states:region:account-id:stateMachine:myStateMachine:1\",\"weight\":80}, {\"stateMachineVersionArn\":\"arn:aws:states:region:account-id:stateMachine:myStateMachine:2\",\"weight\":20}]"
   ```

1. 

**将版本 1 替换为版本 2。**  
在验证新的状态机版本能够正常运行后，您可以部署新的状态机版本。为此，请再次更新别名，将 100% 的执行流量分配给新版本。

   使用 **update-state-machine-alias** 命令设置别名 `PROD` 的路由配置，为版本 2 分配 100% 的流量：

   ```
   aws stepfunctions update-state-machine-alias --state-machine-alias-arn arn:aws:states:region:account-id:stateMachineAlias:myStateMachine:PROD --routing-configuration "[{\"stateMachineVersionArn\":\"arn:aws:states:region:account-id:stateMachine:myStateMachine:2\",\"weight\":100}]"
   ```

**提示**  
要回滚版本 2 的部署，请编辑别名的路由配置，将 100% 的流量转移到新部署的版本。  

```
aws stepfunctions update-state-machine-alias 
  --state-machine-alias-arn arn:aws:states:region:account-id:stateMachineAlias:myStateMachine:PROD 
  --routing-configuration "[{\"stateMachineVersionArn\":\"arn:aws:states:region:account-id:stateMachine:myStateMachine:1\",\"weight\":100}]"
```

您可以使用版本与别名功能来执行其他类型的部署。例如，您可以对状态机的新版本进行*滚动部署*。为此，请在指向新版本的别名的路由配置中，逐渐增加加权百分比。

您也可以使用版本与别名功能来执行*蓝绿部署*。为此，请创建一个名为 `green` 的别名，且该别名运行状态机的当前版本 1。然后，创建另一个名为 `blue` 的别名，运行新版本，例如 `2`。要测试新版本，请向 `blue` 别名发送执行流量。当您确信自己的新版本可以正常运行时，更新 `green` 别名以指向您的新版本。