

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

# API 取用者：將 VPC 端點與和您分享的私有自訂網域名稱建立關聯
<a name="apigateway-private-custom-domains-consumer-create"></a>

下列程序顯示如何在另一個 中使用私有網域名稱 AWS 帳戶。視您與 API 提供者的信任關係而定， AWS RAM 可能會為您完成一些任務。

當您位於 AWS 帳戶 與私有自訂網域名稱不同的 時，您只能將 VPC 端點與私有自訂網域名稱建立關聯並叫用它。您無法檢視私有自訂網域名稱的 `policy` 或其他任何參數。

## 先決條件
<a name="apigateway-private-custom-domains-consumer-prerequisites"></a>

在另一個 中使用私有自訂網域名稱時，需要下列先決條件 AWS 帳戶：
+ `execute-api` 服務的 VPC 和 VPC 端點。您的 VPC 必須將 `enableDnsHostnames` 和 `enableDnsSupport` 設為 `true`。
+ 建議您為每個 VPC 端點設定至少兩個可用區域。

## (選用) 接受私有自訂網域資源分享
<a name="apigateway-private-custom-domains-consumer-accept-resource-share"></a>

如果您的 API 提供者 AWS RAM 用來建立資源共享，您有 **12 小時**的時間可以接受它。如果您使用 AWS Organizations 與 API 提供者位於相同的組織中，則會自動接受共用。如果您所在的組織已啟用自動分享資源，則會自動與您分享資源。

------
#### [ AWS 管理主控台 ]

