使用AWS AppSync私有 API - AWS AppSync

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

使用AWS AppSync私有 API

如果您使用亞馬遜虛擬私有雲(亞馬遜 VPC),則可以創建AWS AppSync私有 API,這是只能從 VPC 存取的 API。使用私有 API,您可以限制對內部應用程式的 API 存取,並連線到 GraphQL 和即時端點,而不會公開資料。

若要在您的 VPC 和AWS AppSync服務,您必須建立VPC 端點介面。介面端點是由 AWS PrivateLink 供電,讓您不需要網際網路閘道、NAT 裝置、VPN 連線或 AWS Direct Connect 連線即可私密存取 AWS AppSync API。VPC 中的執行個體不需要公有 IP 地址,即能與 AWS AppSync API 通訊。​ 您的 VPC 和之間的流量AWS AppSync不離開AWS網絡。

AWS 雲端 architecture showing VPC with public and private subnets connecting to AWS AppSync via PrivateLink.

在啟用私有 API 功能之前,還有一些其他因素需要考慮:

  • 設定下列項目的 VPC 介面端點AWS AppSync啟用私有 DNS 功能將防止 VPC 中的資源叫用其他AWS AppSync公用 API 使用AWS AppSync產生的 API 網址。這是由於對公共 API 的請求通過接口端點進行路由,這是公共 API 不允許的。若要在此案例中叫用公用 API,建議您在公用 API 上設定自訂網域名稱,然後 VPC 中的資源可使用這些網域名稱來叫用公用 API。

  • 您的AWS AppSync只有您的 VPC 才能使用私有 API。該AWS AppSync主控台查詢編輯器只有在瀏覽器的網路設定可以將流量路由到您的 VPC (例如透過 VPN 或透過 VPN 連線) 時,才能存取您的 APIAWS Direct Connect).

  • 使用 VPC 介面端點AWS AppSync,您可以訪問相同的任何私有 APIAWS帳戶和地區。要進一步限制對私有 API 的訪問,您可以考慮以下選項:

    • 確保只有必要的管理員可以建立 VPC 端點介面AWS AppSync。

    • 使用 VPC 端點自訂原則來限制可從 VPC 中的資源叫用哪些 API。

    • 對於 VPC 中的資源,建議您使用 IAM 授權來叫用AWS AppSync通過確保將資源分配給 API 的範圍內角色來實現 API。

  • 建立或使用限制 IAM 主體的政策時,您必須設定authorizationType的方法AWS_IAM或者NONE

創建AWS AppSync私有 API

下面的步驟說明如何創建私有 APIAWS AppSync服務。

警告

您只能在建立 API 期間啟用私有 API 功能。無法修改此設定AWS AppSync應用程式介面或AWS AppSync私人 API 在創建之後。

  1. 登入 AWS Management Console 並開啟 AppSync主控台

    1. 儀表板上,選擇 Create API (建立 API)

  2. 選擇從頭開始設計 API,然後選擇下一步

  3. 私有 API區段中,選擇使用私有 API 功能

  4. 設定其餘選項,檢閱 API 的資料,然後選擇創建

在您可以使用AWS AppSync私有 API,您必須配置接口端點AWS AppSync在您的虛擬私人雲端。請注意,私有 API 和 VPC 必須位於相同AWS帳戶和地區。

為建立介面端點AWS AppSync

您可以為下列項目建立介面端點AWS AppSync使用亞馬遜 VPC 控制台或AWS Command Line Interface(AWS CLI). 如需詳細資訊,請參閱 Amazon VPC 使用者指南中的建立介面端點

Console
  1. 登入AWS Management Console並打開端點亞馬遜 VPC 控制台的頁面。

  2. 選擇 Create endpoint (建立端點)。

    1. 服務類別欄位中,確認AWS服務已選取。

    2. 服務表格中,選擇com.amazonaws.{region}.appsync-api。驗證类型列值是Interface

    3. VPC」欄位中,選擇 VPC 及其子網路。

    4. 若要為介面端點啟用私有 DNS 功能,請勾選啟用 DNS 名稱核取方塊。

    5. 安全性群組」欄位中,選擇一或多個安全性群組。

  3. 選擇 Create endpoint (建立端點)。

CLI

使用 create-vpc-endpoint 命令,並指定 VPC ID、VPC 端點 (介面) 的類型、服務名稱、要使用端點的子網路,以及要與端點網路介面建立關聯的安全群組。例如:

$ aws ec2 create-vpc-endpoint —vpc-id vpc-ec43eb89 \ —vpc-endpoint-type Interface \ —service-name com.amazonaws.{region}.appsync-api \ —subnet-id subnet-abababab —security-group-id sg-1a2b3c4d

