本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
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 目标
示例: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 规则的目标,您需要执行以下操作:
将规则目标的 Amazon 资源名称(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 传递给 Amazon AppSync 突变操作的变量。模板的计算结果必须为 JSON。根据我们的输入路径,您可以编写以下模板:
{ "id": <id>, "statusCode": <statusCode>, "instanceId": <EC2InstanceId> }