本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
AWS AppSync Amazon 的目標 EventBridge
AWS AppSync 使開發人員能夠使用安全、無伺服器和高效能 GraphQL 和 Pub/Sub API,將其應用程式和服務與資料和事件連接起來。您可以使用 GraphQL 突變 AWS AppSync,將即時資料更新發佈到您的應用程式。 EventBridge 支援針對相符事件呼叫有效的 GraphQL 突變作業。當您指定 AWS AppSync API 突變作為目標時,會透過突變作業來 AWS AppSync 處理事件,然後觸發連結至突變的訂閱。
注意
EventBridge 支援 AWS AppSync 公用的 GraphQL API。 EventBridge 目前不支援 AWS AppSync 私有 API。
您可以針對下列使用案例使用 AWS AppSync GraphQL API 目標:
將事件資料推送、轉換和儲存到您設定的資料來源中。
將即時通知傳送至連線的應用程式用戶端。
注意
AWS AppSync 目標僅支援使用AWS_IAM
授權類型呼叫 AWS AppSync GraphQL API。
如需 AWS AppSync GraphQL API 的詳細資訊,請參閱AWS AppSync 開發人員指南中的 GraphQL 和 AWS AppSync 架構。
使用控制台指 AWS AppSync 定 EventBridge 規則的目標
示例:Amazon 的 AWS AppSync 目標 EventBridge
在下列範例中,我們將逐步介紹如何指定 EventBridge 規則的 AWS AppSync 目標,包括定義輸入轉換以設定要傳遞之事件格式的輸入轉換。
假設您有由下列結構描述定義的 AWS AppSync GraphQL API:Ec2EventAPI
type Event { id: ID! statusCode: String instanceId: String } type Mutation { pushEvent(id: ID!, statusCode: String!, instanceId: String): Event } type Query { listEvents: [Event] } type Subscription { subscribeToEvent(id: ID, statusCode: String, instanceId: String): Event @aws_subscribe(mutations: ["pushEvent"]) }
使用此 API 的應用程式用戶端可以訂閱 subscribeToEvent
,該訂閱是由 pushEvent
突變所觸發。
您可以建立具有透過pushEvent
變異將事件傳送至 AppSync API 的目標的 EventBridge 規則。調用突變時,任何訂閱的用戶端都會收到該事件。
若要將此 API 指定為 EventBridge 規則的目標,請執行下列動作:
將規則目標的 Amazon Resource Name (ARN) 設為
Ec2EventAPI
API 的 GraphQL 端點 ARN。將突變 GraphQL 作業指定為目標參數:
mutation CreatePushEvent($id: ID!, $statusCode: String, $instanceId: String) { pushEvent(id: $input, statusCode: $statusCode, instanceId: $instanceId) { id statusCode instanceId } }
突變選取集必須包含您希望在 GraphQL 訂閱中訂閱的所有欄位。
設定輸入轉換器,以指定如何在作業中使用相符事件中的資料。
假設您選取了
“EC2 Instance Launch Successful”
範例事件:{ "version": "0", "id": "3e3c153a-8339-4e30-8c35-687ebef853fe", "detail-type": "EC2 Instance Launch Successful", "source": "aws.autoscaling", "account": "123456789012", "time": "2015-11-11T21:31:47Z", "region": "us-east-1", "resources": ["arn:aws:autoscaling:us-east-1:123456789012:autoScalingGroup:eb56d16b-bbf0-401d-b893-d5978ed4a025:autoScalingGroupName/sampleLuanchSucASG", "arn:aws:ec2:us-east-1:123456789012:instance/i-b188560f"], "detail": { "StatusCode": "InProgress", "AutoScalingGroupName": "sampleLuanchSucASG", "ActivityId": "9cabb81f-42de-417d-8aa7-ce16bf026590", "Details": { "Availability Zone": "us-east-1b", "Subnet ID": "subnet-95bfcebe" }, "RequestId": "9cabb81f-42de-417d-8aa7-ce16bf026590", "EndTime": "2015-11-11T21:31:47.208Z", "EC2InstanceId": "i-b188560f", "StartTime": "2015-11-11T21:31:13.671Z", "Cause": "At 2015-11-11T21:31:10Z a user request created an AutoScalingGroup changing the desired capacity from 0 to 1. At 2015-11-11T21:31:11Z an instance was started in response to a difference between desired and actual capacity, increasing the capacity from 0 to 1." } }
您可以使用目標輸入轉換器的輸入路徑,定義要在範本中使用的下列變數:
{ "id": "$.id", "statusCode": "$.detail.StatusCode", "EC2InstanceId": "$.detail.EC2InstanceId" }
撰寫輸入轉換器範本,以定義 EventBridge 傳遞至變 AWS AppSync 異作業的變數。此範本必須評估為 JSON。考量到輸入路徑,您可以撰寫以下範本:
{ "id": <id>, "statusCode": <statusCode>, "instanceId": <EC2InstanceId> }