若要使用私有 DNS 選項,您必須設定enableDnsHostnamesenableDnsSupportattributes您的虛擬私人雲端的值。如需詳細資訊,請參閱 Amazon VPC 使用者指南中的檢視並更新 VPC 的 DNS 支援。如果您為介面端點啟用私有 DNS 功能,您可以向您的AWS AppSyncAPI GraphQL 和即時端點使用其預設公有 DNS 端點,格式如下:

https://{api_url_identifier}.appsync-api.{region}.amazonaws.com/graphql

如需服務端點的詳細資訊,請參閱服務端點和配額AWS一般參考

如需服務與介面端點互動的詳細資訊,請參閱透過介面端點存取服務亞馬遜 VPC 用戶指南

如需使用建立和設定端點的相關資訊AWS CloudFormation,請參閱AWS:: EC2:: 點點中的資源AWS CloudFormation使用者指南

進階 範例

如果您為介面端點啟用私有 DNS 功能,您可以向您的AWS AppSyncAPI GraphQL 和即時端點使用其預設公有 DNS 端點,格式如下:

https://{api_url_identifier}.appsync-api.{region}.amazonaws.com/graphql

使用介面 VPC 端點公用 DNS 主機名稱,呼叫 API 的基本 URL 將採用下列格式:

https://{vpc_endpoint_id}-{endpoint_dns_identifier}.appsync-api.{region}.vpce.amazonaws.com/graphql

如果您已在 AZ 中部署端點,也可以使用 AZ 特定的 DNS 主機名稱:

https://{vpc_endpoint_id}-{endpoint_dns_identifier}-{az_id}.appsync-api.{region}.vpce.amazonaws.com/graphql.

使用 VPC 端點公用 DNS 名稱將需要AWS AppSync要傳遞的 API 端點主機名稱Host或作為 x-appsync-domain頭的請求。這些範例使用TodoAPI這是在啟動範例結構描述指南:

curl https://{vpc_endpoint_id}-{endpoint_dns_identifier}.appsync-api.{region}.vpce.amazonaws.com/graphql \ -H "Content-Type:application/graphql" \ -H "x-api-key:da2-{xxxxxxxxxxxxxxxxxxxxxxxxxx}" \ -H "Host:{api_url_identifier}.appsync-api.{region}.amazonaws.com" \ -d '{"query":"mutation add($createtodoinput: CreateTodoInput!) {\n createTodo(input: $createtodoinput) {\n id\n name\n where\n when\n description\n }\n}","variables":{"createtodoinput":{"name":"My first GraphQL task","when":"Friday Night","where":"Day 1","description":"Learn more about GraphQL"}}}'

在下面的實例中,我們將使用待辦事項在中產生的應用程式啟動範例結構描述指南。為了測試樣本待辦事項 API,我們將使用私有 DNS 來調用 API。您可以使用您選擇的任何命令行工具; 這個例子使用捲曲發送查詢和突變wscat以設定訂閱。為了模擬我們的例子,請替換括號中的值{ }在下面的命令中,包含來自您的相應值AWS帳戶。

測試突變操作 —createTodo請求

curl https://{api_url_identifier}.appsync-api.{region}.amazonaws.com/graphql \ -H "Content-Type:application/graphql" \ -H "x-api-key:da2-{xxxxxxxxxxxxxxxxxxxxxxxxxx}" \ -d '{"query":"mutation add($createtodoinput: CreateTodoInput!) {\n createTodo(input: $createtodoinput) {\n id\n name\n where\n when\n description\n }\n}","variables":{"createtodoinput":{"name":"My first GraphQL task","when":"Friday Night","where":"Day 1","description":"Learn more about GraphQL"}}}'

測試突變操作 —createTodo回應

{ "data": { "createTodo": { "id": "<todo-id>", "name": "My first GraphQL task", "where": "Day 1", "when": "Friday Night", "description": "Learn more about GraphQL" } } }

測試查詢操作 —listTodos請求

curl https://{api_url_identifier}.appsync-api.{region}.amazonaws.com/graphql \ -H "Content-Type:application/graphql" \ -H "x-api-key:da2-{xxxxxxxxxxxxxxxxxxxxxxxxxx}" \ -d '{"query":"query ListTodos {\n listTodos {\n items {\n description\n id\n name\n when\n where\n }\n }\n}\n","variables":{"createtodoinput":{"name":"My first GraphQL task","when":"Friday Night","where":"Day 1","description":"Learn more about GraphQL"}}}'

測試查詢操作 —listTodos請求

{ "data": { "listTodos": { "items": [ { "description": "Learn more about GraphQL", "id": "<todo-id>", "name": "My first GraphQL task", "when": "Friday night", "where": "Day 1" } ] } } }

測試訂閱操作 — 訂閱createTodo突變

