本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
使用AWS AppSync私有 API
如果您使用亞馬遜虛擬私有雲(亞馬遜 VPC),則可以創建AWS AppSync私有 API,這是只能從 VPC 存取的 API。使用私有 API,您可以限制對內部應用程式的 API 存取,並連線到 GraphQL 和即時端點,而不會公開資料。
若要在您的 VPC 和AWS AppSync服務,您必須建立VPC 端點介面。介面端點是由 AWS 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 在創建之後。
-
登入 AWS Management Console 並開啟 AppSync主控台
。 -
在儀表板上,選擇 Create API (建立 API)。
-
-
選擇從頭開始設計 API,然後選擇下一步。
-
在私有 API區段中,選擇使用私有 API 功能。
-
設定其餘選項,檢閱 API 的資料,然後選擇創建。
在您可以使用AWS AppSync私有 API,您必須配置接口端點AWS AppSync在您的虛擬私人雲端。請注意,私有 API 和 VPC 必須位於相同AWS帳戶和地區。
為建立介面端點AWS AppSync
您可以為下列項目建立介面端點AWS AppSync使用亞馬遜 VPC 控制台或AWS Command Line Interface(AWS CLI). 如需詳細資訊,請參閱 Amazon VPC 使用者指南中的建立介面端點。
若要使用私有 DNS 選項,您必須設定enableDnsHostnames
和enableDnsSupportattributes
您的虛擬私人雲端的值。如需詳細資訊,請參閱 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。您可以使用您選擇的任何命令行工具; 這個例子使用捲曲{ }
在下面的命令中,包含來自您的相應值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 訂閱端點使用wscatAPI
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" } } }