

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

# 使用 Amazon Virtual Private Cloud 保護任務
<a name="usingVPC"></a>

Amazon Comprehend 使用各種安全措施，以確保資料在 Amazon Comprehend 使用期間存放在其中的任務容器安全。不過，任務容器會透過網際網路存取 AWS 資源，例如存放資料和模型成品的 Amazon S3 儲存貯體。

若要控制對資料的存取，建議您建立*虛擬私有雲端* (VPC) 並進行設定，以便無法透過網際網路存取資料和容器。如需 VPC 在建立和設定方面的資訊，請參閱 *Amazon VPC 使用者指南*中的 [Amazon VPC 入門](https://docs.aws.amazon.com/vpc/latest/userguide/getting-started-ipv4.html)的相關文章。使用 VPC 有助於保護您的資料，因為您可以設定 VPC，使其不會連線到網際網路。使用 VPC 也可讓您使用 VPC 流程日誌來監控任務容器內外的所有網路流量。如需詳細資訊，請參閱「Amazon VPC 使用者指南」**中的 [VPC 流程日誌](https://docs.aws.amazon.com/vpc/latest/userguide/flow-logs.html)。

您可以在建立任務時指定 VPC 組態，方法是指定子網路和安全群組。當您指定子網路和安全群組時，Amazon Comprehend 會在其中一個子網路中建立與安全群組相關聯的*彈性網路介面* (ENIs)。ENIs我們的任務容器連線到 VPC 中的資源。如需 ENI 的相關資訊，請參閱 *Amazon VPC 使用者指南*中的[彈性網路介面](https://docs.aws.amazon.com/vpc/latest/userguide/VPC_ElasticNetworkInterfaces.html)。

**注意**  
對於任務，您只能使用執行個體在共用硬體上執行的預設租用 VPC 來設定子網路。如需 VPCs 租用屬性的詳細資訊，請參閱《*Amazon EC2 使用者指南*》中的[專用執行個體](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/dedicated-instance.html)。

## 設定任務以進行 Amazon VPC 存取
<a name="VPCaccess"></a>

若要在 VPC 中指定子網路和安全群組，請使用適用 API 的 `VpcConfig`請求參數，或在 Amazon Comprehend 主控台中建立任務時提供此資訊。Amazon Comprehend 會使用此資訊來建立 ENIs並將其連接至我們的任務容器。ENIs 為任務容器提供 VPC 內未連線至網際網路的網路連線。

下列 APIs包含 `VpcConfig`請求參數：
+ `Create*` APIs：` [CreateDocumentClassifier](https://docs.aws.amazon.com/comprehend/latest/APIReference/API_CreateDocumentClassifier.html)`、 ` [CreateEntityRecognizer](https://docs.aws.amazon.com/comprehend/latest/APIReference/API_CreateEntityRecognizer.html) `
+ `Start*` APIs：` [StartDocumentClassificationJob](https://docs.aws.amazon.com/comprehend/latest/APIReference/API_StartDocumentClassificationJob.html)`、` [StartDominantLanguageDetectionJob](https://docs.aws.amazon.com/comprehend/latest/APIReference/API_StartDominantLanguageDetectionJob.html)`、` [StartEntitiesDetectionJob](https://docs.aws.amazon.com/comprehend/latest/APIReference/API_StartEntitiesDetectionJob.html)`、` [StartKeyPhrasesDetectionJob](https://docs.aws.amazon.com/comprehend/latest/APIReference/API_StartKeyPhrasesDetectionJob.html)`、` [StartSentimentDetectionJob](https://docs.aws.amazon.com/comprehend/latest/APIReference/API_StartSentimentDetectionJob.html)`、` [StartTargetedSentimentDetectionJob](https://docs.aws.amazon.com/comprehend/latest/APIReference/API_StartTargetedSentimentDetectionJob.html)`、、 ` [StartTopicsDetectionJob](https://docs.aws.amazon.com/comprehend/latest/APIReference/API_StartTopicsDetectionJob.html)`

以下是您在 API 呼叫中包含的 VpcConfig 參數範例：

```
"VpcConfig": { 
      "SecurityGroupIds": [
          " sg-0123456789abcdef0"
          ],
      "Subnets": [
          "subnet-0123456789abcdef0",
          "subnet-0123456789abcdef1",
          "subnet-0123456789abcdef2"
          ]
      }
```

若要從 Amazon Comprehend 主控台設定 VPC，請在建立任務時從選用的 **VPC 設定**區段中選擇組態詳細資訊。

![\[建立分析任務中的選用 VPC 區段\]](http://docs.aws.amazon.com/zh_tw/comprehend/latest/dg/images/vpc-image-10.png)


## 為 Amazon Comprehend 任務設定 VPC
<a name="configureVPC"></a>

為 Amazon Comprehend 任務設定 VPC 時，請使用下列準則。如需如何設定 VPC 的相關資訊，請參閱 *Amazon VPC 使用者指南*中的[使用 VPC 和子網路](https://docs.aws.amazon.com/vpc/latest/userguide/working-with-vpcs.html)的相關文章。

**確保子網路具有足夠的 IP 地址**

您的 VPC 子網路應為任務中的每個執行個體至少有兩個私有 IP 地址。如需詳細資訊，請參閱 *Amazon VPC 使用者指南*中的[ IPv4 的 VPC 與子網路的大小調整](https://docs.aws.amazon.com/vpc/latest/userguide/VPC_Subnets.html#vpc-sizing-ipv4)的相關文章。

**建立 Amazon S3 VPC 端點 **

如果您設定 VPC 讓任務容器無法存取網際網路，除非您建立允許存取的 VPC 端點，否則無法連線到包含資料的 Amazon S3 儲存貯體。透過建立 VPC 端點，您可以允許任務容器在訓練和分析任務期間存取您的資料。

當您建立 VPC 端點時，請設定這些值：
+ 選取服務類別做為**AWS 服務**
+ 將服務指定為 `com.amazonaws.region.s3`
+ 選取**閘道**做為 VPC 端點類型 

如果您使用 CloudFormation 建立 VPC 端點，請遵循 [CloudFormation VPCEndpoint](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-vpcendpoint.html) 文件。下列範例顯示 CloudFormation 範本中的 **VPCEndpoint** 組態。

```
  VpcEndpoint:
    Type: AWS::EC2::VPCEndpoint
    Properties:
      PolicyDocument:
        Version: '2012-10-17		 	 	 '
        Statement:
          - Action:
              - s3:GetObject
              - s3:PutObject
              - s3:ListBucket
              - s3:GetBucketLocation
              - s3:DeleteObject
              - s3:ListMultipartUploadParts
              - s3:AbortMultipartUpload
            Effect: Allow
            Resource:
              - "*"
            Principal: "*"
      RouteTableIds:
        - Ref: RouteTable
      ServiceName:
        Fn::Join:
          - ''
          - - com.amazonaws.
            - Ref: AWS::Region
            - ".s3"
      VpcId:
        Ref: VPC
```

我們建議您也建立自訂政策，僅允許來自 VPC 的請求存取 S3 儲存貯體。如需詳細資訊，請參閱 *Amazon VPC 使用者指南*中的[適用於 Amazon S3 的端點](https://docs.aws.amazon.com/vpc/latest/userguide/vpc-endpoints-s3.html)。

以下政策允許存取 S3 儲存貯體。編輯此政策以僅允許存取任務所需的資源。

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": "*",
            "Action": [
                "s3:GetObject",
                "s3:PutObject",
                "s3:ListBucket",
                "s3:GetBucketLocation",
                "s3:DeleteObject",
                "s3:ListMultipartUploadParts",
                "s3:AbortMultipartUpload"
            ],
            "Resource": "*"
        }
    ]
}
```

------

請為端點路由表使用預設的 DNS 設定，如此才能解析標準 Amazon S3 URL (例如 `http://s3-aws-region.amazonaws.com/amzn-s3-demo-bucket`)。如果您不使用預設 DNS 設定，請確定您用來指定任務中資料位置的 URLs 透過設定端點路由表來解析。如需 VPC 端點路由表的相關資訊，請參閱 *Amazon VPC 使用者指南*中的[閘道端點路由](https://docs.aws.amazon.com/vpc/latest/userguide/vpce-gateway.html#vpc-endpoints-routing)的相關文章。

預設端點政策可讓使用者從任務容器上的 Amazon Linux 和 Amazon Linux 2 儲存庫安裝套件。如果不希望使用者從該儲存庫安裝套件，請建立自訂端點政策，明確拒絕至 Amazon Linux 和 Amazon Linux 2 儲存庫的存取。Comprehend 本身不需要任何這類套件，因此不會影響任何功能。以下為拒絕存取上述儲存庫的政策範例：

```
{ 
    "Statement": [ 
      { 
        "Sid": "AmazonLinuxAMIRepositoryAccess",
        "Principal": "*",
        "Action": [ 
            "s3:GetObject" 
        ],
        "Effect": "Deny",
        "Resource": [
            "arn:aws:s3:::packages.*.amazonaws.com/*",
            "arn:aws:s3:::repo.*.amazonaws.com/*"
        ] 
      } 
    ] 
} 

{ 
    "Statement": [ 
        { "Sid": "AmazonLinux2AMIRepositoryAccess",
          "Principal": "*",
          "Action": [ 
              "s3:GetObject" 
              ],
          "Effect": "Deny",
          "Resource": [
              "arn:aws:s3:::amazonlinux.*.amazonaws.com/*" 
              ] 
         } 
    ] 
}
```

**的許可 `DataAccessRole`**

當您搭配分析任務使用 VPC 時，`DataAccessRole`用於 `Create*`和 `Start*`操作的 也必須具有存取輸入文件和輸出儲存貯體的 VPC 許可。

下列政策提供用於 `Create*`和 `Start*`操作`DataAccessRole`的 所需的存取權。

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
              "Action": [
                "ec2:CreateNetworkInterface",
                "ec2:CreateNetworkInterfacePermission",
                "ec2:DeleteNetworkInterface",
                "ec2:DeleteNetworkInterfacePermission",
                "ec2:DescribeNetworkInterfaces",
                "ec2:DescribeVpcs",
                "ec2:DescribeDhcpOptions",
                "ec2:DescribeSubnets",
                "ec2:DescribeSecurityGroups"
            ],
            "Resource": "*"
        }
    ]
}
```

------

**設定 VPC 安全群組**

使用分散式任務時，您必須允許相同任務中不同任務容器之間的通訊。若要執行此操作，請為安全群組設定規則，允許相同安全群組成員彼此間的傳入連線。如需詳細資訊，請參閱《*Amazon VPC 使用者指南*》中的[安全群組規則](https://docs.aws.amazon.com/vpc/latest/userguide/VPC_SecurityGroups.html#SecurityGroupRules)。

**連線到 VPC 外部的資源**

如果您設定 VPC 使其無法存取網際網路，使用該 VPC 的任務就無法存取 VPC 外部的資源。如果您的任務需要存取 VPC 外部的資源，請使用下列其中一個選項來提供存取權：
+ 如果您的任務需要存取支援介面 VPC 端點的 AWS 服務，請建立端點以連線至該服務。如需支援介面端點的服務之清單，請參閱 *Amazon VPC 使用者指南*中的 [VPC 端點](https://docs.aws.amazon.com/vpc/latest/userguide/vpc-endpoints.html)的相關文章。如需有關建立介面 VPC 端點的資訊，請參閱《*Amazon* [VPC 使用者指南》中的介面 VPC 端點 (AWS PrivateLink)](https://docs.aws.amazon.com/vpc/latest/userguide/vpce-interface.html)。
+ 如果您的任務需要存取不支援介面 VPC 端點 AWS 的服務，或存取 外部的資源 AWS，請建立 NAT 閘道並設定安全群組以允許傳出連線。如需有關為您的 VPC 設定 NAT 閘道的資訊，請參閱《Amazon [VPC 使用者指南》中的案例 2：具有公有和私有子網路 (NAT)](https://docs.aws.amazon.com/vpc/latest/userguide/VPC_Scenario2.html) 的 VPC。 **