Amazon AppSync 亚马逊的目标 EventBridge - Amazon EventBridge
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

Amazon AppSync 亚马逊的目标 EventBridge

Amazon AppSync 使开发人员能够使用安全、无服务器和高性能的 GraphQL 和 Pub/Sub API 将其应用程序和服务连接到数据和事件。借助 Amazon AppSync,您可以通过 GraphQL 突变将实时数据更新发布到您的应用程序。 EventBridge 支持为匹配的事件调用有效的 GraphQL 突变操作。当你将 Amazon AppSync API 突变指定为目标时,会通过突变操作 Amazon AppSync 处理事件,然后突变操作可以触发与该突变相关的订阅。

注意

EventBridge 支持 Amazon AppSync 公共 GraphQL 接口。 EventBridge 目前不支持 Amazon AppSync 私有 API。

您可以将 Amazon AppSync GraphQL API 目标用于以下用例:

  • 推送、转换事件数据以及将其存储到您配置的数据源中。

  • 向连接的应用程序客户端发送实时通知。

注意

Amazon AppSync 目标仅支持使用AWS_IAM授权类型调用 Amazon AppSync GraphQL API。

有关 Amazon AppSync GraphQL API 的更多信息,请参阅《开发者指南》中的 G raphQL 和 Amazon AppSync 架构。Amazon AppSync

使用控制台为 EventBridge 规则指定 Amazon AppSync 目标
  1. 创建或编辑规则。

  2. 目标下,通过依次选择 Amazon 服务Amazon AppSync指定目标

  3. 指定要解析和执行的突变操作以及选择集。

    • 选择 Amazon AppSync API,然后选择要调用的 GraphQL API 突变。

    • 配置参数和选择集下,选择使用键值映射或输入转换器创建选择集。

      Key-value mapping

      要使用键值映射来创建选择集,请执行以下操作:

      • 为 API 参数指定变量。每个变量可以是静态值,也可以是事件负载的动态 JSON 路径表达式。

      • 选择集下,选择要包含在响应中的变量。

      Input transformer

      要使用输入转换器创建选择集,请执行以下操作:

      • 指定用于定义要使用的变量的输入路径。

      • 指定输入模板来定义要传递给目标的信息并为其设置格式。

      有关更多信息,请参阅 在创建规则的过程中配置输入转换器

  4. 对于执行角色,选择是创建新的角色还是使用现有角色。

  5. 完成规则的创建或编辑。

示例:Amazon 的 Amazon AppSync 目标 EventBridge

在以下示例中,我们将介绍如何为 EventBridge 规则指定 Amazon AppSync 目标,包括定义输入转换以格式化要交付的事件。

假设你有一个 Amazon AppSync GraphQL APIEc2EventAPI,由以下架构定义:

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 的应用程序客户端可以订阅由 pushEvent 突变触发的 subscribeToEvent 订阅。

你可以创建一个带有通过pushEvent突变向 AppSync API 发送事件的目标的 EventBridge 规则。当调用突变时,任何订阅的客户端都将收到该事件。

要将此 API 指定为 EventBridge 规则的目标,您需要执行以下操作:

  1. 将规则目标的 Amazon 资源名称(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 传递给 Amazon AppSync 突变操作的变量。模板的计算结果必须为 JSON。根据我们的输入路径,您可以编写以下模板:

    { "id": <id>, "statusCode": <statusCode>, "instanceId": <EC2InstanceId> }