若要在中設定 GraphQL 訂閱AWS AppSync,請參閱建立即時WebSocket用戶端。從 VPC 中的 Amazon EC2 執行個體,您可以測試AWS AppSync私有 API 訂閱端點使用wscat。下面的例子使用API KEY用於授權。

$ header=`echo '{"host":"{api_url_identifier}.appsync-api.{region}.amazonaws.com","x-api-key":"da2-{xxxxxxxxxxxxxxxxxxxxxxxxxx}"}' | base64 | tr -d '\n'` $ wscat -p 13 -s graphql-ws -c "wss://{api_url_identifier}.appsync-realtime-api.us-west-2.amazonaws.com/graphql?header=$header&payload=e30=" Connected (press CTRL+C to quit) > {"type": "connection_init"} < {"type":"connection_ack","payload":{"connectionTimeoutMs":300000}} < {"type":"ka"} > {"id":"f7a49717","payload":{"data":"{\"query\":\"subscription onCreateTodo {onCreateTodo {description id name where when}}\",\"variables\":{}}","extensions":{"authorization":{"x-api-key":"da2-{xxxxxxxxxxxxxxxxxxxxxxxxxx}","host":"{api_url_identifier}.appsync-api.{region}.amazonaws.com"}}},"type":"start"} < {"id":"f7a49717","type":"start_ack"}

或者,請使用 VPC 端點網域名稱,同時確保指定主持人中的標頭wscat命令來建立網絡套接字:

$ header=`echo '{"host":"{api_url_identifier}.appsync-api.{region}.amazonaws.com","x-api-key":"da2-{xxxxxxxxxxxxxxxxxxxxxxxxxx}"}' | base64 | tr -d '\n'` $ wscat -p 13 -s graphql-ws -c "wss://{vpc_endpoint_id}-{endpoint_dns_identifier}.appsync-api.{region}.vpce.amazonaws.com/graphql?header=$header&payload=e30=" --header Host:{api_url_identifier}.appsync-realtime-api.us-west-2.amazonaws.com Connected (press CTRL+C to quit) > {"type": "connection_init"} < {"type":"connection_ack","payload":{"connectionTimeoutMs":300000}} < {"type":"ka"} > {"id":"f7a49717","payload":{"data":"{\"query\":\"subscription onCreateTodo {onCreateTodo {description id priority title}}\",\"variables\":{}}","extensions":{"authorization":{"x-api-key":"da2-{xxxxxxxxxxxxxxxxxxxxxxxxxx}","host":"{api_url_identifier}.appsync-api.{region}.amazonaws.com"}}},"type":"start"} < {"id":"f7a49717","type":"start_ack"}

運行以下突變代碼:

curl https://{api_url_identifier}.appsync-api.{region}.amazonaws.com/graphql \ -H "Content-Type:application/graphql" \ -H "x-api-key:da2-{xxxxxxxxxxxxxxxxxxxxxxxxxx}" \ -d '{"query":"mutation add($createtodoinput: CreateTodoInput!) {\n createTodo(input: $createtodoinput) {\n id\n name\n where\n when\n description\n }\n}","variables":{"createtodoinput":{"name":"My first GraphQL task","when":"Friday Night","where":"Day 1","description":"Learn more about GraphQL"}}}'

之後,會觸發訂閱,並顯示訊息通知,如下所示:

< {"id":"f7a49717","type":"data","payload":{"data":{"onCreateTodo":{"description":"Go to the shops","id":"169ce516-b7e8-4a6a-88c1-ab840184359f","priority":5,"title":"Go to the shops"}}}}

使用 IAM 政策限制公用 API 建立

AWS AppSync支援 IAMCondition聲明與私有 API 一起使用。該visibility欄位可包含在以下項目的 IAM 政策聲明中appsync:CreateGraphqlApi用於控制哪些 IAM 角色和使用者可以建立私有和公有 API 的操作。這讓 IAM 管理員能夠定義只允許使用者建立私有 GraphQL API 的 IAM 政策。嘗試建立公用 API 的使用者會收到未經授權的訊息。

例如,IAM 管理員可以建立下列 IAM 政策陳述式,以允許建立私有 API:

{ "Sid": "AllowPrivateAppSyncApis", "Effect": "Allow", "Action": "appsync:CreateGraphqlApi", "Resource": "*", "Condition": { "ForAnyValue:StringEquals": { "appsync:Visibility": "PRIVATE" } } }

IAM 管理員也可以新增下列項目服務控制政策封鎖中的所有使用者AWS組織, 從, 建立AWS AppSync私有 API 以外的 API:

{ "Sid": "BlockNonPrivateAppSyncApis", "Effect": "Deny", "Action": "appsync:CreateGraphqlApi", "Resource": "*", "Condition": { "ForAnyValue:StringNotEquals": { "appsync:Visibility": "PRIVATE" } } }