

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

# 使用 AWS CLI 在資料庫叢集上設定 Neptune ML
<a name="machine-learning-cluster-setup"></a>

除了 CloudFormation 快速入門範本和 之外 AWS 管理主控台，您也可以使用 設定 Neptune ML AWS CLI。

## 為新的 Neptune ML 叢集建立資料庫叢集參數群組
<a name="machine-learning-enabling-create-param-group"></a>

下列 AWS CLI 命令會建立新的資料庫叢集參數群組，並將其設定為使用 Neptune ML：

**建立和設定 Neptune ML 的資料庫叢集參數群組**

1. 建立新的資料庫叢集參數群組：

   ```
   aws neptune create-db-cluster-parameter-group \
     --db-cluster-parameter-group-name (name of the new DB cluster parameter group) \
     --db-parameter-group-family neptune1
     --description "(description of your machine learning project)" \
     --region (AWS region, such as us-east-1)
   ```

1. 建立`neptune_ml_iam_role`資料庫叢集參數設定為 的 ARN，`SageMakerExcecutionIAMRole`以供資料庫叢集在呼叫 SageMaker AI 以建立任務並從託管 ML 模型取得預測時使用：

   ```
   aws neptune modify-db-cluster-parameter-group \
     --db-cluster-parameter-group-name (name of the new DB cluster parameter group) \
     --parameters "ParameterName=neptune_ml_iam_role, \
                   ParameterValue=ARN of the SageMakerExcecutionIAMRole, \
                   Description=NeptuneMLRole, \
                   ApplyMethod=pending-reboot" \
     --region (AWS region, such as us-east-1)
   ```

   設定此參數可讓 Neptune 存取 SageMaker AI，而無需在每次呼叫時傳入角色。

   如需如何建立 `SageMakerExcecutionIAMRole` 的相關資訊，請參閱 [建立自訂 NeptuneSageMakerIAMRole 角色](machine-learning-manual-setup.md#ml-manual-setup-sm-role)。

1. 最後，使用 `describe-db-cluster-parameters` 檢查新資料庫叢集參數群組中的所有參數是否都按照您想要的方式進行設定：

   ```
   aws neptune describe-db-cluster-parameters \
     --db-cluster-parameter-group-name (name of the new DB cluster parameter group) \
     --region (AWS region, such as us-east-1)
   ```

## 將新的資料庫叢集參數群組附加至您將搭配 Neptune ML 使用的資料庫叢集
<a name="machine-learning-enabling-attach-param-group"></a>

現在，您可以使用下列命令，將剛建立的新資料庫叢集參數群組附加至現有的資料庫叢集：

```
aws neptune modify-db-cluster \
  --db-cluster-identifier (the name of your existing DB cluster) \
  --apply-immediately
  --db-cluster-parameter-group-name (name of your new DB cluster parameter group) \
  --region (AWS region, such as us-east-1)
```

若要使所有參數生效，您可以接著重新啟動資料庫叢集：

```
aws neptune reboot-db-instance
  --db-instance-identifier (name of the primary instance of your DB cluster) \
  --profile (name of your AWS profile to use) \
  --region (AWS region, such as us-east-1)
```

或者，如果您要建立新的資料庫叢集以搭配 Neptune ML 使用，則可以使用下列命令，建立附加新參數群組的叢集，然後建立新的主要 (寫入器) 執行個體：

```
cluster-name=(the name of the new DB cluster)
aws neptune create-db-cluster
  --db-cluster-identifier ${cluster-name}
  --engine graphdb \
  --engine-version 1.0.4.1 \
  --db-cluster-parameter-group-name (name of your new DB cluster parameter group) \
  --db-subnet-group-name (name of the subnet to use) \
  --region (AWS region, such as us-east-1)

aws neptune create-db-instance
  --db-cluster-identifier ${cluster-name}
  --db-instance-identifier ${cluster-name}-i \
  --db-instance-class (the instance class to use, such as db.r5.xlarge)
  --engine graphdb \
  --region (AWS region, such as us-east-1)
```

## 將 `NeptuneSageMakerIAMRole`連接至資料庫叢集，以便其可存取 SageMaker AI 和 Amazon S3 資源
<a name="machine-learning-enabling-attach-neptune-sagemaker-role"></a>

最後，請依照 中的指示[建立自訂 NeptuneSageMakerIAMRole 角色](machine-learning-manual-setup.md#ml-manual-setup-sm-role)建立 IAM 角色，讓您的資料庫叢集能夠與 SageMaker AI 和 Amazon S3 通訊。然後，使用以下命令將您建立的 `NeptuneSageMakerIAMRole` 角色附加至資料庫叢集：

```
aws neptune add-role-to-db-cluster
  --db-cluster-identifier ${cluster-name}
  --role-arn arn:aws:iam::(the ARN number of the role's ARN):role/NeptuneMLRole \
  --region (AWS region, such as us-east-1)
```

## 在 Neptune VPC 中為 SageMaker AI 建立兩個端點
<a name="machine-learning-sm-endpoints"></a>

Neptune ML 在 Neptune 資料庫叢集的 VPC 中需要兩個 SageMaker AI 端點：
+ `com.amazonaws.(AWS region, like us-east-1).sagemaker.runtime`
+ `com.amazonaws.(AWS region, like us-east-1).sagemaker.api`

如果您尚未使用快速入門 CloudFormation 範本，而該範本會自動為您建立這些範本，您可以使用下列 AWS CLI 命令來建立這些範本：

下列命令會建立 `sagemaker.runtime` 端點：

```
aws ec2 create-vpc-endpoint
  --vpc-id (the ID of your Neptune DB cluster's VPC)
  --vpc-endpoint-type Interface
  --service-name com.amazonaws.(AWS region, like us-east-1).sagemaker.runtime
  --subnet-ids (the subnet ID or IDs that you want to use)
  --security-group-ids (the security group for the endpoint network interface, or omit to use the default)
  --private-dns-enabled
```

下列命令則會建立 `sagemaker.api` 端點：

```
aws ec2 create-vpc-endpoint
  --vpc-id (the ID of your Neptune DB cluster's VPC)
  --vpc-endpoint-type Interface
  --service-name com.amazonaws.(AWS region, like us-east-1).sagemaker.api
  --subnet-ids (the subnet ID or IDs that you want to use)
  --security-group-ids (the security group for the endpoint network interface, or omit to use the default)
  --private-dns-enabled
```

您也可以使用 [VPC 主控台](https://console.aws.amazon.com/vpc/)建立這些端點。請參閱 [Amazon SageMaker with AWS PrivateLink 中的安全預測呼叫](https://aws.amazon.com/blogs/machine-learning/secure-prediction-calls-in-amazon-sagemaker-with-aws-privatelink/)和[使用 AWS PrivateLink 保護所有 Amazon SageMaker API 呼叫](https://aws.amazon.com/blogs/machine-learning/securing-all-amazon-sagemaker-api-calls-with-aws-privatelink/)。

## 在資料庫叢集參數群組中建立 SageMaker AI 推論端點參數
<a name="machine-learning-set-inference-endpoint-cluster-parameter"></a>

若要避免在對模型進行的每個查詢中指定模型的 SageMaker AI 推論端點，請在 Neptune ML 的資料庫叢集參數群組`neptune_ml_endpoint`中建立名為 的資料庫叢集參數。將參數設定為有問題執行個體端點的 `id`。

您可以使用下列 AWS CLI 命令來執行此操作：

```
aws neptune modify-db-cluster-parameter-group \
  --db-cluster-parameter-group-name neptune-ml-demo \
  --parameters "ParameterName=neptune_ml_endpoint, \
                ParameterValue=(the name of the SageMaker AI inference endpoint you want to query), \
                Description=NeptuneMLEndpoint, \
                ApplyMethod=pending-reboot" \
  --region (AWS region, such as us-east-1)
```