focusOtter / eventbridge-target-appsync

An example repo that showcases how Amazon EventBridge can invoke AWS AppSync as a direct target with IAM permissions

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Amazon EventBridge to AWS AppSync

image

On January 11, 2024, Amazon EventBridge added AWS AppSync as a direct target. While the two could be tied together with an AWS Lambda function or as a direct API via an API Key, this solution provides a tighter and more scalable way of orchestrating data across applications.

This repository shows how to get a simple integration between the two where an event is put on a new event bus and an AppSync mutation is invoked.

🗒️ At the time of this repository creation, the L2 construct for EventBridge Rules does not support AppSync as a target. As such, we drop down to the L1 construct. This repository will be updated once the L2 construct is updated.

Deploying this sample

After cloning this repository, install the dependencies with the following command:

npm i

From there, deploy the cdk project by running the following command:

npx aws-cdk deploy

or if using an AWS Profile:

npx aws-cdk deploy --profile your-profile-name

Testing the event

Due to this process involving multiple services, it's easiest to test this out in the AWS console.

This repo is setup so that any event with a source value of sample.source will trigger the rule:

// lib/choreography/eventbridge.ts
eventPattern: {
			source: ['sample.source'],
		},

In addition, the AppSync API, expects a msg value to be passed:

# lib/api/schema.graphql
type Mutation {
	publishMsgFromEB(msg: String!): String! @aws_iam
}

That means in the AWS Console, we can send a msg string in the event.detail section of our event, and check CloudWatch to verify the API was been triggered and the EventBridge message has been successfully passed.

image image

Subscribing to Real-time data

AWS AppSync comes with real-time data support out-of-the-box through the use of Subscriptions. Subscriptions subscribe to Mutations:

type Subscription {
	onPublishMsgFromEb: String
		@aws_cognito_user_pools
		@aws_subscribe(mutations: ["publishMsgFromEB"])
}

The @aws_cognito_user_pools directive means users have to be signed in with Cognito to subscribe to this operation. Fortunately, this application comes with Cognito setup and integrated with our AppSync API.

To test this out, create a cognito user in the AWS Console and make sure to send an email to the user with a generated password. image

From there, open up a second browser tab and signin with your username and temporary user password. The AppSync console will prompt for a new password.

Next, ensure you are subscribing to data by selecting the onPublishMsgFromEb subscription. image

Lastly, in a different/original browser tab, send an event to EventBridge like in the previous example. The message should show up on your second browser tab.

About

An example repo that showcases how Amazon EventBridge can invoke AWS AppSync as a direct target with IAM permissions


Languages

Language:TypeScript 85.6%Language:JavaScript 14.4%