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.
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
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](https://private-user-images.githubusercontent.com/5106417/300297048-a20356a9-3e0c-42b1-8070-54c0cbeec267.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjMwNTYxOTYsIm5iZiI6MTcyMzA1NTg5NiwicGF0aCI6Ii81MTA2NDE3LzMwMDI5NzA0OC1hMjAzNTZhOS0zZTBjLTQyYjEtODA3MC01NGMwY2JlZWMyNjcucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDgwNyUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA4MDdUMTgzODE2WiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9ZmVhNmU4MjRjNzFmM2ZjZTZjMmM5YTEzYjNhMTM1NmRiMDc1MTliMTMwMzY5NjIyZjBiOTgzYzhiN2Y3Yjk5MyZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.x-6pN4AY1i5IhmRAv6fDl1_4VTVTI6_jqgHJQ2ZL0vE)
![image](https://private-user-images.githubusercontent.com/5106417/300297132-3f9c2453-c7d0-47ab-a024-77180848e10c.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjMwNTYxOTYsIm5iZiI6MTcyMzA1NTg5NiwicGF0aCI6Ii81MTA2NDE3LzMwMDI5NzEzMi0zZjljMjQ1My1jN2QwLTQ3YWItYTAyNC03NzE4MDg0OGUxMGMucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDgwNyUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA4MDdUMTgzODE2WiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9NjYyMDI5YmYwNDg2MWM0Y2ZjNDA1OWE5MTk0ODNlMjM1NzJhZWYwODczMTJjMTEwNzQzMTY0ZTc1OTkwNzE2ZSZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.ak8kqcA2uYriELO2JO7OaGYn_VaWNFymN-1vH-qMgDw)
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.
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.
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.