若要使用 AWS 管理主控台，請參閱*AWS RAM 《 使用者指南*》中的[接受和拒絕資源共用邀請](https://docs.aws.amazon.com/ram/latest/userguide/working-with-shared-invitations.html)。

------
#### [ AWS CLI ]

若要尋找與您分享的所有資源，請使用下列 [get-resource-share-invitations](https://docs.aws.amazon.com/cli/latest/reference/ram/get-resource-share-invitations.html) 命令：

```
aws ram get-resource-share-invitations \
    --region us-west-2
```

使用產生的資源分享 ARN，接受資源分享邀請。下列 [accept-resource-share-invitation](https://docs.aws.amazon.com/cli/latest/reference/ram/accept-resource-share-invitation.html) 命令接受資源分享。

```
aws ram accept-resource-share-invitation \
    --resource-share-invitation-arn arn:aws:ram:us-west-2:123456789012:resource-share-invitation/1e3477be-4a95-46b4-bbe0-c4001EXAMPLE \
    --region us-west-2
```

------

## 為您的 VPC 端點與分享的私有自訂網域名稱建立關聯
<a name="apigateway-private-custom-domains-consumer-associate"></a>

由於私有自訂網域名稱不是唯一的，因此您可以為 VPC 端點與唯一的自訂網域名稱 ARN 建立關聯。建立網域名稱存取關聯後，VPC 端點最多可能需要 15 分鐘才能成功調用您的私有自訂網域名稱。如果您有用於存取公有自訂網域名稱的 VPC 端點，請勿使用該端點建立任何網域名稱存取關聯。

------
#### [ AWS 管理主控台 ]

**為您的 VPC 端點與分享的私有自訂網域名稱建立關聯**

1. 在以下網址登入 API Gateway 主控台：[https://console.aws.amazon.com/apigateway](https://console.aws.amazon.com/apigateway)。

1. 在主要導覽窗格中，選擇**網域名稱存取關聯**。

1. 選擇**建立網域名稱存取關聯**。

1. 針對**網域名稱 ARN**，選取 API 提供者與您共用的網域名稱 ARN。

   網域名稱 ARN 可能不會出現在下拉式清單中。您可以使用 AWS RAM 主控台來檢視與您共用的網域名稱，然後複製網域名稱 ARN 並將其輸入此欄位。

1. 針對 **VPC 端點 ID**，選取您要與之形成網域名稱存取關聯的 VPC 端點 ID。

1. 選擇**建立網域名稱存取關聯**。

------
#### [ AWS CLI ]

由於私有自訂網域名稱不是唯一的，因此您可以為 VPC 端點與唯一的自訂網域名稱 ARN 建立關聯。若要尋找網域名稱 ARN，請使用下列其中一個命令。

1.   
**AWS RAM**  
下列 [list-resources](https://docs.aws.amazon.com/cli/latest/reference/ram/list-resources.html) 命令會列出與您分享的資源。API 提供者必須使用 AWS RAM 與您共用其私有自訂網域，才能使用此命令。  

   ```
   aws ram list-resources \
       --resource-owner OTHER-ACCOUNTS \
       --region us-west-2
       --resource-type apigateway:Domainnames
   ```  
**API Gateway**  
下列`get-domain-names`命令會列出其他 擁有的所有私有自訂網域名稱 AWS 帳戶 ，您可以與其建立網域名稱存取關聯。  

   ```
   aws apigateway get-domain-names \
       --resource-owner OTHER_ACCOUNTS \
       --region us-west-2
   ```

1.  擷取 ARN 之後，使用 API Gateway 在您的 VPC 端點與共用私有自訂網域名稱之間建立網域名稱存取關聯。使用下列 `create-domain-name-access-association` 命令：

   ```
   aws apigateway create-domain-name-access-association \
       --access-association-source-type VPCE \
       --access-association-source 'vpce-1a2b3c4d5e6f1a2b3' \
       --domain-name-arn arn:aws:apigateway:us-west-2:111122223333:/domainnames/private.example.com+abcd1234"
   ```

   輸出將如下所示：

   ```
   {
       "domainNameAccessAssociationARN": "arn:aws:apigateway:us-west-2:444455556666:/domainnameaccessassociations/domainname/private.example.com+abcd1234/vpcesource/vpce-abcd1234efg", 
       "accessAssociationSource": "vpce-1a2b3c4d5e6f1a2b3",
       "accessAssociationSourceType": "VPCE",
       "domainNameARN" : "arn:aws:apigateway:us-west-1:111122223333:/domainnames/private.example.com+a1b2c3"
   }
   ```

------

為您的 VPC 端點與私有自訂網域名稱建立關聯後，請確認您的 API 提供者已更新其私有自訂網域名稱的政策，以允許您的 VPC 端點調用其網域名稱。如需詳細資訊，請參閱[允許其他帳戶調用您的私有自訂網域名稱](apigateway-private-custom-domains-provider-share.md#apigateway-private-custom-domains-provider-policy-update)。

## 建立 Route 53 託管區域
<a name="apigateway-private-custom-domains-consumer-create-route-53-private-hosted-zone"></a>

若要解析私有自訂網域名稱，您需要建立 Route 53 私有託管區域。託管區域是一種容器，其中包含的資訊說明您可以如何在一或多個 VPC 中路由某個網域的流量，而不用將資源公開至網際網路。如需詳細資訊，請參閱[使用私有託管區域](https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/hosted-zones-private.html)。

------
#### [ AWS 管理主控台 ]

若要使用 AWS 管理主控台，請參閱《*Amazon Route 53 開發人員指南*》中的[建立私有託管區域](https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/hosted-zone-private-creating.html)。

對於**名稱**，請使用私有自訂網域名稱的名稱。對於 **VPC ID**，請使用包含您用於網域名稱存取關聯的 VPC 端點的 VPC。

------
#### [ AWS CLI ]

下列 [create-hosted-zone](https://docs.aws.amazon.com/cli/latest/reference/route53/create-hosted-zone.html) 命令會建立私有託管區域：

```
aws route53 create-hosted-zone --name private.example.com \
    --caller-reference 2014-04-01-18:47 \
    --hosted-zone-config Comment="command-line version",PrivateZone=true \
    --vpc VPCRegion=us-west-2,VPCId=vpc-abcd1234
```

輸出內容包含託管區域 ID。您可以在下列步驟中使用託管區域 ID。

------

## 建立 Route 53 DNS 記錄
<a name="apigateway-private-custom-domains-consumer-create-route-53-record"></a>

建立託管區域後，您可以建立記錄來解析私有自訂網域。在此範例中，您會建立 A 記錄類型。如果您針對 VPC 端點使用 IPv6，請建立 AAAA 記錄類型。如果您為 VPC 端點使用雙堆疊，請同時建立 AAAA 和 A 記錄類型。

------
#### [ AWS 管理主控台 ]

若要使用 AWS 管理主控台，請參閱[使用您的網域名稱將流量路由到 Amazon API Gateway API](https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/routing-to-api-gateway.html)。

使用**快速建立**並開啟**別名**。對於端點，請使用 VPC 端點 DNS 名稱。

------
#### [ AWS CLI ]

若要設定您的 DNS 記錄，將私有自訂網域名稱映射至指定託管區域 ID 的主機名稱，首先請建立 JSON 檔案，其中包含用於設定私有網域名稱之 DNS 記錄的組態。

以下 `setup-dns-record.json` 顯示如何建立 DNS `A` 記錄，將私有自訂網域名稱映射至其私有主機名稱。您提供 VPC DNS ID 的 `DNSName`，以及您在上一個步驟中建立的託管區域 ID。

```
{
  "Changes": [
    {
      "Action": "UPSERT",
      "ResourceRecordSet": {
        "Name": "private.example.com",
        "Type": "A",
        "AliasTarget": {
          "DNSName": "vpce-abcd1234.execute-api.us-west-2.vpce.amazonaws.com",
          "HostedZoneId": "Z2OJLYMUO9EFXC",
          "EvaluateTargetHealth": false
        }
      }
    }
  ]
}
```

下列 [change-resource-record-sets](https://docs.aws.amazon.com/cli/latest/reference/route53/change-resource-record-sets.html) 命令會為私有自訂網域名稱建立 DNS 記錄：

```
aws route53 change-resource-record-sets \
    --hosted-zone-id ZABCDEFG1234 \
    --change-batch file://path/to/your/setup-dns-record.json
```

將 `hosted-zone-id` 取代為您帳戶中所設定之 DNS 記錄的 Route 53 託管區域 ID。`change-batch` 參數值指向 JSON 檔案。

------

## API 取用者的後續步驟
<a name="apigateway-private-custom-domains-consumer-next-steps"></a>

您現在可以在自己的 AWS 帳戶中調用私有 API。在您的 VPC 中，您可以使用下列 curl 命令來存取您的私有自訂網域名稱。

```
curl https://private.example.com/v1
```

如需有關調用私有 API 的其他方式的詳細資訊，請參閱 [使用自訂網域名稱調用私有 API](apigateway-private-api-test-invoke-url.md#apigateway-private-custom-domains-provider-invoke)。