

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

# 使用一次全部流量轉移
<a name="deployment-guardrails-blue-green-all-at-once"></a>

透過一次全部流量轉移，您可以使用藍/綠部署的安全防護機制快速推出端點更新。您可以使用此流量轉移選項，將更新持續時間降至最低，同時仍可利用藍/綠部署的可用性保證。製作中期間功能可協助您在終止舊執行個體之前監控新執行個體的效能和功能，確保新機群可完全運作。

下圖顯示了一次全部流量轉移如何管理新舊機群。

![\[從舊機群成功將 100％ 流量轉移到新機群。\]](http://docs.aws.amazon.com/zh_tw/sagemaker/latest/dg/images/deployment-guardrails-blue-green-all-at-once.png)


使用一次全部流量轉移時，SageMaker AI 會將 100% 的流量路由到新機群 (綠色機群)。一旦綠色機群開始接收流量，製作中期間開始。製作中期間是指預先指定的 Amazon CloudWatch 警示監控綠色機群效能的設定時間長度。如果在製作中期間沒有觸發警示，SageMaker AI 會終止舊機群 (藍色機群)。如果在製作中期間觸發任何警示，則會啟動自動還原，並且將 100％ 的流量轉移回藍色機群。

## 先決條件
<a name="deployment-guardrails-blue-green-all-at-once-prereqs"></a>

在設定具有一次全部流量轉移的部署之前，您必須先建立 Amazon CloudWatch 警示以監看端點中的指標。如果有任何警報在製作中期間觸發，那麼流量就會回復到您的藍色機群。要瞭解如何在終端節點上設置 CloudWatch 警報，請參閱先決條件頁面[自動回復組態與監控](deployment-guardrails-configuration.md)。如需進一步了解 CloudWatch 警示，請參閱 *Amazon CloudWatch 使用者指南*中的[使用 Amazon CloudWatch 警示](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/AlarmThatSendsEmail.html)。

## 設定一次全部流量轉移
<a name="deployment-guardrails-blue-green-all-at-once-configure"></a>

一旦準備好進行部署，並為您的端點設定 CloudWatch 警示後，您可以使用中的 SageMaker AI [UpdateEndpoint](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_UpdateEndpoint.html) API 或 AWS Command Line Interface 中的 [update-endpoint](https://docs.aws.amazon.com/cli/latest/reference/sagemaker/update-endpoint.html) 命令來啟動部署。

**Topics**
+ [如何更新端點 (API)](#deployment-guardrails-blue-green-all-at-once-configure-api-update)
+ [如何使用現有的藍色/綠色更新政策 (API) 來更新端點](#deployment-guardrails-blue-green-all-at-once-configure-api-existing)
+ [如何更新端點 (CLI)](#deployment-guardrails-blue-green-all-at-once-configure-cli-update)

### 如何更新端點 (API)
<a name="deployment-guardrails-blue-green-all-at-once-configure-api-update"></a>

下列範例示範如何使用 Amazon SageMaker API 中的 [UpdateEndpoint](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_UpdateEndpoint.html) 透過一次全部流量轉移更新所有端點。

```
import boto3
client = boto3.client("sagemaker")

response = client.update_endpoint(
    EndpointName="<your-endpoint-name>",
    EndpointConfigName="<your-config-name>",
    DeploymentConfig={
        "BlueGreenUpdatePolicy": {
            "TrafficRoutingConfiguration": {
                "Type": "ALL_AT_ONCE"
            },
            "TerminationWaitInSeconds": 600,
            "MaximumExecutionTimeoutInSeconds": 1800
        },
        "AutoRollbackConfiguration": {
            "Alarms": [
                {
                    "AlarmName": "<your-cw-alarm>"
                },
            ]
        }
    }
)
```

若要設定一次全部流量轉移選項，請執行下列動作：
+ 針對 `EndpointName`，請使用您要更新的現有端點名稱。
+ 在 `EndpointConfigName` 中，使用您要使用的端點組態名稱。
+ 在 `DeploymentConfig` 和 `BlueGreenUpdatePolicy` 下，在 `TrafficRoutingConfiguration` 中，將 `Type` 參數設定為 `ALL_AT_ONCE`。這會指定使用一次全部流量轉移模式的部署。
+ 對於 `TerminationWaitInSeconds`，請使用 `600`。參數會告知 SageMaker AI 在綠色機群完全作用後等待指定的時間量 (以秒為單位)，然後才終止藍色機群中的執行個體。在此範例中，SageMaker AI 會在最後製作中期間後等待 10 分鐘，然後才終止藍色機群。
+ 對於 `MaximumExecutionTimeoutInSeconds`，請使用 `1800`。此參數設定部署在逾時前可以執行的時間上限。在上述範例中，您的部署時間上限為 30 分鐘。
+ 在 `AutoRollbackConfiguration` 中，`Alarms` 欄位，您可以依據名稱新增您的 CloudWatch 警示。為您要使用的每個警示建立一個 `AlarmName: <your-cw-alarm>` 項目。

### 如何使用現有的藍色/綠色更新政策 (API) 來更新端點
<a name="deployment-guardrails-blue-green-all-at-once-configure-api-existing"></a>

當您使用 [CreateEndpoint](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateEndpoint.html) API 建立端點時，您可以選擇性地指定部署組態，以便在未來的端點更新時重複使用。您可以使用與先前 UpdateEndpoint API 範例相同的 `DeploymentConfig` 選項。CreateEndpoint API 行為沒有變更。指定部署組態不會自動在端點上執行藍/綠更新。

使用 [UpdateEndpoint](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_UpdateEndpoint.html) API 更新您的端點時，會出現使用先前部署組態的選項。更新端點時，您可以使用 `RetainDeploymentConfig` 選項來保留您在建立端點時指定的部署組態。

呼叫 [UpdateEndpoint](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_UpdateEndpoint.html) API 時，請設定 `RetainDeploymentConfig` 為 `True` 以保留原始端點組態中的 `DeploymentConfig` 選項。

```
response = client.update_endpoint(
    EndpointName="<your-endpoint-name>",
    EndpointConfigName="<your-config-name>",
    RetainDeploymentConfig=True
)
```

### 如何更新端點 (CLI)
<a name="deployment-guardrails-blue-green-all-at-once-configure-cli-update"></a>

如果您使用的是 AWS CLI，下列範例會示範如何使用 [update-endpoint](https://docs.aws.amazon.com/cli/latest/reference/sagemaker/update-endpoint.html) 命令來啟動藍/綠全部部署。

```
update-endpoint
--endpoint-name <your-endpoint-name> 
--endpoint-config-name <your-config-name> 
--deployment-config '"BlueGreenUpdatePolicy": {"TrafficRoutingConfiguration": {"Type": "ALL_AT_ONCE"},
    "TerminationWaitInSeconds": 600, "MaximumExecutionTimeoutInSeconds": 1800},
    "AutoRollbackConfiguration": {"Alarms": [{"AlarmName": "<your-alarm>"}]}'
```

若要設定一次全部流量轉移選項，請執行下列動作：
+ 在 `endpoint-name` 中，使用您想更新的端點名稱。
+ 在 `endpoint-config-name` 中，使用您要使用的端點組態名稱。
+ 在 `deployment-config` 中，使用 [BlueGreenUpdatePolicy](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_BlueGreenUpdatePolicy.html) JSON 物件。

**注意**  
如果您希望將 JSON 對象保存在文件中，請參閱[正在生成AWS CLI骨架和輸入參數](https://docs.aws.amazon.com/cli/latest/userguide/cli-usage-skeleton.html)在*AWS CLI用戶指南*。