

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

# 使用 部署編譯的模型 AWS CLI
<a name="neo-deployment-hosting-services-cli"></a>

如果模型是使用 或 Amazon SageMaker AI 主控台編譯的 適用於 Python (Boto3) 的 AWS SDK AWS CLI，您必須滿足[先決條件](https://docs.aws.amazon.com//sagemaker/latest/dg/neo-deployment-hosting-services-prerequisites)區段。請遵循下列步驟，使用 [AWS CLI](https://docs.aws.amazon.com/cli/latest/reference/) 建立和部署 SageMaker Neo 編譯的模型。

**Topics**
+ [部署模型](#neo-deploy-cli)

## 部署模型
<a name="neo-deploy-cli"></a>

在您滿足[先決條件](https://docs.aws.amazon.com//sagemaker/latest/dg/neo-deployment-hosting-services-prerequisites)之後，請使用 `create-model`、 `create-enpoint-config`和 `create-endpoint` AWS CLI 命令。下列步驟說明，如何使用這些命令部署使用 Neo 編譯的模型：



### 建立模型
<a name="neo-deployment-hosting-services-cli-create-model"></a>

從[Neo 推論容器映像](https://docs.aws.amazon.com/sagemaker/latest/dg/neo-deployment-hosting-services-container-images.html)中，選取推論映像 URI，然後使用 `create-model` API 建立 SageMaker AI 模型。您可用兩個步驟完成這項工作：

1. 建立 `create_model.json` 檔案。在檔案中，指定模型的名稱、映像 URI、Amazon S3 儲存貯體中 `model.tar.gz` 檔案的路徑，以及 SageMaker AI 執行角色：

   ```
   {
       "ModelName": "insert model name",
       "PrimaryContainer": {
           "Image": "insert the ECR Image URI",
           "ModelDataUrl": "insert S3 archive URL",
           "Environment": {"See details below"}
       },
       "ExecutionRoleArn": "ARN for AmazonSageMaker-ExecutionRole"
   }
   ```

   如果您使用 SageMaker AI 訓練模型，請指定下列環境變數：

   ```
   "Environment": {
       "SAGEMAKER_SUBMIT_DIRECTORY" : "[Full S3 path for *.tar.gz file containing the training script]"
   }
   ```

   如果您未使用 SageMaker AI 訓練模型，請指定下列環境變數：

------
#### [ MXNet and PyTorch ]

   ```
   "Environment": {
       "SAGEMAKER_PROGRAM": "inference.py",
       "SAGEMAKER_SUBMIT_DIRECTORY": "/opt/ml/model/code",
       "SAGEMAKER_CONTAINER_LOG_LEVEL": "20",
       "SAGEMAKER_REGION": "insert your region",
       "MMS_DEFAULT_RESPONSE_TIMEOUT": "500"
   }
   ```

------
#### [ TensorFlow ]

   ```
   "Environment": {
       "SAGEMAKER_PROGRAM": "inference.py",
       "SAGEMAKER_SUBMIT_DIRECTORY": "/opt/ml/model/code",
       "SAGEMAKER_CONTAINER_LOG_LEVEL": "20",
       "SAGEMAKER_REGION": "insert your region"
   }
   ```

------
**注意**  
`AmazonSageMakerFullAccess` 和 `AmazonS3ReadOnlyAccess` 政策必須連接到 `AmazonSageMaker-ExecutionRole` IAM 角色。

1. 執行以下命令：

   ```
   aws sagemaker create-model --cli-input-json file://create_model.json
   ```

   如需 `create-model` API 的完整語法，請參閱 [https://docs.aws.amazon.com/cli/latest/reference/sagemaker/create-model.html](https://docs.aws.amazon.com/cli/latest/reference/sagemaker/create-model.html)。

### 建立一個端點組態
<a name="neo-deployment-hosting-services-cli-create-endpoint-config"></a>

建立 SageMaker AI 模型之後，請使用 `create-endpoint-config` API 建立端點組態。若要這麼做，請使用端點組態規格建立 JSON 檔案。例如，您可使用下列程式碼範本並將其儲存為 `create_config.json`：

```
{
    "EndpointConfigName": "<provide your endpoint config name>",
    "ProductionVariants": [
        {
            "VariantName": "<provide your variant name>",
            "ModelName": "my-sagemaker-model",
            "InitialInstanceCount": 1,
            "InstanceType": "<provide your instance type here>",
            "InitialVariantWeight": 1.0
        }
    ]
}
```

現在執行下列 AWS CLI 命令來建立您的端點組態：

```
aws sagemaker create-endpoint-config --cli-input-json file://create_config.json
```

如需 `create-endpoint-config` API 的完整語法，請參閱 [https://docs.aws.amazon.com/cli/latest/reference/sagemaker/create-endpoint-config.html](https://docs.aws.amazon.com/cli/latest/reference/sagemaker/create-endpoint-config.html)。

### 建立端點
<a name="neo-deployment-hosting-services-cli-create-endpoint"></a>

建立端點組態後，請使用 `create-endpoint` API 建立端點：

```
aws sagemaker create-endpoint --endpoint-name '<provide your endpoint name>' --endpoint-config-name '<insert your endpoint config name>'
```

如需 `create-endpoint` API 的完整語法，請參閱 [https://docs.aws.amazon.com/cli/latest/reference/sagemaker/create-endpoint.html](https://docs.aws.amazon.com/cli/latest/reference/sagemaker/create-endpoint.html)。