AWS AppSync Amazon EventBridge 中規則的目標 - Amazon EventBridge

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

AWS AppSync Amazon EventBridge 中規則的目標

AWS AppSync 可讓開發人員使用安全、無伺服器且高效能的 GraphQL 和 Pub/Sub APIs,將其應用程式和服務連線至資料和事件。使用 AWS AppSync,您可以將即時資料更新發佈至具有 GraphQL 變動的應用程式。EventBridge 支援為相符事件呼叫有效 GraphQL 突變作業。當您指定 AWS AppSync API 變動做為目標時, 會透過變動操作 AWS AppSync 處理事件,然後觸發連結至變動的訂閱。

注意

EventBridge 支援 AWS AppSync 公有 GraphQL APIs。EventBridge 目前不支援 AWS AppSync 私有 APIs。

您可以針對下列使用案例使用 AWS AppSync GraphQL API 目標:

  • 將事件資料推送、轉換和儲存到您設定的資料來源中。

  • 將即時通知傳送至連線的應用程式用戶端。

注意

AWS AppSync 目標僅支援使用 AWS_IAM 授權類型呼叫 AWS AppSync GraphQL APIs。

如需 AWS AppSync GraphQL APIs的詳細資訊,請參閱《 AWS AppSync 開發人員指南》中的 GraphQL 和 AWS AppSync 架構

使用主控台指定 EventBridge 規則 AWS AppSync 的目標
  1. 建立或編輯此規則。

  2. 目標下,依序選擇 AWS 服務AWS AppSync來指定目標

  3. 指定要剖析和執行的突變作業,以及選取集。

    • 選擇 AWS AppSync API,然後選擇要叫用的 GraphQL API 變動。

    • 設定參數和選取集下,選擇使用鍵-值對映或輸入轉換器來建立選取集。

      Key-value mapping

      若要使用鍵-值對映來建立選取集:

      • 指定 API 參數的變數。每個變數都可以是靜態值,也可以是事件承載的動態 JSON 路徑表達式。

      • 選取集下,選擇您要在回應中包含的變數。

      Input transformer

      若要使用輸入轉換器建立選取集:

      • 指定定義要使用之變數的輸入路徑。

      • 指定輸入範本,以定義和格式化您要傳遞至目標的資訊。

      如需詳細資訊,請參閱在 EventBridge 中建立規則時設定輸入轉換器

  4. 對於執行角色,選擇是否要新建角色,或使用現有的角色。

  5. 完成規則的建立或編輯。

範例:Amazon EventBridge AWS AppSync 的目標

在下列範例中,我們將逐步解說如何指定 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 突變所觸發。

您可以使用目標建立 EventBridge 規則,該規則會透過 pushEvent 突變將事件傳送至 AppSync API。調用突變時,任何訂閱的用戶端都會收到該事件。

若要指定此 API 作為 EventBridge 規則的目標,您需執行下列動作:

  1. 將規則目標的 Amazon Resource Name (ARN) 設為 Ec2EventAPI API 的 GraphQL 端點 ARN。

  2. 將突變 GraphQL 作業指定為目標參數:

    mutation CreatePushEvent($id: ID!, $statusCode: String, $instanceId: String) { pushEvent(id: $input, statusCode: $statusCode, instanceId: $instanceId) { id statusCode instanceId } }

    突變選取集必須包含您希望在 GraphQL 訂閱中訂閱的所有欄位。

  3. 設定輸入轉換器,以指定如何在作業中使用相符事件中的資料。

    假設您選取了 “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> }